How Do You Use The Defold Timer?


This may sound stupid, but how do you use the timer in defold.
I am trying to make the sprite i have fade by ‘tinting’ it by 0.1 every 0.5 second.

local splash_timer = timer.delay(0.5, true, tint_background())
function tint_background(self, handle, time_elapsed)
	local i = 1
	if(i <= 10) then
		go.set("#logo", "tint.w", (0.1*i))
		i = i + 1

The error message I keep getting is:
ERROR:SCRIPT: Unable to create a timer, the lua context does not have a timer world

I don’t get what I am doing wrong after reading Timer Reference.

Thanks for your help.


If this line:

local splash_timer = timer.delay(0.5, true, tint_background())

Is above the function tint_background(), like in your post - it wont work. The function should be above any instances where it used.

But I have no experience using the built in timer, I haven’t used it :thinking:

Ok so I messed with a timer a little :smiley:
You should also remove the parenthesis from your function in the timer declaration so it’s like this:

local splash_timer = timer.delay(0.5, true, tint_background)


Just tried it and now I get:
ERROR:SCRIPT: /main/main.script:1: bad argument #3 to ‘delay’ (function expected, got nil)


Nvm, I put

local splash_timer = timer.delay(0.5, true, tint_background)

Before the tint background function, that’s why your solution did not work.
Thank you for helping me :grinning:


Thank you too! I learned something new here also.

Does anyone from Defold know the advantage of using the built in timer function, as opposed to writing timer in LUA? Does this operate in C++ ?


The built in timer operates in C and will be less taxing on the CPU to use than constantly checking the timer in Lua.


Just out of curiousity does go.animate constantly check the timer?


No go.animate is a separate system, optimized to run many animations at the same time.


@jaymestmorris16 Have you considered using go.animate for this?

I’m not entirely sure about what kind of effect you’re after, but perhaps you could try something like this:

local function anim()
	go.animate("#sprite", "tint.w", go.PLAYBACK_ONCE_PINGPONG, 0.0, go.EASING_INOUTSINE, 0.5, 1.0, anim)

function init(self)

EDIT: go.animate


No, I haven’t been using Defold that long (this is my first project) and so far I have not messed with go.animate yet; but thank you for the suggestion, I will look into it. :+1: