View previous topic :: View next topic |
Author |
Message |
Gou Cheater Reputation: 0
Joined: 02 Oct 2017 Posts: 39
|
Posted: Sun Jan 21, 2018 3:23 pm Post subject: Making while loop more efficient? |
|
|
Hey guys!
So i wrote this short script, and i used a while loop.
It does work, but it chews a lot of CPU power, and ends up freezing CE.
Here it is
Code: | while blah == true do
if readBytes(myAddress) == 0 then
writeBytes(myOtherAddress,myValue)
else if readBytes(myAddress) == 255 then
writeBytes(myOtherAddress,myValue)
end
end |
Is there a way to slow it down a bit? I'm guessing with a timer but i dont really understand them very well
Thanks in advance!
|
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sun Jan 21, 2018 3:28 pm Post subject: |
|
|
Code: | --------
-------- Timer
--------
local aTimer = nil
local aTimerInterval = 100
local aTimerTicks = 0
local aTimerTickMax = 0 -- set to zero for no max or set to the max. ticks allowed
local function aTimer_tick(timer)
if aTimerTickMax > 0 and aTimerTicks >= aTimerTickMax then
timer.destroy()
end
aTimerTicks = aTimerTicks + 1
-- body
local b = readBytes(myAddress, 1)
if b == 0 or b == 255 then
writeBytes(myOtherAddress,myValue)
end
end
aTimer = createTimer(MainForm, false)
aTimer.Interval = aTimerInterval
aTimer.OnTimer = aTimer_tick
aTimer.Enabled = true |
EDIT:
You can also add "if not bla then timer.destroy() end" to the timer tick function.
_________________
|
|
Back to top |
|
|
Gou Cheater Reputation: 0
Joined: 02 Oct 2017 Posts: 39
|
Posted: Sun Jan 21, 2018 4:10 pm Post subject: |
|
|
Thanks for the reply Tim!
As i said earlier, i dont understand timers very well(i really should look into them haha). It ended up not working. I don't really know what was the issue, and dont have the knowledge to mess with it. Ended up using the basic example code that you can find on the wiki.
Code: | local timer = createTimer(getMainForm())
timer.Interval = 100
timer.OnTimer = function(timer)
local b = readBytes(myAddress, 1)
if b == 0 then
writeBytes(myOtherAddress,myValue)
else if b == 255 then
writeBytes(myOtherAddress,myOtherValue)
end
if hack == false then
timer.destroy()
end
end
end |
And it works like a charm!
Thanks once again man!
|
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sun Jan 21, 2018 4:38 pm Post subject: |
|
|
Best I can say is it may have been the "MainForm" vs "getMainForm()", "MainForm" was added in 6.6 or 6.7 I think.
_________________
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sun Jan 21, 2018 9:00 pm Post subject: |
|
|
MainForm was added in 6.7, I remember having to change it when testing something in 6.6 recently and it failing.
Though I don't see any reason to explicitly pass MainForm as the owner, if the MainForm closes doesn't all of CE close and if CE itself closes then the timer isn't going to do anything...
|
|
Back to top |
|
|
TheyCallMeTim13 Wiki Contributor Reputation: 50
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sun Jan 21, 2018 9:08 pm Post subject: |
|
|
For me the thread would run tell it made an access violation.
At least in older versions of CE.
_________________
|
|
Back to top |
|
|
|