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
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:
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
This is a very old thread, but I’m leaving this here for those who are still learning in case they stumble upon the same situation:
Bitmasks or bitwise operations are very useful when you have a lot of bools that can be set at the same time.
For example, if you have to deal with save data checks. Let’s say you have a bunch of flags (bools) that you need to load each time the player boots up the game. You could add them all one by one and then remember them or place them in a list. But going through them and performing operations would be rather slow since you’d have to go through the whole list every time to check a single flag.
A bitwise mask is in layman’s terms squeezing all your bools into a single number. All those 1’s and 0’s (True and False respectively) get sandwiched into a single number. What for you might be 3 for the computer is “011” and 4 would be “100”.
Think about the power mains in your house, you’ve got a bunch of switches all lined up together and they are all “up” (“111111”). And when one goes down it turns to 0 (“1110111”). So it is very easily readable what’s on and off. Same thing goes for the computer, and that’s why they are so helpful.
The other option you have is a “dictionary”. Unreal calls them Maps, a structure (or struct) is basically a Map that is saved as an asset.
It stores values in a list where you can define what kind of value goes in each place, and all values are associated with a “key” which is a name that identifies the slot. Dictionaries are basically lists since you can’t have two identical keys in them.
Going through all their values is slow, so they are useful when you only need a single value in specific or store different kinds of values in the same list (booleans, integers, and strings in the same list as an example). You can fetch any value you want, change it and store it again.
Hope this is useful for anyone that stumbles upon this post once again.
A simple work around is to create a Macro that sets all the bools inside your player to true and a 2nd Macro to set them to false. Obviously you’ll need to figure out which ones will need to be set every time and then the odd one that doesnt fit you can add to the end after the macro
But at the end of the day you could drag out your macro as one node which will set all the bools in it to T/F. And if you add variables later you can add it to the macro and then it’ll add it to all the previous macros you’ve already implemented into your project