【问题标题】:How can a access a variable name using a string and concatination如何使用字符串和连接访问变量名称
【发布时间】:2014-09-11 22:44:21
【问题描述】:

我想做如下的事情:

local archetype = "melee"
local meleeNames = { x,y,z }

if itemNumber > # [archetype .. "Names"] then 
    itemNumber = # [archetype .. "Names"] 
end

但是,我不确定如何访问该变量,这不是...

[archetype .. "Names"] 

谢谢, 古利

【问题讨论】:

  • 你不能这样编造句法。如果变量是全局变量,请使用_G 表。但是该变量在您的代码中是本地的,在这种情况下,请阅读Access local variable by name

标签: variables lua


【解决方案1】:

正如@yu-hao 在他的评论中所说,你可以组成这样的语法。您可以使用讨厌的技巧来产生类似的效果,但不建议这样做。而是做这样的事情:

local archetypes = { meleeNames = { x, y, z },
                     ... others }
local archetype = "melee"

if itemNumber > #archetypes[archetype .. "Names"] then
  itemNumber = #archetypes[archetype .. "Names"]
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多