Reputation: 23
I like to make games in Roblox and I code in lua. Well coding games I find myself asking if a value equals another value a lot and it can make really long lines of code and can be quite repetitive.
For example:
if x.ClassName == 'Shirt' or
x.ClassName == 'Accessory' or
x.ClassName == 'Pants' or
x.ClassName == 'CharacterMesh' then
-- do thing
end
doing that not only can be very messy but is also just asking the same thing over and over again slightly modified.
I was wondering if there was a way in lua to ask if a value equals multiple diferant separate values
For example:
if x.ClassName == ( 'Shirt' or 'Accessory' or 'Pants' or 'CharacterMesh' ) then
-- do thing
end
Upvotes: 2
Views: 3719
Reputation: 72312
You could do something like this:
if string.match('/Shirt/Accessory/Pants/CharacterMesh/', '/'..x.ClassName..'/') then ... end
but it's unlikely to be any faster than the if chain you have.
Upvotes: 0
Reputation: 11576
There's no native way to do this in Lua but you can implement a helper function set
to achieve that.
function set(...)
local ret = {}
for _,k in ipairs({...}) do ret[k] = true end
return ret
end
local classname = 'Shirt'
if set('Shirt', 'Accessory', 'Pants', 'CharacterMesh')[classname] then
print('true')
end
Upvotes: 4