Skip to content
Grisgram edited this page Aug 15, 2025 · 14 revisions

The var keyword in Scriptor is optional when declaring a script variable but it offers advantages:

  • You receive an error, if a variable with the specified name does already exist, so unintentionally overwriting existing variables can not happen.

  • Auto-Cleanup. A variable declared with the var keyword will be removed from the script at the end.
    Keep in mind, that scriptor preserves variables between runs. This is important for callbacks which will be launched sometimes many frames after the main code of the script. Variables declared with var will automatically vanish.

  • Using the var keyword has no performance impact at runtime.

Syntax

var <variable_name> = [value|expression|initializer]

Examples:

  1. Declaration with a value:
var myvar = 25    
  1. Declaration with an expression:
var myvar = self.x + self.sprite_width / 2   
  1. Declaration with an array initializer:
var myvar = []   
  1. Declaration with a struct initializer:
var myvar = {}   

Initializers

Arrays:

You can assign values to an array directly during initialization.

Examples:

var myarray = [1,2,3,4]
var myarray = ["Hello", "World", self.text]

Structs:

Unlike arrays, you cannot initialize struct members directly during declaration.

Invalid Example:

var mystruct = { x:255, y:200 }   // This is an error (not in GML, but in scriptor)

Valid Example:

var mystruct = {}
mystruct.x = 255
mystruct.y = 200

Clone this wiki locally