Posted January 04, 2020 by Ximo
#pico8 #tutorial #flags
For our convenience, PICO-8 has 8 flags for each sprite that can be either true or false. You can see the flags in the editor next to the sprite you have selected.
In the UI each flag is represented as a circle. It will be colored if the flag is set to true. The first flag is the flag 0 and has the color red.
In your code you can get the value of a flag using the function fget and passing as argument the sprite number and which flag do you want to retrieve.
In the screenshot above, fget(6, 1) will return true because flag 1 (orange) of the sprite 6 is active. Remember the flags go from 0 to 7.
You can also modify the flag: fset(6, 2, false) will set the third flag of the sprite 6 to false.
Leveraging the sprite flags is very useful and will probably save you from writing a lot of code.
If you are writing something like:
if tile == 1 or tile == 2 or tile == 23 then something() end
You could activate the flag 0 for the tiles 1, 2 and 23 and replace the code with:
if fget(tile, 0) then something() end
The PICO8 Jelpi demo uses flags for various things:
Flags 2, 6 and 7 seem to be unused.
In this game I used all 8 flags for one thing or another
The flags 3 to 6 indicate which sides connect together:
A pipe X that has water will only transfer water to the pipe on its right Y if X has the flag 4 and Y has the flag 6.
The flags are used in the water flow computation:
Currently there are only pipes with 1, 2 o 4 openings. If I wanted to add new pipes with 3 openings I would just need to create the sprites for them and then activate the appropriate flags. No code change needed!
I hope you found useful this tutorial. Please let me know your feedback in the comments section. If there is any other cool trick using flags I'd like to learn about it!