Delay functions?

How can i delay functions? invoke exist here? or how to make a simple count down to do any action after some seconds?

1 Like

At the moment there is no dedicated function for delay but as I understand it is in the backlog. Meanwhile most people either use the go.animate function with a callback function to create the same thing or create a timer var that is ticked down in a script update function.

i tried the go.animate function but it doesnt wait the seconds that i assign, i don’t know why… in less than a second it calls the callback function =S

edit: nvm, i don’t know why, but now is working XDD

Hi Tino,

I’m encountering the issue where the callback function is called way too early.
How did you solve it? Couldn’t find it in the threads, sorry.

Could it be that you are passing the callback function with paranthesis? That is:

go.animate("#", "property", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_LINEAR, 0, callback())

instead of

go.animate("#", "property", go.PLAYBACK_ONCE_FORWARD, 1, go.EASING_LINEAR, 0, callback)

2 Likes

Yeah it seems to be the case.

Creating a function within the parameter works well for me too.

sorry for the late response. As Johan said, the problem was the parenthesis when you call the function.

How is that callback function of the dummy-animation looking exactly? Found this post, when I was searching on how I make a Sprite visible for like 3 sec, invisible for 1 sec, … looping

There are several ways to achieve a kind of timer with a callback. One is to use an empty “dummy” game object and use go.animate to animate for instance the position and then utilise the callback function in go.animate to execute the delayed code. Example:

local function delay(seconds, fn)
	go.animate("dummy_go", "position.z", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_LINEAR, seconds, 0, fn)
end

local show
local hide

show = function()
	msg.post("#sprite", "enable")
	delay(3, hide)
end

hide = function()
	msg.post("#sprite", "disable")
	delay(1, show)
end

function init(self)
	show()
end

Another method for delayed function calls

You can also modify it to setup a scheduled function call on a set interval (cron like)

1 Like

Thanks britzl, your example works fine and after playing around for a while I actually start to know what im doing in defold a little bit better :smiley:
Nevertheless once I need to hand over arguments in the delayed function, it just gets executed immediately without a delay :confused:

go.property("wait", 0)
local function wait(seconds, fn)
	go.animate("#", "wait", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_LINEAR, seconds, 0, fn)
end

local show
local hide

show = function(testargument)
	print("test", testargument)
	delay(3, hide(5))
end

hide = function(testargument)
	print("test", testargument)
	delay(1, show(5))
end

function init(self)
	show(5)
end

Hi @peterH!

I see two things:

  1. You renamed “delay” to “wait”, but are still calling “delay(…)” :wink:

The “hide(5)” calls the function immediately and returns the result of that function (which is nil). It’s not a reference to the function. If you compare it to @britzl’s example, you see that he passes in the function references, not the return values of the functions.

I’m not sure what a good way of passing extra values would be in this case. Do you have a specific use case you are trying to solve?

1 Like

Thanks @Mathias_Westerdahl
Woops, the renaming error happened when I was breaking the code down for the forum code-segment.
I just got it working, for others dealing with the same issue:

go.property("wait", 0)
local function wait(seconds, fn, argument)
	go.animate("#", "wait", go.PLAYBACK_ONCE_FORWARD, 0, go.EASING_LINEAR, seconds, 0, function()
	fn(argument)
	end)
end

local show
local hide

show = function(testargument)
	print("test", testargument)
	wait(3, hide, testargument)
end

hide = function(testargument)
	print("test", testargument)
	wait(1, show, testargument)
end

function init(self)
	show("testargument")
end

You can pass the function including arguments like in the wait-function in this example.
(You could also do it like this in the hide/show functions)

1 Like

Hi. How can I call it in my function?

1 Like

You should use timer.delay() now.

Thanks.