Syntax
Syntax for the Haasonline Script Editor
Local vs. Global
Variables in HaasScript can be defined as either global or local. The difference between these two is that the other is accessible globally and the other is not:
-- This is a global variable (accessible globally)
myVariable = 1
-- This is a local variable (accessible locally)
local myVariable = 1This means, that any variables defined as global will be visible everywhere in the script's scope, whereas the variables defined as local are only visible inside the scope they were first defined in but also in their sub-scopes.
Scope
To give you an example what a scope is in HaasScript, here's a script with an if-statement and a function:
-- We are inside the script's scope.
-- Variables defined here, either global or local,
-- are visible in sub-scopes within this script.
if something then
-- We are now inside the scope of an if-statement.
-- Local variables defined here are not visible outside.
end
local someFunction = function()
-- We are now inside the scope of a function.
-- Local variables defined here are not visible outside.
endInside if-statements
Inside functions
Conflicting variables
Example of bad practice:
Example of good practice:
Overwriting variables in a function:
If
If & Else
If & ElseIf
If (One-liner)
Syntax:
Example:
Booleans!?
Nil values and assignments
Loading without default values
For each
The for each iterator returns the value of each element in the collection.
The iterator can also be used with key,value, if the key is defined.
Indexing
Lua's index is based on 1. You can use 0 as index in Lua, but HaasScript will throw you an error for it. This restriction is implemented to break any confusions.
Accessing object fields
For loop
All the for loops are based on a default structure. The init and max/min value are always required and the increment is optional (default +1)
Forward loop
Using # with the array variable will return the array size. This way we can easily loop over each value.
Backwards loop
For the backwards loop we swap start and end value and set the increment to -1.
Comments
HaasScript supports single- and multi-line comments.
Arrays
Arrays are ordered arrangements of objects, which may be a one-dimensional array containing a collection of rows or a multi-dimensional array containing multiple rows and columns.
In HaasScript, arrays are implemented using indexing tables with integers. The size of an array is not fixed and it can grow based on your requirements, subject to memory constraints.
HaasScript also offers an alternative way for creating and manipulating arrays.
Array HelpersOne-dimensional array
A one-dimensional array can be represented using a simple table structure and can be initialized and read using a simple for loop. An example is shown below.
When we run the above code, we will get the following output.
As you can see in the above code, when we are trying to access an element in an index that is not there in the array, it returns nil. In HaasScript, indexing is limited to start at index 1. It is possible to create objects at index 0 and below 0 as well. Array using negative indices is shown below where we initialize the array using a for loop.
When we run the above code, we will get the following output.
Multi-Dimensional Array
Multi-dimensional arrays can be implemented in two ways.
Array of arrays
Single dimensional array by manipulating indices
An example for multidimensional array of 3. 3 is shown below using array of arrays.
When we run the above code, we will get the following output.
An example for multidimensional array is shown below using manipulating indices.
When we run the above code, we will get the following output.
As you can see in the above example, data is stored based on indices. It is possible to place the elements in a sparse way and it is the way Lua implementation of a matrix works. Since it does not store nil values in Lua, it is possible to save lots of memory without any special technique in Lua as compared to special techniques used in other programming languages.
Last updated
Was this helpful?
