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 = 1
This 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.
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.end
if something then​-- a global variablethisIsGlobal = 5-- a local variablelocal thisIsLocal = 5 * 2end​Log(thisIsGlobal) -- Outputs 5Log(thisIsLocal) -- ERROR: Unknown references: thisIsLocal
local myFunction = function(value)​-- a global variablethisIsGlobal = value-- a local variablelocal thisIsLocal = value * 2end​-- Call the functionmyFunction(5)​Log(thisIsGlobal) -- Outputs 5Log(thisIsLocal) -- ERROR: Unknown references: thisIsLocal
text = "Awesome text!"​local changeText = function(value)text = valueend​changeText("..not anymore!")​Log(text) -- Outputs '..not anymore!'
local text = "Awesome text!"​local changeText = function(value)-- 'text' is visible within this sub-scope, even when defined as localLog(text) -- Outputs 'Awesome text!'-- Local 'text' variable for this sub-scopelocal text = valueend​changeText("..not anymore?")​Log(text) -- Outputs 'Awesome text!'
local text = "Awesome text!"​local changeText = function(text)-- The input parameter 'text' will be-- hiding the locally defined 'text',-- so this Log() prints what-- ever the input is.Log(text)end​changeText("..not anymore?") -- Outputs '..not anymore?'​Log(text) -- Outputs 'Awesome text!'
if condition then​end​if condition == true then​end​if condition == false then​end​if not condition then​end
if condition1 then​else​end
if condition1 then​elseif condition2 then​end
Syntax:
local value = condition and value_if_true or value_if_false
Example:
local condition = truelocal text = condition and "Condition is true." or "Condition is false."​Log(text) -- Outputs: Condition is true.
local condition1 = falselocal condition2 = true​Log("condition1:")Log(condition1 and false or true) -- Outputs: trueLog(condition1 and true or false) -- Outputs: false​Log("condition2:")Log(condition2 and false or true) -- Outputs: trueLog(condition2 and true or false) -- Outputs: true
local variableLog( variable and 'variable is not nil' or 'variable is nil')-- Output: variable is nil​local variable = nilLog( variable and 'variable is not nil' or 'variable is nil')-- Output: variable is nil​local variable = 0Log( variable and 'variable is not nil' or 'variable is nil')-- Output: variable is not nil​local variable = ''Log( variable and 'variable is not nil' or 'variable is nil')-- Output: variable is not nil​local variable = falseLog( variable and 'variable is true' or 'variable false')-- Output: variable is false​local variable = trueLog( variable and 'variable is true' or 'variable false')-- Output: variable is true
-- Load defaults to 'nil'local powerLevel = Load('pl')​if not powerLevel then -- or 'if powerLevel == nil then'powerLevel = 9001Log('Power level set.')end​Save('pl', powerLevel)
The for each iterator returns the value of each element in the collection.
closePrices = ClosePrices()​for value in closePrices doLog(value)end
The iterator can also be used with key,value, if the key is defined.
object = { keyA = 'valueA', keyB = 'valueB'}​for key,value in pairs(object) doLog(key .. " = " .. value)end
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.
prices = ClosePrices()price = prices[1] -- First price value in 'prices'
object = { keyA = 'valueA', keyB = 'valueB'}​Log(object.keyA) -- Outputs 'valueA'Log(object.keyB) -- Outputs 'valueB'Log(object.keyC) -- Outputs 'nil' (doesn't exist)​-- You can create new fields in objects-- by assigning a value into them.object.keyC = 'valueC'Log(object.keyC) -- Outputs 'valueC'
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)
for init, max/min value, [increment] do-- Do somethingend
Using #
with the array variable will return the array size. This way we can easily loop over each value.
closePrices = ClosePrices()​for i = 1, #closePrices doLog(closePrices[i])end
For the backwards loop we swap start and end value and set the increment to -1.
closePrices = ClosePrices()​for i = #closePrices, 1, -1 doLog(closePrices[i])end
HaasScript supports single- and multi-line comments.
-- Single-line comment​if commentingIsCool == true then--[[ Multi-line comment, thatcan continue throughmultiple lines andstop suddenly]] but = "allows code to continue"end​--[[This also works.--]]​--[===[And this.--]===]​--[=[This doesn't--]==]
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.
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.
array = {"HaasScript", "Tutorial"}​for i = 0, 2 doLog(array[i])end
When we run the above code, we will get the following output.
nilHaasScriptTutorial
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.
array = {}​for i= -2, 2 doarray[i] = i * 2end​for i = -2, 2 doLog(array[i])end
When we run the above code, we will get the following output.
-4-2024
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.
-- Initializing the arrayarray = {}​for i=1,3 doarray[i] = {}for j=1,3 doarray[i][j] = i*jendend​-- Accessing the array​for i=1,3 do​for j=1,3 doLog(array[i][j])endend
When we run the above code, we will get the following output.
123246369
An example for multidimensional array is shown below using manipulating indices.
-- Initializing the array​array = {}​maxRows = 3maxColumns = 3​for row=1,maxRows do​for col=1,maxColumns doarray[row*maxColumns +col] = row*colendend​-- Accessing the array​for row=1,maxRows do​for col=1,maxColumns doLog(array[row*maxColumns +col])endend
When we run the above code, we will get the following output.
123246369
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.