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 


Timer on a function that has a parameter

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
crumbleybumbley
How do I cheat?
Reputation: 0

Joined: 28 Dec 2014
Posts: 8

PostPosted: Tue Jan 20, 2015 7:13 pm    Post subject: Timer on a function that has a parameter Reply with quote

I am logging some data from a game into a file, and the way I do that is using timers to check values on a table every x seconds. Basically it works like this:

Objects are added into the game periodically, they last for a variable period of time, and then leave. I want to track when they are added and when they leave. I do this by having a list of all the objects that have been added to the game, say objects a b and c that were added at 1, 2 and 3 seconds. So now I have a list of the objects (a, b, c) and a list of the start times (1, 2, 3). I also create a list of end times, and before I know when I leave the values are just 0, so (0, 0, 0).
SO three lists:
objects (actually has the address of the object)
(a, b, c)

start time
(1, 2, 3)

end time
(0, 0 ,0)

I detect if an object is gone with an AoB scan for the objects. So if the aobscan returned the addresses of a and c, I would check the objects list, and any objects left in the list that AoBScan didn't find again, I take the index of the unfound object and change that index in the end time list to the current time. So if at 4 seconds I found a and c, i would have the following lists:

found
(a, c)

objects
(a, b, c)

start
(1, 2, 3)

end
(0, 4, 0)


I need to keep that list of objects in order to see what objects are now missing. There really isn't another reliable way to track what isn't there anymore that I'm aware of.
My problem is I have to be able to pass my list of objects into the function every time I run it, which doesn't work when I use a timer for the functions. My timer set up looks like this:

Code:
function delayFunc(func, mlsec, sleeper)
   timer_setInterval(sleeper, mlsec)
   timer_onTimer(sleeper, func)
   timer_setEnabled(sleeper, true)
end

function writeObjects(parameter)
  --stuff to write to my lists using parameter
end

timerWriteObjects = createTimer(nil, disabled)

delayFunc(writeObjects(objectList), 5000, timerWriteObjects)


What happens is the timer goes only 1 time, then doesn't run again. When I create a script that has everything the same but with some dummy print statement in writeObjects and no parameter, it works fine. And I have also made sure it isn't just the way I wrote the writeObjects function, any function requiring a parameter will only run once.

I need the parameter because I get an access violation if I just try to call the objectList from inside the fucntion. I tried to instead use a while loop to write, but even with trying several sleep implementations I can never actually cancel the while loop when I want to because cheat engine just locks up (since I am unsure how long I will be running the script, I would have to cancel the while loop manually) .

Any help would be greatly appreciated.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25288
Location: The netherlands

PostPosted: Tue Jan 20, 2015 7:53 pm    Post subject: Reply with quote

you can't just pass functions with parameters with timers. The only parameter passed is the timer object itself (instead of func you are passing the result of writeObjects(objectList) to delayFunc, which is probably nil )



what you could use is the 'Tag' field of the timer to get to your data indirectly

Code:


function delayFunc(func, mlsec, sleeper, objectlist)
   sleeper.Tag=createRef(objectlist)
   sleeper.Interval=mlsec
   sleeper.onTimer=func
   sleeper.Enabled=true
end

function writeObjects(sender) --sender is the timer
  local objectlist=getRef(sender.Tag)
  --do stuff with objectlist

  --stuff to write to my lists using parameter
end

timerWriteObjects = createTimer(nil, disabled)

delayFunc(writeObjects, 5000, timerWriteObjects, objectlist)


please note that when the timer is not needed anymore that you do free the reference to the objectlist (memoryleaks otherwise)
so destroy it inside the timer function itself(using sender), or keep track of the timer yourself. (only using timerWriteObjects may cause issues...)

--
(you can use nativethread's which do support custom parameters and don't block on loops, but i'm not sure you wish to go there)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Sat Jan 24, 2015 2:01 pm    Post subject: Reply with quote

hi, crumbleybumbley, please check pm.

I've tried DB's suggestion on using nativethread, it seems can be more clear on the program logic if careful on thread handling.

This code test on how nativethread work:
Code:
TH_Release_All = false

local say = function(th,id,cnt,interval)
  th.freeOnTerminate(true)
  local delay = type(interval) == 'number' and interval or 1000
  for i=1,cnt do
    if TH_Release_All == true then return end -- left a way to end the thread from outside, don't know if any other method
    print(id,i)
    sleep(delay)
  end
end


local t1 = createNativeThread(say,"A",10,1000)
local t2 = createNativeThread(say,"B", 5,2000)
local t3 = createNativeThread(say,"C",30, 333)
Back to top
View user's profile Send private message
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