Posted: Mon Dec 23, 2019 7:44 pm Post subject: using timer on Lua Engine
I have an script where I am exctracting an Adress. after that, I want to check the same adress until this change to a new value and continue with my script.
this is my example.
Code:
-- My script where I find the Adress.
MyAdress = -- adress
Value = readDouble(MyAdress)
while true do
New_Value = readDouble(MyAdress)
if ( New_Value ~= Value ) then
Value = New_Value
break
end
end
-- continue my scrip...
the problem is if the value take a lot of time to change, the CE have issues like "not responding" and this happened even if I put a sleep in the loop, I tried to use timer in lua instead to use while loop. but the problem is, the timer start when the whole script is finished. for example.
timer.OnTimer = function(timer)
New_Value = readDouble(MyAdress)
if New_Value ~= Value then
Value = New_Value
timer.destroy()
end
end
Code:
-- Continue my script
print('The new value is --> ',Value)
And this timer is working well ending with a new value, but the problem is first the final print is execute, and after that, the timer. I only have some days with lua language, I am still learning. maybe I am not using the timer really well. Any correction on my script I would apreciate. _________________
I would like to improve my level of hackng programs.
The reason is, that the code is running continuously in the main thread, thus it cannot proceed and handle other events until it's out of the loop.
On solution is to insert in your loop (while true do ... end);
The following command,
Code:
processMessages();
So C.E would continue to handle messages and won't go unresponsive.
But doing such loop is quite intensive and unnecessary (for this kind of task), so using a timer is much better solution.
Which bring us to the next solution;
Another simple solution is to place the rest of the script inside a function, and from the timer object call it.
timer.OnTimer = function(timer)
New_Value = readDouble(MyAdress)
if New_Value ~= Value then
Value = New_Value
timer.destroy();
ContinueScript();
end
end
function ContinueScript(...)
print('The new value is --> ',Value)
end
_________________
I'm rusty and getting older, help me re-learn lua.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum