Object-orientated lua is breaking my mind, help me out with a timer class I'm working on.



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


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:



[ERROR] cooldown.lua:21: attempt to index local 'self' (a nil value)


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 :slight_smile:

I don’t think timers have accepted extra parameters since GMod 13. Do this instead:



timer.Create( "test", self.amount, 1, function() self:Finish() end )


Or even better, since you’re only running it once:



timer.Simple( self.amount, function() self:Finish() end )


Works like a charm, thank you very much!

Sort of a side note: How do I set this thread to ‘solved’ ?

No problem.

Reply will be changed with “mark as solved” if you are the thread creator.