fset = "flag set"

 fset( sprite, [flag], value )
sprite the sprite number of the sprite you want a flag checked.
flag (optional) the flag number (0-7) of the flag you want checked.
value true or false to turn the flag on or off. Or a bitfield.

This function will set the sprite's flag(s) on or off. If you provide a flag argument then it will set the value of that specific flag to the value.

If you do not provide a flag argument, then the fset function will apply the value to all flags.


In this example you can see that we manually turned on flag 0 for the bomb sprite. So the first time we get the flag 0, it returns as true. Then we set it false using fset, and get it again and find that it is now false, turned off. So we can change which flags are on for sprites in code while the game is running.

-- bomb= sprite 1, flag 0 on
print( fget(1,0) ) --true

fset(1,0,false) --set flag 0 to false 
print( fget(1,0) ) --false

You can easily turn all flags on or off for a single sprite like this:

fset( 1,true )  --all on
fset( 1,false ) --all off

Setting Multiple Flags

You can set the flags manually in the sprite editor or with this function fset. You can have multiple flags turned on for a single sprite. Here we have flag 0 turned on for the bomb sprite and multiple flags turned on for the man sprite in the sprite editor. We can do the same thing in code like this:

-- bomb= sprite 1, flag 0
-- man = sprite 2, flag 1,3,5,7 

fset( 1,0,true ) --flag 0, on

fset( 2,1,true ) --flag 1, on
fset( 2,3,true ) --flag 3, on
fset( 2,5,true ) --flag 5, on
fset( 2,7,true ) --flag 7, on


To avoid many calls to the same function, there is an easier way to set multiple flags. To do this, we need to understand the bitfield number that we can use to represent multiple boolean states (flags as true or false).

Bitfields represent exactly which flags are turned on and they will be a number between 0 (all flags turned off) and 255 (all flags turned on).

Here is a table of flag numbers and their corresponding bitfield values.

flag # bitfield
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128

By adding the bitfield value of the flags that you want turned on, you will get the unique number that is returned by a sprite that has those specific flags turned on. In our example, the bomb sprite has only flag 0 turned on so the bitfield value is simply 1.

fset( 1, 1, true ) --flag 0, true

And the man sprite we want multiple flags turned on, specifically flags 1, 3, 5 and 7. If you refer to the table above, we can add up the bitfield values of each of those flags and get the total bitfield value that represents those exact flags turned on, which is 170 because:

flag #s bitfields total
1, 3, 5, 7 2 + 8 + 32 + 128 = 170

Now we can show you how you can simplify the multiple calls to the function for each flag, down to a single call using a bitfield value.

--set multiple flags using multiple calls
fset( 2,1,true ) --flag 1, on
fset( 2,3,true ) --flag 3, on
fset( 2,5,true ) --flag 5, on
fset( 2,7,true ) --flag 7, on

--set multiple flags using 1 call
fset( 2,170 ) --flags 1,3,5,7 on

Notice that when we don't specify a flag in the arguments, and instead after the sprite number, we only provide the number 170 which will be taken as the value argument, not the flag argument.


29 Apr 2024