Gui.animate end callback run too early? (SOLVED)

I’m trying to animate the alpha of a button, but i see the end_animation is executed while the button is still changing alpha, why?
I tried setting very high delay and duration to see this clearly

local function end_animation(self,node,value)
	-- store the node-ID in table with value TRUE (means, animation is finished!)
		
		 self.animatedbuttons[gui.get_id(node)] = value
		 print("end_anim")
		 print(value)
	
end

local function animate_back_button(self, node)
if self.animatedbuttons[gui.get_id(node)] == nil then
	
		self.animatedbuttons[gui.get_id(node)] = false
local to_color = gui.get_color(node)
to_color.w = 1.0
gui.animate(node, gui.PROP_COLOR, to_color, gui.EASING_IN, 1.4,0,  end_animation(self,node,true) ,gui.PLAYBACK_ONCE_FORWARD )

end
end

Ah, you’re making a classic mistake. You’re actually calling end_animation() immediately instead of passing it as an argument to be called when the animation has finished. This is the correct line of code:

gui.animate(node, gui.PROP_COLOR, to_color, gui.EASING_IN, 1.4,0, end_animation, gui.PLAYBACK_ONCE_FORWARD )

Sorry for that, so if i want to pass a value to the function i should include it in self right?

Ah, yes, or wrap it in an anonymous function:

gui.animate(node, gui.PROP_COLOR, to_color, gui.EASING_IN, 1.4,0, function() end_animation(self, node, true) end, gui.PLAYBACK_ONCE_FORWARD )
1 Like

Many thanks!

hi, if this is a classic mistake it would be helpful to add this info in the documentation. came here for the exact same problem because it is not very obvious that it is a mistake…

Well, it’s a classic mistake if you are not familiar with Lua when you pass a function as an argument. This goes for all of the API functions that takes a callback function as an argument, not only gui.animate.

The example in the API docs shows the correct way: https://www.defold.com/ref/gui/#gui.animate:node-property-to-easing-duration--delay---complete_function---playback- Maybe we should also specifically warn about this?