LiveCode

Pitfalls

Why this Page

LiveCode is not perfect.  As new releases appear, the language will improve.  Yet there will also always be problems of one kind or another.

Here I want to show a number of difficulties that you may experience in the beginning.  There may be other solutions than the ones I give below, if you find any I would be grateful if you let me know.

Loops

In the Circles example there is a loop:

repeat with j = the number of graphics down to 1

delete graphic j

end repeat

It seems more natural to write the loop like this:

repeat with j = 1 to the number of graphics

delete graphic j

end repeat

But that fails and the reason is not obvious.  Suppose there are 4 graphics.  First j is 1, and graphic 1 will be deleted.  But LiveCode renumbers the objects immediately.  When the loop starts the graphics are numbered 1,2,3,4.  After the first one is deleted we do not have 2,3,4 but they are now numbered 1,2,3.  Then the second one is deleted and instead of being left with graphics 1,3 we in fact have 1,2.  The next time around the loop wants to delete graphic number 3, but that does not exist and the program fails with an error.

Note that this is all very well specified.  Exactly the same problem occurs when deleting lines of text or any other sort of chunk or object:  the renumbering means that you best start at the end and work your way towards the beginning.

The dictionary also mentions that the expressions at the start of the loop are evaluated once at that moment and do not change later.  Hence this piece of code:

repeat with j = the number of graphics down to 1

delete graphic j

put return & the number of graphics after msg

end repeat

would produce this output in the message box:

4 3 2 1

showing that the number of graphics is changing inside the loop, but the value it had at the start is kept for the entire duration of the loop.

You might however write:

repeat with j = 1 to the number of graphics

delete graphic 1

end repeat

and that would work, but it is not much more understandable.