Better Way to Handle Booleans?

Hey guys,

I wonder if you can help me with something, or more specifically give me a tips on handling my booleans.
Often times, particularly when activating or deactivating player abilities/inputs I am setting multiple booleans. Such as setting whether I can use my movement inputs, whether I can attack, cast other abilities etc etc.
Once I start adding more abilities and more inputs the character can use I find myself adding more and more booleans that need to be set or run a check on. 4-5 set booleans I can foresee turning into 9-10 once I start adding more abilities.

Is there a better way of handling such a slew of booleans or is having a massive amount of booleans normal?

Yes, you can use bit flags (also called bit masks), and store booleans as bits of integer, this concept use the fact that numbers in computer is stored in bits and each can be set either 0 or 1 and integers use in blueprint are stored in 32-bits so you can store 32 on and off states in single number, 32 different Boolean states in single integer varable. it also allows to do some more efficient ways of managing them with bitwise operations

You can use this concept in blueprint using enumeration with bitmask on, make enumeration click arrow on first category and there should be check box for bitmask, now make integer varable in blueprint and in it;s properties turn on Bitmask and select bitmask enum that you made and this will turn integer in to packed Boolean type holding different options. I not sure how comftible it is to use use in blueprint, but you can always use & (AND) operation to check turned on flags. Here is official doc

https://docs.unrealengine.com/en-us/Engine/Blueprints/UserGuide/Variables/Bitmask

1 Like

I will look into this and see if I can integrate it into what I am doing at the moment.
Thank you :slight_smile:

The more I look into this the more I think this is not quite what I am looking for (Unless I am misunderstanding).

So more explanation into what I want to happen:
For example when ending a cast of an ability I have a bunch of set nodes

Set no longer casting ability
Set no longer cancelling ability (when releasing the key this is set to true to queue cancelling ability after the animation)
Set can cast the ability
Set can use movement input
Set can melee attack (locked out of melee attacking and moving during this ability)

Ideally what I would want is to at least store the ability specific booleans in a way where I can toggle them to be true or false with one node instead of using 5 separate set nodes.
From the documentation that you gave it seems as though they would be stored in a easier to read way but I would still need to use 5 set nodes to change the integer values

Depends on how you are using them. There are a lot of cases where you can keep booleans behind the curtains of other graphs so you don’t have to sort through them. Other times you can do away with storing booleans and use Selector-nodes instead. You could also look into using Enumerators to cut out certain player actions (think locking certain abilities behind certain move-sets).

No you don’t need to if you do the math, thats the whole point of bit masks, you dont even need to use enums for this since this is simple bit math technique commonly used in programing. Each bit alone creates power of 2 number so for example in case of 8 bit:

Bin         Hex    Dec
0b00000001 = 0x01 = 1
0b00000010 = 0x02 = 2
0b00000100 = 0x04 = 4
0b00001000 = 0x08 = 8
0b00010000 = 0x10 = 16
0b00100000 = 0x20 = 32
0b01000000 = 0x40 = 64
0b10000000 = 0x80 = 128

Knowing that you know that if you gonna add those power of 2 number you gonna activate specific bits:

so for example 1+2+16+64 = 83 = 0b01010011

and you can disable bits by subtracting power of 2 numbers, so for example to disable 2nd bit we substract 2:

83 - 2 = 81 =  0b01010001

and with that you can set those bits with just single integer set, and manipulate massive maps of boolean bits with few math nodes.

To read bit you use AND gate (&) bit operator, you can put it against more then one bit and if value is higher then 0 then that means one of those bits you put against the bit mask number is set so for example we want to check if 1st or 4th is set:

0b00001001 & 0b01010011 = 9 & 83 = 0b00000001 = 1 // more then 0 that means either bit 4 and 1 is on

Other then that you might consider using structures, it will let you set multiple bools by breaking the structure pin (right click and there option for that) in set node

Hmmm, I’ll have a look into structures. Again, I’m not super good at programming and it seems to me as though bitmasks would be used for very specific things.
Structures seem to be exactly what I was imagining, thank you :slight_smile: