Object-orientated lua is breaking my mind, help me out with a timer class I'm working on.
5 replies, posted
[code]
CoolDown = {}
CoolDown.__index = CoolDown
function CoolDown.create(amount)
local me = {}
setmetatable(me,CoolDown)
me.ready = true
me.amount = amount
return me
end
function CoolDown:run() --Starts the timer
if self.ready == true then
timer.Create("test",self.amount,1,self.finish,self) --notice that I'm giving self as a parameter
self.ready = false
end
end
function CoolDown:finish() --should be run when the timer finishes
self.ready = true --Problem line: It sees 'self' as nil.
end
[/code]
I'm calling the function CoolDown:Finish from a timer in CoolDown:Run. As you can see I am clearly giving 'self' as a parameter. For some reason it still throws the following error in my face though:
[code]
[ERROR] cooldown.lua:21: attempt to index local 'self' (a nil value)
[/code]
What am I doing wrong? I tried adding a parameter to CoolDown:Finish but that didn't help, I even tried to give in the entire metatable as parameter. I'm getting the feeling that the timer function isn't properly giving the parameters to Cooldown:Finish. Should I be using it in a different way? Help!
-snip- I am retarded, I apologise.
edit: I should learn to read and not just skim things I have no experience in :)
I don't think timers have accepted extra parameters since GMod 13. Do this instead:
[code]
timer.Create( "test", self.amount, 1, function() self:Finish() end )
[/code]
Or even better, since you're only running it once:
[code]
timer.Simple( self.amount, function() self:Finish() end )
[/code]
[QUOTE=EvacX;41326287]I don't think timers have accepted extra parameters since GMod 13. Do this instead:
[code]
timer.Create( "test", self.amount, 1, function() self:Finish() end )
[/code]
Or even better, since you're only running it once:
[code]
timer.Simple( self.amount, function() self:Finish() end )
[/code][/QUOTE]
Works like a charm, thank you very much!
Sort of a side note: How do I set this thread to 'solved' ?
No problem.
[QUOTE=Anthophobian;41326292]Works like a charm, thank you very much!
Sort of a side note: How do I set this thread to 'solved' ?[/QUOTE]
[img]http://i.imgur.com/xmEjcUA.png[/img]
Reply will be changed with "mark as solved" if you are the thread creator.
Sorry, you need to Log In to post a reply to this thread.