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 inside class method

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

Joined: 25 Nov 2021
Posts: 2

PostPosted: Thu Nov 25, 2021 8:38 am    Post subject: Timer inside class method Reply with quote

Hello! I'm creating a class,
Code:

myClass = {}

function myClass:foo()
  print("placeholder")
end

function myClass:stop(timer)
  if <some condition> then
    timer.destroy()   
  else
    self:foo()
  end
end

function myClass:run()
  timer = createTimer(getMainForm())
  timer.Interval = 5 * 1000
  timer.OnTimer = self.stop
end

function myClass:new(t)
  t = t or {}
  setmetatable(t, self)
  self.__index = self
  return t
end

myObj = myClass:new()
myObj:run()


And I'm getting error inside myClass:stop func
"attempt to index a nil value (local 'timer')"

for some reason it doesn't pass timer inside func
Back to top
View user's profile Send private message
LeFiXER
Grandmaster Cheater Supreme
Reputation: 20

Joined: 02 Sep 2011
Posts: 1053
Location: 0x90

PostPosted: Thu Nov 25, 2021 12:37 pm    Post subject: Reply with quote

I'm not sure if CE is capable of using Lua this way, but it's good practice to make sure that the object is instantiated before trying to access its properties.
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Thu Nov 25, 2021 1:06 pm    Post subject: Reply with quote

The colon is just syntactic sugar for omitting the first parameter. The actual signature of the myClass:stop function is function myClass.stop(self, timer). When the stop function gets invoked by the timer, it passes the timer object to the first parameter (i.e. self) and nothing to the second parameter (i.e. timer == nil).

You'd be better off making timer a property of the class, unless for some reason it absolutely must be a global.

Code:
myClass = {}

function myClass:foo()
  print("placeholder")
end

function myClass:stop(timer)
  timer = timer or self.timer
  if <some condition> then
    timer.Enabled = false
  else
    self:foo()
  end
end

function myClass:run()
  self.timer.Enabled = true
end

function myClass:new(t)
  t = t or {}
  -- unless you're accessing something in the GUI of the main
  -- form (e.g. address list), it shouldn't need a parent
  t.timer = t.timer or createTimer(getMainForm(), false)
  t.timer.Interval = 5 * 1000
  t.timer.OnTimer = function(timer) self.stop(t, timer) end
  self.__index = self
  return setmetatable(t, self)
end

-- I *think* CE already does this, but just in case...
function myClass.__gc(o)
  if o.timer then
    o.timer.destroy()
    o.timer = nil
  end
end

myObj = myClass:new()
myObj:run()

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
ILuciferI
How do I cheat?
Reputation: 0

Joined: 25 Nov 2021
Posts: 2

PostPosted: Fri Nov 26, 2021 6:07 am    Post subject: Reply with quote

ParkourPenguin wrote:
The colon is just syntactic sugar for omitting the first parameter. The actual signature of the myClass:stop function is function myClass.stop(self, timer). When the stop function gets invoked by the timer, it passes the timer object to the first parameter (i.e. self) and nothing to the second parameter (i.e. timer == nil).

Yeah, that's what I thought... Thanks.

I made it like this, and it solved the problem.
Code:
timer.OnTimer = function(timer) self:stop(timer) end
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