I want to run a which is depending on the value of local defined variable, let say XYZ
This variable can have various states, let say that interesting for me are 1, 3, 7.
I want to build a condition that if XYZ is 1 or 3 or 7 then a code is executed.
You can put [test value] = answer for all the values you need to check (and for ones you don’t specify, the answer will be nil).
To be honest, just use the OR statement for this small number of values, and only do something like this if you have a lot (say, more than one line of values in the IF statement).
@rge is quite correct. As I mentioned, there are many ways to create this structure. Recall that any non-nil value is treated as a logical ‘true’, so it doesn’t matter what value you give the table elements.
In full, both methods shown already are equivalent to:
@Slartibartfast
Some advantage of building up a function is that you can integrate it into startup lua, then call it every time you need from a various scenes, just defining the parameters according to the needs.
Then the code within a specific scene is more “elegant”, I would say.
I am also not a programmer so I am a bit “blind” here, but anyhow I want to know better ways to do something if they are.
… will always print out ‘1’, because the concatenated ‘or’ operator chooses the first non-nil value. Although, indeed, @rge is correct about the treatment of ‘false’, so …
local x = false or 42
print(x)
… will always print out ‘42’.
For an easier guide to Lua than the reference manual, try: Programming in Lua