Animating a gui node in helper function doesn't work

I have some code I want to re-use between menus, which involves a simple animation. However, when I call this function from the lua module, the animation fails to work. I am passing self and dt to the helper function to set the timing animation. If I run the same code during initialization within the gui menu code itself, the animation plays fine. The node play_flipbook and text setting functions work fine in the helper code, but the animation seems to not work. If I set the animation timing using non-dt multiplied values then I can see some animation, but the positioning doesn’t work (the objects move much further than specified.)

You need to elaborate on “the animation fails to work”. Does nothing happen? Do you get an error?

I’d also post the code you are using.

1 Like

Here’s the code:

function M.refresh_items(self, dt, item_container, item, itemtext)
	self.offset = 1
	for i = 1, #item_container do
		if item_container[i] > 0 then
			local graphic_name = get_graphic_for_item(i)
			local node = gui.get_node(item .. self.offset)
			local pos = gui.get_position(node)
			gui.animate(node, "position.y", pos.y + 8, gui.EASING_INEXPO, dt * 64, (self.offset + 1) % 2 * 16 * dt, nil, gui.PLAYBACK_LOOP_PINGPONG)
			local textnode = gui.get_node(itemtext .. self.offset)
			gui.play_flipbook(node, graphic_name)
			gui.set_text(textnode, "x " .. item_container[i])
			self.offset = self.offset + 1
		end
		if self.offset > 20 then break end
	end
	for i = self.offset, 20 do
		local node = gui.get_node(item .. i)
		local textnode = gui.get_node(itemtext .. i)
		gui.play_flipbook(node, "item_placeholder")
		gui.set_text(textnode, "")
	end
end

Everything works when I use this helper function except the line:

gui.animate(node, "position.y", pos.y + 8, gui.EASING_INEXPO, dt * 64, (self.offset + 1) % 2 * 16 * dt, nil, gui.PLAYBACK_LOOP_PINGPONG)

However the items don’t animate at all from the helper function. If I use the same code from the game object (but I explicitly state the item_container and other arguments after) in the init section then the items bounce up and down. I made a function since I have multiple screens where I want to display the items so I didn’t want to copy+paste and slightly modify this section of code.

Are you calling M.refresh_items() every frame? If so, it will initiate a new animation each frame. This will have the effect of overwriting the previous animation that you initiated. I think you’ll get one frame of animation out of it - but depending on the easing function you’ve picked it might be miniscule.

No, only once when initializing the menu. Thanks though I have run into that before, but it’s not the case this time :smiley:

I think I’m stumped then, unfortunately.

I thought perhaps you weren’t calling the module function from a gui script, but because the other gui functions work then you must be.

Do you get a sensible value for dt? You say you call it when initialising the menu but you must be calling it in update rather than init, so that you actually have a dt?

I’m actually a bit confused by your use of dt in determing the times here. If you’re using a fixed dt then there aren’t any issues other than it being a bit weird (you could just specify an actual length in seconds rather than multiplying by your fixed dt). If you’re allowing a variable dt, then the duration and delay of your animation could potentially be lengthened significantly if for whatever reason the frame you begun the animation took a long time (which I suppose is most likely to happen in your circumstance - i.e. loading a lot of things at initialisation).