mvbg

VM_SET_CONST

VM_SET_CONST is a GBVM instruction used to assign an immediate (constant) value directly to a specified GBVM variable.

Purpose: This is one of the most common and straightforward ways to set the value of a variable in your GBVM scripts. It is essential for:

Unlike instructions that operate on the stack (like VM_SET_INT16 which takes its value from the stack), VM_SET_CONST takes the value directly as an argument, making it very direct and easy to use for simple assignments.

Syntax:

VM_SET_CONST IDX, VAL

Usage Example: Setting Player Score to Zero at Game Start

Imagine your game needs to reset the player’s score to 0 when a new game begins. The score is stored in VAR_PLAYER_SCORE.

; In your game initialization script:

; Variable to hold the player's score
VAR_PLAYER_SCORE:
  .R_INT16 0 ; Declared as a 16-bit integer

; Set the player's score to 0
VM_SET_CONST VAR_PLAYER_SCORE, 0

; Now, VAR_PLAYER_SCORE contains the value 0.

; Example of using the score later:
VM_LOAD_TEXT_VAR VAR_PLAYER_SCORE
VM_DISPLAY_TEXT ; Will display "0"

; ... rest of game initialization ...

In this example, VM_SET_CONST VAR_PLAYER_SCORE, 0 directly assigns the constant value 0 to the VAR_PLAYER_SCORE variable. This is a clean and efficient way to initialize or reset variables to fixed values.

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

It’s the most fundamental way to put a literal value into a variable.