# Can you animate a variable?

**URL:** <https://forum.defold.com/t/can-you-animate-a-variable/73361>\
**Category:** Questions\
**Created:** [May 25, 2023, 5:03am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361 "2023-05-25T05:03:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mako](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mako/32/32009_2.png) [@mako](https://forum.defold.com/u/mako)\
**Post date:** [May 25, 2023, 5:03am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/1 "2023-05-25T05:03:38Z")

</div>

`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.

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [May 25, 2023, 5:19am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/2 "2023-05-25T05:19:41Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![mako](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mako/32/32009_2.png) [@mako](https://forum.defold.com/u/mako)\
**Post date:** [May 25, 2023, 5:25am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/3 "2023-05-25T05:25:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [May 25, 2023, 7:22am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/4 "2023-05-25T07:22:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mako](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mako/32/32009_2.png) [@mako](https://forum.defold.com/u/mako)\
**Post date:** [May 25, 2023, 8:02am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/5 "2023-05-25T08:02:17Z")

</div>

I ended up solving this with the update function.

```lua
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.

---

<div class="post-metadata">

**Author:** ![Insality](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/insality/32/39737_2.png) [@Insality](https://forum.defold.com/u/Insality)\
**Post date:** [May 25, 2023, 9:32am UTC](https://forum.defold.com/t/can-you-animate-a-variable/73361/6 "2023-05-25T09:32:38Z")

</div>

> [@mako](#):
>
> `go.animate` and `gui.animate` are great for property animations, but is there a way to animate one of your own variables?

I share the “tweener” for things like this here: [Defold Eva - Overview](https://forum.defold.com/t/defold-eva-overview/73257#a-hrefhttpsgithubcominsalitydefold-evablobmasterevalibstweenerlua-target_blanktweenera-18)
