
30
CONTENTS
7.6.2.7
Transient state management
Local and global variables of scripts are shared between
created in a single script environment but are
in general separate between separately loaded script environments. In the example below, if you create several
threads using
thread_creator
, they will all reference the same instances of
local_var
and
global_
←
-
var
:
local local_var=0
global_var=0
function thread_fn()
while true do
...
local_var=lo1
global_var=glo1
...
end
end
function thread_creator()
for i=1,10 do
thread.run(thread_fn)
end
end
Even if you run
thread_creator
several times (e.g. from the web UI) without changing source code, all of the
threads will share both
local_var
and
global_var
.
However, if you change the code and launch
thread_creator
again, new instances of
local_var
and
global_var
will be created; the 10 new threads will be completely separate from the old threads.
This makes handling global functions and variables consistent (e.g. if you don't have a global variable in the script,
you won't accidentally trip over it if it was there in a script you loaded several edit iterations earlier), but this default
behaviour may or may not be what you want.
To store arbitrary data between script edits, you can create entries in the global
sticky
table, like this:
sticky["variable"]="some value to save between script edits"
As usual in Lua, for identifier-like keys you can alternatively use the dot syntax:
sticky.variable="some value to save between script edits"
The above example then becomes:
function init_sticky()
sticky.local_var=0
sticky.global_var=0
end
function thread_fn()
while true do
...
sticky.local_var=sticky.lo1
sticky.global_var=sticky.glo1
...
end
end
function thread_creator()
for i=1,10 do
thread.run(thread_fn)
end
end
DLI LPC9 User’s Guide: 1.7.24.0
Содержание LPC9
Страница 1: ...DLI LPC9 User s Guide 1 7 24 0 ...
Страница 81: ......