GBStudio - Swapping two Global Variables by Index Number

Posted by SysL, on July 2, 2026. [Link]

References:

  1. https://mxashlynn.itch.io/memory-gbs4/devlog/855246/shuffling-20-cards-in-gb-studio-4


Details

Blog Image


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.


  1. Set Temp 0 to the index of variable 1
  2. Set Temp 1 to the index of variable 2
  3. Set the variables (for this test, if already set, skip)
  4. Display the variables (optional)
  5. 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
  1. Display the variables again (optional)


Things to Keep in Mind

  1. Variables do not exist until they are assigned a value in GBStudio, even if you have given them a unique name.
  2. Do not have any holes in your created variables, if you assign something to a hole, all the numbers after will change.