GMS 2 [SOLVED] Bitwise Setting and Clearing flags


Hi guys,

So I've just started getting into using bitwise operations and I'm just experimenting with a grid-based movement system. There are a few things I want to clear up:

1. Defining the bits - I've got them set as macros:
#macro COMBATGRID 64 //grid size
#macro FORBID 1 //can't go here
#macro OCCUPIED 2 //if clear, player/AI can go here
#macro ATTRITION 4 //attrition zones
Not sure if I'm on the right track if I'm being honest...

2. Setting flags in the play area
Following the bits, these are the values I have. I figured out the values I need
Empty play space = 0
Occupied play space = 2
Empty Attrition space = 4
Occupied Attrition space = 6
for (i=_roomH-1;i>=0;i--)
        //Initialise the grid
        Space[j,i] = FORBID; //set areas player/enemy can go - but we might still spawn things there
        if (i>1 && i<5 && j>1 && j<8)
            Space[@j,i] &= OCCUPIED; //this is the actual play space
        if (i>1 && i<5 && j>4 && j<8)
            Space[@j,i] |= ATTRITION; <---Here!

Again, not sure if I'm doing things right here

3. Setting and clearing flags when the player moves in the grid space
Here I set the OCCUPIED flag by OR-ing the x,y position
cCombat.Space[@x div COMBATGRID,y div COMBATGRID] |= OCCUPIED;

Here is where I have problems - clearing and setting flags at run-time
if (cCombat.Space[@TargetX,_y]==OCCUPIED || cCombat.Space[@TargetX,_y]==FORBID)exit;
        cCombat.Space[@CurrentX,_y] = !OCCUPIED; <----HERE! Am I supposed to use ~ or & here?
        x = TargetX * COMBATGRID;
        cCombat.Space[@TargetX,_y] |= OCCUPIED;
So here's the full problem:
I want to set a specific space to be an attrition zone (where the player will take damage over time). I've been able to do that in the play area create event though.
However, during run-time, when the player moves, I want to clear the OCCUPIED flag whilst leaving the ATTRITION flag alone. I've tried NOT but that doesn't seem to work (as in !OCCUPIED ~ATTRITION), I've also tried AND (as in &=OCCUPIED ~ATTRITION) but I don't get anywhere with it.

Quite lost, to be honest - any help is appreciated.



ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
To reset a flag, do
variable &= ~flag;
~flag bitwise inverts a value (~...0010 == ...1101), so AND-ing that afterwards will reset the flag bits.