PICO-8 Wiki
mset( celx, cely, snum )
Sets a cell on the map to a new sprite number.
The column (x) coordinate of the cell.

The row (y) coordinate of the cell.

The new sprite number to store.

The mset() function modifies the map data.

A simple use of mset() is to place or remove objects on the map, such as a treasure that the player can pick up. This allows for the level designer to set the initial locations of objects.

In a more sophisticated version of this technique, the program can scan the map for objects with mget(), store their locations in a table, then erase them from the map data and draw them separately. This may make the objects easier to animate or participate in physics simulation.

Advanced techniques that use mset() include generating levels procedurally, or storing very large maps as compressed data and decompressing it into the map region as needed. In both cases, once the maps are written to memory, the game engine can use map() to draw the level to the screen.

Setting values on the map changes the data in memory, but does not change the cartridge. A program can restore the original data from the cartridge with reload(), and can save the modified data to the cart with cstore(). See Memory for information about the memory addresses to use.


gems = {}

-- scan a region of the map for gems
-- (sprite 16), store their locations
-- in the gems table, then delete them
-- from the map data
for x=0,15 do
  for y=0,15 do
    if mget(x, y) == 16 then
      add(gems, {x, y})
      mset(x, y, 0)

See also[]