11.12.2025, 10:24
A key feature of the resident scripts with 0 second pause is that their global variables are memorized from one execution to the next one. Knowing this, if we want to have a script that always does the same thing over and over, we have two options:
Option 1 - Each execution of the script terminates and then passes the values of the global variables to the next execution
Option 2 - There is only one execution of the script, it runs forever
Can you explain to me which of the two options is the most efficient and the most CPU-friendly, and tell me why?
Option 1 - Each execution of the script terminates and then passes the values of the global variables to the next execution
Code:
if MY_SCRIPT == nil then
-- initialization
MY_SCRIPT = {}
MY_SCRIPT.foo1 = value1
MY_SCRIPT.foo2 = value2
MY_SCRIPT.loop = function()
-- do some stuff
end
end
MY_SCRIPT.loop()
-- one loop execution runs, then the script ends,
-- and because it's a resident 0, it will restart itself
-- immediately, with the variable MY_SCRIPT still in memory,
-- so the loop function will run againOption 2 - There is only one execution of the script, it runs forever
Code:
-- initialization
MY_SCRIPT = {}
MY_SCRIPT.foo1 = value1
MY_SCRIPT.foo2 = value2
MY_SCRIPT.loop = function()
-- do some stuff
end
-- we loop forever, so it's always the same instance of the script running
while true do
MY_SCRIPT.loop()
endCan you explain to me which of the two options is the most efficient and the most CPU-friendly, and tell me why?