[SOLVED] Gui.animate > callback function triggered when the anim starts (if parameter)

Happy new year guys, hope it’ll be a great one for Defold and everyone here!

And… here is my first issue of the year!
(this may not be a bug at all… you tell me)

When I want to use a parameter in the gui.animate’s callback function, this function is triggered not when the animation is over but when it starts :thinking:

function update(self, dt)
gui.animate(node_test, hash("position.y"), anim_pos_target_y, gui.EASING_OUTELASTIC, anim_duration, 0, function_test, gui.PLAYBACK_ONCE_FORWARD)
end

=> :white_check_mark: OK / function called when the anim ends

 function update(self, dt)
gui.animate(node_test, hash("position.y"), anim_pos_target_y, gui.EASING_OUTELASTIC, anim_duration, 0, function_test(value), gui.PLAYBACK_ONCE_FORWARD)
end

=> :x: KO / function called when the anim starts

In both cases, the function is called, but… in the second case (function parameter), not at the right moment.

The (super simple) local function looks like this:

local function function_test(value)
	print("value = "..value)
end

Have I done something wrong?

In the second case you are calling the function within the parameter, so the parameter is the output of the function (which ends up being nil).

You need to wrap it up in an anonymous function:

gui.animate(node_test, hash("position.y"), anim_pos_target_y, gui.EASING_OUTELASTIC, anim_duration, 0, function() function_test(value) end, gui.PLAYBACK_ONCE_FORWARD)
2 Likes

Klear is right. This is a feature of Lua. Here’s a codepad illustrating the principle:

https://defold.com/codepad/#?c=#cp_sprite&s1=DYewxghsAEBmCuA7MAXAliRcmo4g+hABQAOEAThALYCUAUNI9CeWoikQEQBiO6m0CNEjBgAUwAmnemMQS6dUCOzJ+WBKrz4ARqQrV6TZq3ZdemgduFRxUmXIUbcAtmg4BnMcFiGmDJkpQ0ABuFITQALwqzgQQ/oyBMKHkOpHRajpEAIwATADMvox0shJAA==

By default, the callback parameters are (self, node) as shown here:

3 Likes

Thank you @Alex_8BitSkull and @Klear, you made it really clearer for me!

3 Likes