mvbg

VM_SET

VM_SET is a fundamental GBVM instruction used to assign the value of one variable to another variable.

Purpose: This instruction is the direct equivalent of a variable assignment in most programming languages. It allows you to copy the current content of a source variable into a destination variable. This is essential for:

VM_SET operates on the values stored in the variables, not their memory addresses. The size of the value (8-bit or 16-bit) is typically inferred from the declaration of the destination variable (IDXA).

Syntax:

VM_SET IDXA, IDXB

Usage Example: Updating Player Position for Display

Imagine your game has internal variables VAR_PLAYER_WORLD_X and VAR_PLAYER_WORLD_Y that track the player’s precise position in the game world. For display purposes, you might have VAR_DISPLAY_X and VAR_DISPLAY_Y that need to be updated to reflect the player’s current position.

; In your game loop, after player movement is processed:

; Player world position variables
VAR_PLAYER_WORLD_X:
  .R_INT16 0
VAR_PLAYER_WORLD_Y:
  .R_INT16 0

; Display position variables
VAR_DISPLAY_X:
  .R_INT16 0
VAR_DISPLAY_Y:
  .R_INT16 0

; ... code that updates VAR_PLAYER_WORLD_X and VAR_PLAYER_WORLD_Y ...

; Update display variables with current player world position
VM_SET VAR_DISPLAY_X, VAR_PLAYER_WORLD_X
VM_SET VAR_DISPLAY_Y, VAR_PLAYER_WORLD_Y

; Now, VAR_DISPLAY_X and VAR_DISPLAY_Y hold the same values as
; VAR_PLAYER_WORLD_X and VAR_PLAYER_WORLD_Y, respectively.

; You can then use VAR_DISPLAY_X and VAR_DISPLAY_Y for rendering the player sprite.
VM_ACTOR_SET_POS PLAYER_ACTOR, VAR_DISPLAY_X, VAR_DISPLAY_Y

; ... continue game loop ...

In this example, VM_SET VAR_DISPLAY_X, VAR_PLAYER_WORLD_X copies the value from VAR_PLAYER_WORLD_X into VAR_DISPLAY_X. Similarly, VM_SET VAR_DISPLAY_Y, VAR_PLAYER_WORLD_Y copies the Y coordinate. This ensures that the display variables are always synchronized with the actual player position, allowing the rendering system to use these updated values.

Analogy to other programming languages: This is directly analogous to a variable assignment statement in most programming languages:

It’s the fundamental operation for copying data between variables, enabling the flow of information throughout your game’s logic.