Tutorial: PICO-8 flags


Introduction

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.

Uses

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

Example 1: Jelpi

The PICO8 Jelpi demo uses flags for various things:

  • Flag 0: indicates the sprite is part of the scenario 
  • Flag 1: indicates that the sprite is solid.
  • Flag 3: indicates that the sprite is a monster
  • Flag 4: is active if the block can be destroyed. Set it to true for all the ground sprites and you’ll be able to destroy the level!

  • Flag 5: indicates if the sprite is an item that can be picked

Flags 2, 6 and 7 seem to be unused.

Example 2: Toy Box Pipe

In this game I used all 8 flags for one thing or another 

  • Flag 0: indicates the tile is solid. The player cannot be on top of it. The walls and the fire are have it activated.
  • Flag 1: indicates the tile can be pushed or pulled. It is used for the pipes.
  • Flag 2: is active for the tiles that are pipes. It is used for the water flow computation.
  • Flags 3-6: represent the sides of a pipe that can be connected. It is explained in detail below.
  • Flag 7: indicates the tile activates the flow of water. Only the sprite that seem to me like a valve uses it.

The flags 3 to 6 indicate which sides connect together:

  • Flag 3: up
  • Flag 4: right
  • Flag 5: down
  • Flag 6: left

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!

Farewell

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!

Get Toy Box Pipe

Download NowName your own price

Comments

Log in with itch.io to leave a comment.

Thank you for writing this tutorial, it helped a lot.

Thanks! Glad you found it useful!