VM_SET_INDIRECT
is a GBVM instruction used to assign a value from one variable to another variable that is addressed indirectly, using an index stored in a third variable.
Purpose:
This instruction is the counterpart to VM_GET_INDIRECT
. While VM_GET_INDIRECT
retrieves a value from an indirectly addressed variable, VM_SET_INDIRECT
allows you to write a value to such a variable. This is crucial for:
Syntax:
VM_SET_INDIRECT IDXA, IDXB
IDXA
: The variable that contains the index (or offset) of the target variable. This variable tells the GBVM where to write the value.IDXB
: The source variable that contains the value to be assigned. This variable holds the data that will be written.Usage Example: Updating an Inventory Slot
Imagine you have an inventory system where VAR_INVENTORY_SLOT_0
, VAR_INVENTORY_SLOT_1
, etc., represent inventory slots. You have a variable VAR_TARGET_SLOT
that stores the index of the slot you want to modify, and VAR_NEW_ITEM_ID
holds the ID of the item you want to place in that slot.
; In your script:
; Inventory slot variables (conceptual, assumed to be contiguous)
VAR_INVENTORY_SLOT_0:
.R_INT8 0 ; Empty
VAR_INVENTORY_SLOT_1:
.R_INT8 10 ; Sword ID
VAR_INVENTORY_SLOT_2:
.R_INT8 25 ; Potion ID
; Variable to hold the target slot index
VAR_TARGET_SLOT:
.R_INT8 0 ; Initialize to slot 0
; Variable to hold the new item ID to place
VAR_NEW_ITEM_ID:
.R_INT8 0 ; Initialize to 0
; Scenario: Player picks up a new item (e.g., a Shield, ID 50) and wants to put it in slot 0.
VM_SET_INT8 VAR_TARGET_SLOT, 0 ; Set target slot to 0
VM_SET_INT8 VAR_NEW_ITEM_ID, 50 ; Set new item ID to 50 (Shield)
; Assign the value from VAR_NEW_ITEM_ID to the inventory slot pointed to by VAR_TARGET_SLOT
VM_SET_INDIRECT VAR_TARGET_SLOT, VAR_NEW_ITEM_ID
; Now, VAR_INVENTORY_SLOT_0 will contain 50 (Shield ID).
; Scenario: Player wants to replace the Sword (slot 1) with an empty slot (ID 0).
VM_SET_INT8 VAR_TARGET_SLOT, 1 ; Set target slot to 1
VM_SET_INT8 VAR_NEW_ITEM_ID, 0 ; Set new item ID to 0 (empty)
VM_SET_INDIRECT VAR_TARGET_SLOT, VAR_NEW_ITEM_ID
; Now, VAR_INVENTORY_SLOT_1 will contain 0 (empty).
In this example, VM_SET_INDIRECT
uses the value in VAR_TARGET_SLOT
(e.g., 0
or 1
) to determine which global variable (VAR_INVENTORY_SLOT_0
or VAR_INVENTORY_SLOT_1
) to write to. It then takes the value from VAR_NEW_ITEM_ID
and places it into that indirectly addressed inventory slot.
Analogy to other programming languages: This is analogous to assigning a value to an element of an array using a variable as the index:
inventory_array[target_slot] = new_item_id;
inventory_list[target_slot] = new_item_id
It provides a way to dynamically update the content of a variable whose identity is itself stored in another variable, enabling powerful indirect addressing capabilities for writing data.