If statement using multiple "or" condition [syntax question]

Hey guys, 

Is there a syntax in lua for using multiple “or” condition?

Say for example I want to do this:

if (gamePhase==1 or gamePhase==2 or gamePhase==3 or gamePhase==4 or gamePhase==5) then

– do something

end

What you have written is the only way

For some reason my post was incomplete,

My question was, is it possible to do something like this instead to shorten the code:

[lua]

if (gamePhase==1,2,3,4,5 ) then

– do something

end

[/lua]

 

But from your answer I can tell there is only one way, the long way…

 

Cheers!

Roy.

You could make a function to do it for you?

function multipleOr(obj, table) for i = 1, #table do if obj == table[i] then return true end end return false end function multipleAnd(obj, table) local allTrue = true for i = 1, #table do if obj ~= table[i] then allTrue = false end end return allTrue end if multipleOr(gamePhase, {1, 2, 3, 4, 5}) then --do something end if multipleOr(event.phase, {"began", "moved", "ended"}) then --do something end

Edit: Just realised that I actually made an “and” statement function. I’ve edited it to be an or statement (and included the and function separately.

Thanks for the suggestion Alan!

Roy.

See my edit, I made the wrong function the first time.

You do not have to even use table - Lua function can have variable number of arguments so you would even do function like OrFunc(compared, …) [dots literally]

What you have written is the only way

For some reason my post was incomplete,

My question was, is it possible to do something like this instead to shorten the code:

[lua]

if (gamePhase==1,2,3,4,5 ) then

– do something

end

[/lua]

 

But from your answer I can tell there is only one way, the long way…

 

Cheers!

Roy.

You could make a function to do it for you?

function multipleOr(obj, table) for i = 1, #table do if obj == table[i] then return true end end return false end function multipleAnd(obj, table) local allTrue = true for i = 1, #table do if obj ~= table[i] then allTrue = false end end return allTrue end if multipleOr(gamePhase, {1, 2, 3, 4, 5}) then --do something end if multipleOr(event.phase, {"began", "moved", "ended"}) then --do something end

Edit: Just realised that I actually made an “and” statement function. I’ve edited it to be an or statement (and included the and function separately.

Thanks for the suggestion Alan!

Roy.

See my edit, I made the wrong function the first time.

You do not have to even use table - Lua function can have variable number of arguments so you would even do function like OrFunc(compared, …) [dots literally]