消除Lua中表的前缀

消除Lua中表的前缀,lua,Lua,您可以这样做来访问子成员吗 function1 weaponsList.earth = {weapon_type='regular',fireTime=0, fireRate=0.7, speed = 250, img = nil} end 而不是必须这样做 "using weaponsList,earth" fireTime = 2 fireRate = 2 speed = 2 end 不确定这是什么,但是我在C或C++中看到了。在Lua有没有

您可以这样做来访问子成员吗

function1
   weaponsList.earth = {weapon_type='regular',fireTime=0, fireRate=0.7, speed = 250, img = nil}
end
而不是必须这样做

  "using weaponsList,earth"
     fireTime = 2
     fireRate = 2
     speed = 2
   end

不确定这是什么,但是我在C或C++中看到了。在Lua有没有办法做到这一点,它叫什么?< /P> < P>你可以做下面的,但是要小心。将_ENV设置为您的表意味着,在该范围内,您无法看到表外的内容

   weaponsList.earth.fireTime = 2
   weaponsList.earth.fireRate = 2
   weaponsList.earth.speed = 2
另一种可能更好的方法是:

do local _ENV = weaponsList.earth
  fireTime = 2
  fireRate = 2
  speed = 2
end

在执行多个操作之前,将嵌套表分配给局部变量实际上是基本Lua实现中的一种优化,因为在每次使用中不需要取消对外部表的引用。

您可以执行以下操作,但要小心。将_ENV设置为您的表意味着,在该范围内,您无法看到表外的内容

   weaponsList.earth.fireTime = 2
   weaponsList.earth.fireRate = 2
   weaponsList.earth.speed = 2
另一种可能更好的方法是:

do local _ENV = weaponsList.earth
  fireTime = 2
  fireRate = 2
  speed = 2
end

在执行多个操作之前,将嵌套表分配给局部变量实际上是基本Lua实现中的一个优化,因为在每次使用中不需要取消对外部表的引用。

您可以将表简化为较小的变量

do local e = weaponsList.earth
  e.fireTime = 2
  e.fireRate = 2
  e.speed = 2
end
现在t.firerate和weaponsList.earth.firerate是一样的。调整t的值也将更新原始表

不确定这是否有帮助,但您也可以将表中的所有变量保存到全局变量中

t = weaponsList.earth

编辑:感谢lhf的更正,您可以将表格简化为更小的变量

do local e = weaponsList.earth
  e.fireTime = 2
  e.fireRate = 2
  e.speed = 2
end
现在t.firerate和weaponsList.earth.firerate是一样的。调整t的值也将更新原始表

不确定这是否有帮助,但您也可以将表中的所有变量保存到全局变量中

t = weaponsList.earth

编辑:感谢更正lhf

更改
t.speed
将更改
weaponsList.earth.speed
。更改
t.speed
将更改
weaponsList.earth.speed