beautypg.com

Tables – ETC Unison Mosaic Designer v1.11.0 User Manual

Page 216

background image

Unison Mosaic Designer User Manual

-- first choice

elseif myNumber < 15 and myNumber > 10 then

-- second choice

else

-- third choice

end

The other control structures all involve blocks of script that need to be repeated a certain number of times. The
most straightforward is the while loop, which will repeat the enclosed block of script as long as the test at the
start is true:

myNumber = 10

while myNumber > 0 do

-- some useful script

myNumber = myNumber - 1 -- myNumber counts down

end

The repeat until loop is really exactly the same, but here the test is done at the end of each loop and it will
repeat while the test is false.

myNumber = 1

maxNumber = 4096

repeat

-- some useful script

myNumber = myNumber * 2

until myNumber == maxNumber

Here it is worth noting the use of two equal signs == to mean 'is equal to' in a test. This is different from a single
equal sign, which is used for assigning values. It is another very common mistake to assign a value when you
meant to test if it was equal, and it can be hard to spot because it is valid syntax that will not generate an error.
The opposite of == meaning 'is equal to' is ~= meaning 'is not equal to'.

The other control structure is the for loop, which has a number of powerful options beyond the scope of what we
need here. But it is worth seeing how it can be used to do basic loops in a slightly neater way:

for i = 1,10 do

-- some useful script where i has value 1 to 10

end

A final word of caution regarding loops: be careful that you do not write a loop that will never exit! This is all too
easy to do by forgetting to increment a counter value that you are using in the test for the loop. If your script has
one of these 'infinite loops' then the Controller will get stuck when it runs the script and be reset by the watchdog
feature (provided this is enabled). Script is a tool for the grown-ups and it will not protect you from doing silly
things - so make sure you test your scripts carefully before leaving them to run.

Tables

Often you will need to store a set of values within a script - these might be a list of timeline numbers or the current
states of all the contact closure inputs. Lua allows us to store multiple values within a single named variable and
this is called a Table.

A table has to be created before it can be used:

- 216 -