mvbg

VM_INPUT_ATTACH

VM_INPUT_ATTACH is a GBVM instruction used to associate a prepared script (from a VM_CONTEXT_PREPARE slot) with a specific input event, so that the script executes when that input is detected.

Purpose: This instruction is fundamental for handling player input and triggering game events in response to button presses. It allows you to:

When VM_INPUT_ATTACH is called, the GBVM creates a link between the specified MASK (representing an input button or combination) and the script located in the SLOT. Whenever the input corresponding to the MASK is detected, the script in the SLOT will be executed.

Syntax:

VM_INPUT_ATTACH MASK, SLOT

Usage Example: Attaching Dialogue to the A Button

Imagine you want the player to initiate dialogue with an NPC by pressing the A button when near them. You would prepare a dialogue script and then attach it to the A button input.

; In your game initialization or scene setup:

; Define the dialogue script
NPC_DIALOGUE_SCRIPT:
  VM_LOAD_TEXT TEXT_NPC_GREETING
  VM_DISPLAY_TEXT
  VM_INPUT_WAIT
  VM_RET

TEXT_NPC_GREETING:
  .TEXT "Hello, adventurer!"
  .TEXT_END

; Prepare the dialogue script in slot 1
VM_CONTEXT_PREPARE 1, BANK(NPC_DIALOGUE_SCRIPT), NPC_DIALOGUE_SCRIPT

; Attach slot 1 to the A button input
VM_INPUT_ATTACH .INPUT_A, 1

; ... rest of game logic ...

; Input constants (these would be defined globally)
.INPUT_A:
  .R_INT8 1 ; Example value for A button

In this example:

  1. NPC_DIALOGUE_SCRIPT is defined, which handles displaying text and waiting for input.
  2. VM_CONTEXT_PREPARE 1, BANK(NPC_DIALOGUE_SCRIPT), NPC_DIALOGUE_SCRIPT registers NPC_DIALOGUE_SCRIPT with slot 1.
  3. VM_INPUT_ATTACH .INPUT_A, 1 then links the A button input to slot 1. Now, whenever the A button is pressed, the NPC_DIALOGUE_SCRIPT will be executed.

This allows for a clean separation of input detection and the actions that result from that input. You can easily change what the A button does by attaching a different script to its input mask.

Analogy to other programming languages/game engines: This is analogous to registering an event listener or a callback function for a specific input event:

It provides a flexible and event-driven way to respond to player input, making your game interactive and responsive.