VM_GET_TILE_XY
is a GBVM instruction used to retrieve the tile ID of a background tile at a specified X and Y position on the game map.
Purpose:
Game Boy games often use tile-based maps for their environments. Each tile has a unique ID that corresponds to a specific graphic. VM_GET_TILE_XY
is essential for:
This instruction allows your script to query the game world’s background layer and get information about its composition at a granular level.
Syntax:
VM_GET_TILE_XY TILE_IDX, X_IDX, Y_IDX
TILE_IDX
: The target variable (a GBVM variable) that will receive the retrieved tile ID. This variable will hold the numerical identifier of the tile at the specified coordinates.X_IDX
: The X-coordinate (horizontal position) of the background tile you want to query. This is typically in tile units (e.g., 0-31 for a 32x32 tile map).Y_IDX
: The Y-coordinate (vertical position) of the background tile you want to query. This is typically in tile units.Usage Example: Checking if Player is Standing on a Water Tile
Imagine your game has water tiles (e.g., tile ID 5) that slow down the player. You can use VM_GET_TILE_XY
to check the tile beneath the player and adjust their speed accordingly.
; In your player movement script or update loop:
; Assume PLAYER_ACTOR_ID is the actor ID for the player
PLAYER_ACTOR_ID:
.R_INT8 0 ; Example Actor ID
; Variables to store player's tile coordinates and the tile ID
VAR_PLAYER_TILE_X:
.R_INT8 0
VAR_PLAYER_TILE_Y:
.R_INT8 0
VAR_CURRENT_TILE_ID:
.R_INT8 0
; Define the tile ID for water
TILE_ID_WATER:
.R_INT8 5 ; Example: Tile ID 5 represents water
; Define player speeds
SPEED_NORMAL:
.R_INT8 1
SPEED_SLOW:
.R_INT8 0 ; Slower speed (e.g., half of normal)
; Get player's current pixel position
VM_ACTOR_GET_POS PLAYER_ACTOR_ID, VAR_PLAYER_PIXEL_X, VAR_PLAYER_PIXEL_Y
; Convert pixel coordinates to tile coordinates (assuming 8x8 pixel tiles)
; Divide by 8 (or shift right by 3) to get tile X and Y
VM_RPN
.R_REF_MEM .R_INT16, VAR_PLAYER_PIXEL_X
.R_INT16 3 ; Shift right by 3 for division by 8
.R_OPERATOR >>
.R_INT8
VM_STOP
VM_SET_INT8 VAR_PLAYER_TILE_X, 0
VM_RPN
.R_REF_MEM .R_INT16, VAR_PLAYER_PIXEL_Y
.R_INT16 3
.R_OPERATOR >>
.R_INT8
VM_STOP
VM_SET_INT8 VAR_PLAYER_TILE_Y, 0
; Get the tile ID at the player's current tile position
VM_GET_TILE_XY VAR_CURRENT_TILE_ID, VAR_PLAYER_TILE_X, VAR_PLAYER_TILE_Y
; Check if the current tile is a water tile
VM_IF_CONST .EQ, VAR_CURRENT_TILE_ID, TILE_ID_WATER, PLAYER_IN_WATER, 0
; If not water, set normal speed
VM_ACTOR_SET_MOVE_SPEED PLAYER_ACTOR_ID, SPEED_NORMAL
VM_JUMP END_TILE_CHECK
PLAYER_IN_WATER:
; If in water, set slow speed
VM_ACTOR_SET_MOVE_SPEED PLAYER_ACTOR_ID, SPEED_SLOW
END_TILE_CHECK:
; ... continue player movement logic ...
In this example, VM_GET_TILE_XY VAR_CURRENT_TILE_ID, VAR_PLAYER_TILE_X, VAR_PLAYER_TILE_Y
retrieves the ID of the background tile the player is currently standing on. This ID is then compared to TILE_ID_WATER
. If they match, the player’s movement speed is reduced, creating a gameplay effect based on the terrain.
Analogy to other programming languages/game engines: This is analogous to querying a tilemap or a 2D array representing your game world:
TileBase tile = tilemap.GetTile(new Vector3Int(x, y, 0));
int tile_id = get_cell(x, y);
tile_id = game_map[y][x]
It provides a way to read the static background data of your game world, which is crucial for environmental interactions and map-based logic.