playMaker

Author Topic: Encode/Decode bit-flags  (Read 2583 times)

Red

  • Hero Member
  • *****
  • Posts: 563
Encode/Decode bit-flags
« on: April 01, 2015, 07:49:38 PM »
Hey there... Two new actions that might help some of you out there if you're looking for a way to manage large sets of data that are yes/no values (such as key-items in one's inventory or maybe a questing system) into bit-flags.

I didn't know what they were officially called but it's basically taking advantage of the 1/0 binary values (1 being true, 0 being false) and converting that into an integer value (and of course, going the other way.)

Note, this IS a beta action set... So, typical disclaimer applies.

How to use:
Tell it how many booleans you want it to handle, assigning those bools in the action to bools in the FSM and also an integer variable that you want it to either run from or store to. Run it.

NOTE: This is important. Since this is beta it's raw and the most important thing here is that the encode and decode need to use the same number of booleans.

There is room in here for some error-checking and redundancy but that might be a bit over my head at this current time. I'll keep nibbling at this going forward but if you use these as recommended there shouldn't be issues.

I've tested with up to 16 bools but since it's using an int as the final storage value, you can make a lot more. (unless it's changed, I believe the resulting int value's max can be something like 2 billion.)

I made this because I wanted a way of storing in the player prefs a simpler value for what weapons the player has unlocked. I only have six but the FSM for that without these was massive and beastly. This cut that down to a much more manageable and easier to read FSM significantly.

EDIT: Performed a stress test... This can handle up to 31 boolean values stored into an integer variable. If you put it at 32, it's at the upper limit of the int's values (since this is, essentially, a power-of-two thing at it's core considering the int-to-bin conversion and the int's max possible value.)
« Last Edit: April 01, 2015, 10:26:05 PM by Red »

Red

  • Hero Member
  • *****
  • Posts: 563
Re: Encode/Decode bit-flags
« Reply #1 on: April 02, 2015, 01:24:50 PM »
Did some more testing and it MAY be useable for 32 booleans... It isn't returning bad results in the bool values in my testing platform but the integer value that it produces in the encoding process does return -1. This might be a bug though so it might be a better idea to keep the boolean values to encode at 31 or below.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Encode/Decode bit-flags
« Reply #2 on: April 27, 2015, 04:56:01 AM »