Skip to content
Kiki edited this page Dec 25, 2024 · 11 revisions

The var keyword in Scriptor is optional when declaring a script variable.

Using var offers additional safeguards, such as raising an error if the variable has already been declared in the script. This helps improve code safety by preventing accidental overwrites.
However, it 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

Valid Example:

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