mvbg

VM_INPUT_GET

VM_INPUT_GET is a GBVM instruction used to read the current state of the Game Boy’s joypad buttons and store the result into a specified variable.

Purpose: This instruction is fundamental for any interactive game, as it allows your script to detect player input. It is essential for:

When VM_INPUT_GET is called, it captures the state of the specified joypad’s buttons at that exact moment. The result is typically a bitmask, where each bit corresponds to a specific button (e.g., A, B, Start, Select, D-pad directions).

Syntax:

VM_INPUT_GET IDX, JOYID

Usage Example: Player Character Movement Based on D-Pad Input

Imagine your player character moves based on D-pad input. You would use VM_INPUT_GET to read the joypad state and then use conditional logic to move the player.

; In your main game loop or player input handling routine:

; Variable to store the current joypad input state
VAR_JOYPAD_STATE:
  .R_INT8 0

; Assume PLAYER_ACTOR_ID is the actor ID for the player
PLAYER_ACTOR_ID:
  .R_INT8 0

; Input constants (these would be defined globally, often as bitmasks)
.INPUT_UP:
  .R_INT8 1
.INPUT_DOWN:
  .R_INT8 2
.INPUT_LEFT:
  .R_INT8 4
.INPUT_RIGHT:
  .R_INT8 8
.INPUT_A:
  .R_INT8 16
.INPUT_B:
  .R_INT8 32

GAME_LOOP:
  ; Get the current state of Joypad 0
  VM_INPUT_GET VAR_JOYPAD_STATE, .JOY0

  ; Check for Up input
  VM_RPN
    .R_REF_MEM .R_INT8, VAR_JOYPAD_STATE
    .R_REF_MEM .R_INT8, .INPUT_UP
    .R_OPERATOR &
    .R_REF_MEM .R_INT8, .INPUT_UP
    .R_OPERATOR ==
    .R_INT8
  VM_STOP
  VM_IF_CONST .NE, 0, 0, MOVE_PLAYER_UP, 0 ; If result is true (not 0), jump

  ; Check for Down input
  VM_RPN
    .R_REF_MEM .R_INT8, VAR_JOYPAD_STATE
    .R_REF_MEM .R_INT8, .INPUT_DOWN
    .R_OPERATOR &
    .R_REF_MEM .R_INT8, .INPUT_DOWN
    .R_OPERATOR ==
    .R_INT8
  VM_STOP
  VM_IF_CONST .NE, 0, 0, MOVE_PLAYER_DOWN, 0

  ; ... similar checks for LEFT, RIGHT, A, B buttons ...

  VM_JUMP GAME_LOOP ; Continue game loop

MOVE_PLAYER_UP:
  VM_ACTOR_SET_DIR PLAYER_ACTOR_ID, .DIR_UP
  VM_ACTOR_MOVE_TO PLAYER_ACTOR_ID, VAR_PLAYER_X, VAR_PLAYER_Y - 8, 0 ; Move up 8 pixels
  VM_JUMP GAME_LOOP

MOVE_PLAYER_DOWN:
  VM_ACTOR_SET_DIR PLAYER_ACTOR_ID, .DIR_DOWN
  VM_ACTOR_MOVE_TO PLAYER_ACTOR_ID, VAR_PLAYER_X, VAR_PLAYER_Y + 8, 0 ; Move down 8 pixels
  VM_JUMP GAME_LOOP

; ... other movement/action routines ...

In this example, VM_INPUT_GET VAR_JOYPAD_STATE, .JOY0 reads the current state of the first joypad. The subsequent RPN blocks use bitwise AND (&) to check if specific buttons (like .INPUT_UP) are pressed. If a button is pressed, the player’s actor is moved accordingly. This forms the core of player control in many Game Boy games.

Analogy to other programming languages/game engines: This is analogous to polling input devices or checking the state of specific keys/buttons:

It provides the direct means to read player input, which is fundamental for making your game interactive and responsive to player commands.