@espace3d,
Before I start, this will sound like criticism, but I’m trying to be helpful.
The code you’ve presented is very:
-
long - You said this yourself.
-
hard to read - Numeric flags have no meaning to anyone but the author and later you will forget too.
-
error prone - Because of the prior two points, you will likely find yourself making and correcting many errors, which leads to…
- hard to maintain
So, how can we make this better? I think the answer is fairly straight forward, but answer these questions first:
- Are you willing to pay a small performance cost for easy to maintain code?
- Are you willing to learn something new?
- With regards to your ‘flags’, are there more than 32 unique possibilities?
If you answered, “Yes”, to each of the above questions, then you can solve your troubles by learning about bit-masks and using the Lua Bit plugin.
I’m including some code below for you to examine and play with. It is NOT sophisticated can be be made much more so and more powerful. It is simply a starting place. Best of luck. Also you can get the whole sample (with build.settings file) here:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/02/bitsAreBetter.zip
local bitLib = require( "plugin.bit" ) -- Make a table of masks (you could put this in a module and re-use it in multiple files) -- local masks = {} -- You have 32-bit to work with masks.dog = bit.lshift(1, 0) -- Shift left by 0 (creates a unique mask) masks.cat = bit.lshift(1, 1) -- Shift left by 1 (creates a unique mask) masks.small = bit.lshift(1, 2) -- ... masks.medium = bit.lshift(1, 3) -- ... masks.big = bit.lshift(1, 4) -- ... -- .. masks.hairless = bit.lshift(1, 28) -- ... masks.hairy = bit.lshift(1, 29) -- ... masks.dirty = bit.lshift(1, 30) -- ... masks.clean = bit.lshift(1, 31) -- Last possible shift value is 31 (not 32) -- Tip: If you later change the above 'shifts' the code will still work as long as you use -- named masks -- Let's see the masks (will print in random order due to pairs()) for k,v in pairs(masks) do print( k, bit.tohex(v)) end print("\n========================") local animals = { { name = "Buddy", flag = bit.bor( masks.dog, masks.small, masks.clean, masks.hairy ) }, { name = "Gumbo", flag = bit.bor( masks.dog, masks.big, masks.dirty, masks.hairy ) }, { name = "Whiskers", flag = bit.bor( masks.cat, masks.small, masks.clean, masks.hairless ) }, } -- Let's see the animal records for i = 1, #animals do print( animals[i].name, bit.tohex(animals[i].flag) ) end print("\n========================") local function printCleanAnimals( list ) for i = 1, #list do local animal = list[i] local isClean = bit.band( animal.flag, masks.clean ) local isCat = bit.band( animal.flag, masks.cat ) print("----------------------") print( i, isClean, isCat ) print( i, bit.tohex(isClean), bit.tohex(isCat) ) if( isClean ~= 0 ) then if( isCat ~= 0 ) then print( animal.name .. " is a clean cat.\n\n" ) else print( animal.name .. " is a clean dog.\n\n" ) end end end end printCleanAnimals( animals )
Prints this ( I reordered the output manually for legibility):
dog 00000001 cat 00000002 small 00000004 medium 00000008 big 00000010 hairless 10000000 hairy 20000000 dirty 40000000 clean 80000000 ======================== Buddy a0000005 Gumbo 60000011 Whiskers 90000006 ======================== ---------------------- 1 -2147483648 0 1 80000000 00000000 Buddy is a clean dog. ---------------------- 2 0 0 2 00000000 00000000 ---------------------- 3 -2147483648 2 3 80000000 00000002 Whiskers is a clean cat.