Dynamically assigned table variables?

时光怂恿深爱的人放手 提交于 2019-12-04 04:35:45

问题


Writing a function in Lua, which creates two tables. I want the tables to be assigned to the value name with an x added, and one with a y added. For example if name was line, it would create two tables linex and liney, but I can't figure out how to do it. The following obviously doesn't work (and is just for display purposes) but how would I go about doing this?

function makelinep(x,y,minrand,maxrand,name,length)
  name..x = {}
  name..y = {}

Later I hope to access "linex" and "liney" after values have been written.


回答1:


If you want these in the global name space you would use

_G[name..'x']={}
_G[name..'y']={}

For a module you'd use _M in place of _G.



来源:https://stackoverflow.com/questions/2180620/dynamically-assigned-table-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!