Whats the best way to perform an action periodically? (SOLVED)

Hey so im a fairly new user to Defold (Love it so far) and game development.

Im trying to set some variables every 3 seconds. I have been using the socket.time() function and have tried modding by 3( and 3.0) but it is not working. Are there any built in functions that do this and if not any suggestions as to a good way to do this?

Thanks,

James

2 Likes

Use a timer.

4 Likes

So, i have been searching but I cannot seem to find of an example of this being used. Could you provide some link (or example code for this). I am doing something wrong.

And all the examples Ive seen have been of the timer that Britzl created using the timer.seconds()

Something like this:

local function timer_callback(self, handle, time_elapsed)
	print("time_elapsed: " , time_elapsed)
	-- Cancel anytime with:  
	-- timer.cancel(self.timer_handle ) 
	-- or
	-- timer.cancel(handle) 
end
function init(self)
        -- timer.delay(delay, repeat, callback)
	self.timer_handle =  timer.delay(3, true, timer_callback)
end
1 Like

Thanks! That works!

1 Like

Is there a way to reset the timer in the middle of operation instead of cancelling it?

As far as I know no, you can’t reset the timer.
If you need more complex solution then you can calculate your timing in the “update” function.

1 Like