VM_REPLACE_TILE_XY
is a GBVM instruction used to replace a single background tile at a specific X and Y position on the screen with a new tile from a tileset.
Purpose: This instruction allows for dynamic changes to the game’s background at a very granular level. It is essential for:
When VM_REPLACE_TILE_XY
is called, the tile at the specified X
and Y
coordinates on the background layer is instantly updated with the new tile graphic. This change is typically permanent until another VM_REPLACE_TILE_XY
or a full map load occurs.
Syntax:
VM_REPLACE_TILE_XY X, Y, TILEDATA_BANK, TILEDATA, START_IDX
X
: The X-coordinate (horizontal position) of the background tile to be replaced. This is in tile units (e.g., 0-19 for a 20-tile wide screen).Y
: The Y-coordinate (vertical position) of the background tile to be replaced. This is in tile units (e.g., 0-17 for an 18-tile high screen).TILEDATA_BANK
: The memory bank number where the new tile’s data (TILEDATA
) is located. This is crucial for accessing graphical data that might be in a different ROM bank.TILEDATA
: The address (label) of the tileset containing the new tile. This points to the raw graphical data for the tiles.START_IDX
: The tile ID of the new tile within the TILEDATA
tileset. This specifies which specific tile from the provided tileset should be used as the replacement.Usage Example: Breaking a Destructible Block
Imagine a game where the player can hit certain blocks to break them, changing their appearance from a solid block to a broken one.
; In your script, when a destructible block is hit:
; Define the coordinates of the block to be broken (in tile units)
BLOCK_X_COORD:
.R_INT8 5 ; Example: Tile column 5
BLOCK_Y_COORD:
.R_INT8 8 ; Example: Tile row 8
; Define the new tile data for the broken block
TILESET_BROKEN_BLOCK_BANK:
.R_INT8 BANK(TILESET_BROKEN_BLOCK)
TILESET_BROKEN_BLOCK:
; ... raw tile data for a broken block graphic ...
TILE_ID_BROKEN_BLOCK:
.R_INT8 0 ; Assuming the broken block graphic is the first tile in its tileset
; ... code for detecting player hitting the block at BLOCK_X_COORD, BLOCK_Y_COORD ...
PLAYER_HITS_BLOCK:
; Replace the solid block tile with the broken block tile
VM_REPLACE_TILE_XY BLOCK_X_COORD, BLOCK_Y_COORD, TILESET_BROKEN_BLOCK_BANK, TILESET_BROKEN_BLOCK, TILE_ID_BROKEN_BLOCK
VM_SFX_PLAY SFX_BLOCK_BREAK ; Play a sound effect
VM_IDLE 10 ; Short pause
; ... potentially spawn particles or drop items ...
VM_RET
SFX_BLOCK_BREAK:
; ... sound effect data ...
In this example, when the player hits a block at (BLOCK_X_COORD, BLOCK_Y_COORD)
, VM_REPLACE_TILE_XY
is called. This instruction takes the coordinates of the block, the bank and address of the TILESET_BROKEN_BLOCK
, and the TILE_ID_BROKEN_BLOCK
(which is 0 in this case, meaning the first tile in that tileset). The tile at (5, 8)
on the background layer will instantly change its appearance to the broken block graphic, providing immediate visual feedback to the player.
Analogy to other programming languages/game engines: This is analogous to updating a single cell in a tilemap in modern game engines:
tilemap.SetTile(new Vector3Int(x, y, 0), newTile);
set_cell(x, y, tile_id);
It provides a direct and efficient way to modify the static background of your Game Boy game, enabling dynamic and interactive environments.