VM_GET_INDIRECT
is a GBVM instruction that allows you to access the value of a variable indirectly, using another variable as an index or pointer to the target variable. This is a powerful feature for implementing array-like structures or dynamic variable access in GBVM.
Purpose:
In many programming scenarios, you need to work with collections of data or variables where the specific variable you want to access is not known until runtime. VM_GET_INDIRECT
addresses this by enabling you to:
IDXB
holds the index into that array.Syntax:
VM_GET_INDIRECT IDXA, IDXB
IDXA
: The target variable where the value retrieved indirectly will be stored. This is the variable that will receive the data.IDXB
: The index variable. This variable contains the index (or offset) of the source variable whose value you want to retrieve. The GBVM interprets this index relative to its internal global variable table.Usage Example: Accessing Player Inventory Slots
Imagine you have a player inventory system where each inventory slot is represented by a global variable (e.g., VAR_INVENTORY_SLOT_0
, VAR_INVENTORY_SLOT_1
, etc.). You want to retrieve the item ID from a specific inventory slot, where the slot number is stored in another variable, VAR_CURRENT_SLOT
.
; Assume VAR_INVENTORY_SLOT_0, VAR_INVENTORY_SLOT_1, etc., are defined
; and hold item IDs (e.g., 0 for empty, 1 for Sword, 2 for Potion).
; Variable to hold the current slot number (e.g., 0, 1, 2...)
VAR_CURRENT_SLOT:
.R_INT8 0 ; Initialize to slot 0
; Variable to store the retrieved item ID
VAR_RETRIEVED_ITEM_ID:
.R_INT8 0
; Set VAR_CURRENT_SLOT to, for example, 1 (to access VAR_INVENTORY_SLOT_1)
VM_SET_INT8 VAR_CURRENT_SLOT, 1
; Get the value from the inventory slot pointed to by VAR_CURRENT_SLOT
; and store it in VAR_RETRIEVED_ITEM_ID.
VM_GET_INDIRECT VAR_RETRIEVED_ITEM_ID, VAR_CURRENT_SLOT
; Now VAR_RETRIEVED_ITEM_ID will contain the value of VAR_INVENTORY_SLOT_1
; ... you can then use VAR_RETRIEVED_ITEM_ID for game logic ...
; Example of how inventory slots might be defined (conceptual)
; These would be global variables in your project
VAR_INVENTORY_SLOT_0:
.R_INT8 0 ; Empty
VAR_INVENTORY_SLOT_1:
.R_INT8 1 ; Sword
VAR_INVENTORY_SLOT_2:
.R_INT8 2 ; Potion
; ... and so on ...
In this example, VM_GET_INDIRECT
uses the value in VAR_CURRENT_SLOT
(which is 1
) to determine which global variable to read from. It effectively reads the value of VAR_INVENTORY_SLOT_1
and places it into VAR_RETRIEVED_ITEM_ID
.
Analogy to other programming languages: This is directly analogous to array indexing in most programming languages:
retrieved_item = inventory_array[current_slot];
retrieved_item = inventory_list[current_slot]
Where current_slot
is a variable holding the index. VM_GET_INDIRECT
provides this dynamic access capability within the GBVM environment, allowing you to build more flexible data structures and logic.