Callback function in go creating by factory and use go.animate bug

local function callback ()
 print "hi"
end
......
function init(self)
 local obj = factory.create("#factory", go.get_position())
 go.animate(obj, "scale", go.PLAYBACK_ONCE_FORWARD, self.end_scale, go.EASY_LINEAR, self.live_time, 0, callback)
 go.delete()
end

callback not called

That is not the way it designed I’m afraid. The callback is called when the animation is completed, thus reaching a unit time of 1.

You delete the game objects immediately after starting an animation. Then the object gets removed, and of course also its properties and functions, so it makes sense to stop its animations (no property to animate, and no function to call)

It is described in the documentation, albeit a bit fuzzy, only mentioning starting an animation w/callback in the “final” function. But the same logic applies.

@sicher, perhaps we can clarify this somehow in the documentation?

I was about to give a similar answer, bit noticed that it’s not the spawned “obj” that is deleted. However, when the go is deleted I think the callback function is deleted too. I’ll do some tests to verify.

In this case it seems better to start the animation from the spawned GO:s init() function.

[EDIT: See below]

1 Like

This seems like a bug. A bit clearer example perhaps:

function init(self)
	local pos = vmath.vector3(100, 100, 0)
	local spawn = factory.create("#factory", pos)
	go.animate(spawn, "scale", go.PLAYBACK_ONCE_FORWARD, 2, go.EASING_LINEAR, 1, 0, function()
		print("hello")
	end)
	go.delete() -- Delete myself, not the spawned "obj".
end

The animation plays all the way to the end but the callback is not called.

1 Like

I just found the same problem here, but with flip animation:


But in my case I have no crash.

Is this really a bug? The anonymous function is created within the scope of the script component that gets deleted. I would actually not expect the callback to get invoked in that case.

Do you get the same behavior if you use a global callback function or a callback function declared in a module? If yes, then I’m also inclined to call this a bug.

1 Like

Yes.If you provide “print” as callback you get the same behavior.