2

I'd like to be able to do this

local aoe_radius = userconfig[chosenconfig].radius --chosenconfig is a variable where value could be "small","normal" or "big" local aoe_damage = userconfig[chosenconfig].damage 

where userconfig array would something like:

local userconfig = {{name="small",radius="5",damage="50"}, {name="normal",radius="8",damage="100"}, {name="big",radius="15",damage="200"}} 

How do you access radius and damage of a given "name"?

I'd like to avoid writing it like this:

if chosenconfig == "small" then local aoe_radius = 5 local aoe_damage = 50 elseif chosenconfig == "normal" then ... end 

I'm new in Lua so I welcome redesign of above. Thanks.

1 Answer 1

4

You can do it like this:

local userconfig = {small={radius="5",damage="50"}, normal={radius="8",damage="100"}, big={radius="15",damage="200"}} local aoe_radius = userconfig[chosenconfig].radius --chosenconfig is a variable where value could be "small","normal" or "big" local aoe_damage = userconfig[chosenconfig].damage 

Note: if chosenconfig isn't "small", "normal" or "big" then userconfig[chosenconfig] gives nil, and then .radius gives an error (can't access field 'radius' of nil)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.