Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Making while loop more efficient?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Gou
Cheater
Reputation: 0

Joined: 02 Oct 2017
Posts: 39

PostPosted: Sun Jan 21, 2018 3:23 pm    Post subject: Making while loop more efficient? Reply with quote

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 Very Happy
Thanks in advance!
Back to top
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sun Jan 21, 2018 3:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Gou
Cheater
Reputation: 0

Joined: 02 Oct 2017
Posts: 39

PostPosted: Sun Jan 21, 2018 4:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sun Jan 21, 2018 4:38 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sun Jan 21, 2018 9:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
TheyCallMeTim13
Wiki Contributor
Reputation: 50

Joined: 24 Feb 2017
Posts: 976
Location: Pluto

PostPosted: Sun Jan 21, 2018 9:08 pm    Post subject: Reply with quote

For me the thread would run tell it made an access violation.

At least in older versions of CE.

_________________
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites