Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Boolean array/flaglist  (Read 1356 times)

0 Members and 1 Guest are viewing this topic.
Boolean array/flaglist
« on: May 03, 2010, 09:21:04 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Okay I don't know why I put this up, because it actually is way to simple to make an elaborate code example for it. But hell. What I made were actually three scripts/functions that would allow you to use a single numerical variable as an array with 53 boolean elements.

What do these scripts do and how do they work?
All the scripts have the flaglist as a parameter and the index of the bit that needs to be changed. The first script is queryFlag. This script only looks at whether the bit is set 1 (true) or 0 (false). The second script is setFlag. This script sets the bit at the given index in the flaglist to 1. The final script unsetFlag does the same, but instead it sets the bit to 0. Both setFlag and unsetFlag return the new value of the flaglist if the bit is changed or the old value of the flaglist.

Why setFlag and unsetFlag?
Actually, it is not necessary to have two different scripts because the line of code that sets the bit is also used to unset the bit. What makes the two scripts different is that one first checks if the bit is set and the other checks if it is unset before changing the bit. I did this to make it more dummy proof. Now you explicitly have to set and unset the bit to change the value.

Does queryFlag return true and false?
It all depends on how you see true and false. Gamemaker does not really have a datatype boolean. When the bit is set to 0 all the bits in the return value are set to 0. Thus 0 would be a return for false. When the requested bit is set to 1, only the bit at that position will be 1 in the return value. However the value of the return is equal to 2 to the power of the index.

How are you doing it?
I use bitwise operators. Before changing or checking the requested bit a temp value is created where the first bit is set to 1. Then the bit is left shifted to the correct position of the index. For checking the bitwise AND operator is used. If the bit was unset the result would be a lot of zeros, if it is set the result will have a single 1 at the requested index. Changing the bit is done by using the XOR operator. This will change the bit at the index from 1 to 0 and 0 to 1 and leaves all the other bits unchanged.

Thus I only need to call the scripts?
For querying the flag yes, only a call to the script is enough. For changing the bit only calling the script is not enough. Number parameters of scripts are passed by value and not by reference. Even if the parameter value would be changed it does not change the original flaglist. A assignment statement is needed to actually change the flaglist, like this
Code: [Select]
flaglist = setFlag(flaglist, index);

Why is the array 53 elements long
That is because I chose to use the number datatype instead of a string. Gamemaker's number datatype uses the binary64 standard or better known as a Double-precision floating point value. This datatype has 64 bits. The first 52 (+ 1 hidden) bits are the significand bits and thus the usable ones. The next 11 bits are the exponent bits, and changing these bits automatically also changes the significand bits. The final bit is the sign bit, making it a positive or negative value. Therefore of the 64 bits only the first 53 are usable.

Does this help me in any way make my game better?
It all depends on how you have organized your engine and how you have set up you game. The memory saving is pretty meaningless, because it is for the PC that has a shitload of memory. Also compared to the rest that needs to be stored it is relatively small. And something like
Code: [Select]
has_Opened_Chest_X_in_Area_Y = true;
is probably a lot more clearer what is set than setting an number in a list. But you can get swamped in the shitload of variables. Using these scripts however greatly diminishes the number variables used and can give a lot more order to your progress variables. In addition you can set up more generic objects, which do not need specialized code.

Ah well, the three scripts can be directly copied to any project, because they don't have any dependencies.
« Last Edit: May 03, 2010, 09:23:05 pm by Niek »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.049 seconds with 39 queries.

anything