Skip to content

Assignments

Hawkmax edited this page Dec 27, 2024 · 8 revisions

An assignment statement is any operation that assigns a value to a target.

This can range from a simple variable assignment, such as var x = 25, to more complex expressions like alive = (enemy.hp > 0 || enemy.immortal). In all cases, an assignment statement is characterized by the presence of an equals sign = as its second word.

While the expression evaluation engine in scriptor is superior, we face some limitations on assignments in favor to parser complexity and performance.

General Rules

Scriptor allows you to assign any value to a variable, supporting simple and complex expressions. For Example:

// Assigning values to variables
self.x = 100
self.y = 2 * x + self.sprite_xoffset * ROOMCONTROLLER.camera_zoom

// Variable assignments
myvar = 25

// Array assignments
my_array = []
my_array = [1,2,3,4]

// Struct assignments
my_struct = {}
my_struct.my_val = 25

Expression Evaluation in Assignments

Everything on the right-hand side of the equals sign = is processed by Scriptor's expression evaluation engine, allowing for highly complex expressions.

However, there are limitations on the left-hand side (the receiving part) when it comes to array assignments.

Limitations for Array Assignments

You cannot directly assign a value to a specific index of an array using the bracket notation []. This is a deliberate restriction to prioritize performance and reduce parser complexity.

Invalid Example:

my_array[4] = 25                  // This does NOT work!

Valid Example:

Instead of direct indexing, use the array_set function:

array_set(my_array, 4, 25)  

Built-In Array Functions

Scriptor provides the most important GML array functions out-of-the-box for managing arrays:

array_pop
array_push
array_shift
array_get
array_set
array_first
array_last
array_length

These functions allow for flexible and performant array manipulation without compromising the efficiency of the parser.

Clone this wiki locally