How Do You Use The Defold Timer? (SOLVED)

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
	else
		timer.cancel(handle)
	end
end

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:

Edit:
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)
1 Like

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:

2 Likes

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++ ?

1 Like

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

4 Likes

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.

1 Like

@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)
end

function init(self)
	anim()
end

EDIT: go.animate

7 Likes

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:

2 Likes

Hi!

Sorry, still having trouble with this. I need to pass some variables onto a function. This code is not working. bad argument #3 to ‘delay’ (function expected, got no value):

timer.delay(1, false, type(self, self.cola[1], "STATE:", " SYSTEM CHECK"))

When i do this, everything works:

type(self, self.cola[1], "STATE:", " SYSTEM CHECK")

but I can’t get it working with the timer. Any help? Thanks!

1 Like

got it!

timer.delay(1, false, function(self) type(self, self.cola[1], "STATE:", " SYSTEM CHECK") self.state = 1 end)```

Nice that you got it!

An explanation for other readers:

This returns a value, whereas the callback passed in should be a function.

1 Like

I was just looking for ways to make timers in defold when I came across your post I was just wondering what unit is used to measure time in the timer.delay function

Seconds.

1 Like

Okay just asking because it looked like milliseconds because I did not have the correct parameters in my function.