The mset() function in PICO-8 is used to set the sprite for a tile on the map. It takes in three arguments: column, row, and a sprite number.

mset( column, row, sprite )
mset "map set"
column number of tiles from the left (8 pixels in 1 tile)
row number of tiles from the top (8 pixels in 1 tile)
sprite sprite number

If you open the map editor, and hover your mouse over the map canvas, you can see the column and row number as x and y values in the bottom left corner. 

Changing Map Sprite from Player Interaction

Often times, you will be drawing sprites to the screen using pixel coordinates but, just like mget(), this function uses tiles instead of pixels. So to use this function alongside sprites that you draw on your screen using code instead of using the map, you will probably need to convert pixel coordinates over to tile coordinates and that can be as simple as by dividing by 8. 

In this example, we have a player drawn at (x,y) in pixels, standing next to a flower sprite that is drawn on the map.

Let's say we want to allow the player to pluck the flower. We can interact with the map by changing the flower sprite on the map to a flowerless sprite.

player_column = player_x / 8
player_row = player_y / 8
flower_column = 4
flower_row = 5
flower = 20     --sprite #
flowerless = 21 --sprite #

if player_column+1 == flower_column
and player_row == flower_row 
and btnp(4) then
	mset( flower_column, flower_row, flowerless )

This is useful for changing certain things on the map, when something happens. In the code above, we check if the player is standing next to the flower, and if they press button #4, then set the flower sprite to be flowerless and add a flower to the player's inventory count. This will change the sprite on the map and make the player look like they are able to pluck the flower, allowing sprites you draw over the map to interact with the map.


10 Apr 2023