Can you animate a variable?

go.animate and gui.animate are great for property animations, but is there a way to animate one of your own variables?

I have a score variable that I want to animate over time, from one number to another over a specific duration.

Yes, you can animate any property, even ones you create - which can be used as variable:

go.property("my_variable", 0)

function init(self)
    go.animate(".", "my_variable", go.PLAYBACK_LOOP_PINGPONG, 100, go.EASING_OUTBOUNCE, 2)
end
1 Like

Is this true in GUI scripts too, where you need to specify a node?

No, there is nothing like go.property() for the gui.* namespace.

1 Like

I ended up solving this with the update function.

function update(self, dt)
	if not self.is_timer_converting then return end

	update_score(self, 100)
	update_time(self, 1)

	if self.time <= 0 then
		self.is_timer_converting = false
	end
end

I set a boolean to false on init, then set it to true to enable the animation. Once the value reached a certain amount, I set the boolean back to false to finish the animation.

This works on both go and gui namespaces, but might require more code if you want different easing curves.

I share the “tweener” for things like this here: Defold Eva - Overview

5 Likes