Don't understand the meaning of "local"

Jim Hurley jhurley at infostations.com
Thu Dec 22 15:58:18 EST 2005


I suspect that  this issue has been dealt with before, but I don't 
get it. I thought I understood what a local variable was.

Create a field. Put a bunch  of returns into it--or several lines of text.

Lock the field and put the following script into it.

Hold the mouse down and move it through the field. I would have 
expected a list of the lines that have been moved over to appear in 
the msg box. Instead only the current line is shown.

But I so get the entire list if I include tLines as  a local script 
variable. Apparently tLines is set to empty after every execution of 
mouseMove, unless tLines is included as a local script variable.

I get the feeling that mouseMove stops and restarts with every cycle 
and therefore forgets the value of tLines.

However if tLines is set as a local script varable then the script 
continues to run and therefore remembers tLines.

I had thought that the function of the local script declaration was 
to allow the variable to be used throughout  the handlers of the 
script. However, in this example tLines is used only in mouseMove.

I think I ran into this behavior earlier when adding a rolling 
snowball asynchronously to Scott's snow storm. I wondered why local 
variables were kept in memory each time a handler called itself after 
a few milliseconds. Once again it appears that setting a local script 
variable retains the variables  in memory--even when the script stops 
(?) until called again.

Anyone understand the different behavior of this script depending on 
whether tLines is declared as a local script variable? Or, more 
challenging, can anyone explain it to me?


local myName,  --tLines

on mouseDown
   put the name of me into myName
end mouseDown

on mouseUP
   put "" into myName
end mouseUP


on mouseMove
   if myName is "" then exit mouseMove

   put  word 2 of the MouseLine into tLine
   if tLine is not in tLines then put  tLine &  comma after tLines
   put tLines into msg box

end mouseMove



More information about the use-livecode mailing list