PICO-8 中的错误变量更新

Wrong variable update in PICO-8

我正在从头开始构建一个简单的贪吃蛇游戏作为 PICO-8 和 Lua 的练习。

我试图通过创建旧 body 位置的副本并沿长度更新,让 body 跟随头部。

我创建了一个 t_old 变量来存储原始 body 位置,但它与 t 同时更新。我没有解释为什么。

function train_move(t,d)
 local t_old=t --grab existing
 --update head based on direction of movement
 if d==0 then 
  t[1].x-=sprite_size --left
 elseif d==1 then
  t[1].x+=sprite_size --right
 elseif d==2 then
  t[1].y-=sprite_size --up
 else
  t[1].y+=sprite_size --down
 end
 --update body **I have noticed that t[1]==t_old[1] here??
 for i=2,#train do
  t[i].x=t_old[i-1].x
  t[i].y=t_old[i-1].y
 end
 return t
end

Table 值是通过引用复制的。

tt_old 引用相同的 table 值。

读这个How do you copy a Lua table by value?