Background tile swapping is a new built-in feature of GB Studio 3, but it is not yet easily accessible as of this guide. Background tile swapping can be useful for multiple things, such as animation and HUDs if you do not want to use sprites/palette swapping for them.
Firstly, you must determine whether you need to swap every tile of a certain type (like water animation) or just a certain tile on the background (such as placing a flower on a specific tile of grass). This guide will cover the former type of tile swapping.
Now, you have to make a background with all the tiles that you want to replace with. Below is an example of such for a flower animation.
Make sure to name the background image as first alphabetically compared to the other background names. You can do this by adding a !
before the file name.
Add a scene into your GB Studio project with this background. You may need to make this a logo scene. It is okay if the player can not reach this scene, but the scene has to be here for it to be loaded into the game.
Next, select the scene where you want to swap tiles. Add the event named "GBVM Script." There are two GBVM commands that you can use for this: VM_REPLACE_TILE_XY
and VM_REPLACE_TILE
. VM_REPLACE_TILE_XY
is easier to use, so inside the text box, type the commands
VM_PUSH_CONST START_IDX
VM_REPLACE_TILE_XY X, Y, ___bank_tileset_0, _tileset_0, .ARG0
VM_POP 1
Note: in GB Studio versions after 3.0.3, add a reference to the tiles background, then click on it to copy the required text, then paste it in place of
___bank_tileset_0, _tileset_0
.
where START_IDX
is the tile index you want to replace with from the tiles background you created before (e.g. 0
for the first tile, 1
for the second, etc.) and X
and Y
denote the position of the tile you want to replace on this scene (you can hover over a specific tile to see its coordinates). You likely will not need to change the numbers for ___bank_tileset_0
and _tileset_0
unless you have more than 256 tiles to replace with.
Alternatively, you can do something like
VM_REPLACE_TILE_XY X, Y, ___bank_tileset_0, _tileset_0, VAR_MYVAR
after setting the global variable "MyVar" to the tile you want to replace with.
For example, here is my code for swapping the flower tile at X: 4, Y: 11 with the left-shifted flower tile.
VM_PUSH_CONST 1
VM_REPLACE_TILE_XY 4, 11, ___bank_tileset_0, _tileset_0, .ARG0
VM_POP 1
Combined with other scripts, here is the result:
this is nice ! i have a few question about it :
Thanks :)