Gullie667
Gullie667

Reputation: 91

How can a access a variable name using a string and concatination

I want to do something like the following:

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

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

However, I am not sure how to access the variable and this isn't it...

[archetype .. "Names"] 

Thanks, Gullie

Upvotes: 2

Views: 122

Answers (1)

osa1
osa1

Reputation: 7088

As @yu-hao said in his comment, you'can make up a syntax like that. You can use nasty tricks to have a similar effect, but that's not recommended. Instead do something like this:

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

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

Upvotes: 5

Related Questions