GBStudio - Swapping two Global Variables by Index Number
Posted by SysL, on July 2, 2026. [Link]
References:
Details
Look at your (tmp)\_gbsbuild\include\data\game_globals.h for what index numbers each global variable are, I used 0/1 for an easy example.
- Set Temp 0 to the index of variable 1
- Set Temp 1 to the index of variable 2
- Set the variables (for this test, if already set, skip)
- Display the variables (optional)
- GBVM script to swap them around by placing them and pulling them from the stack.
VM_RPN ; Required for the operations below
.R_REF_IND VAR_TEMP_0 ; Store pointer to Var 0 (set above) on stack
.R_REF_IND VAR_TEMP_1 ; Store pointer to Var 1 (set above) on stack
.R_REF_SET_IND VAR_TEMP_0 ; Pull from stack (Pointer to var 1) and assign to Var 0
.R_REF_SET_IND VAR_TEMP_1 ; Pull from stack (Pointer to var 0) and assign to Var 1
.R_STOP ; End RPN Mode
- Display the variables again (optional)
Things to Keep in Mind
- Variables do not exist until they are assigned a value in GBStudio, even if you have given them a unique name.
- Do not have any holes in your created variables, if you assign something to a hole, all the numbers after will change.





