Defold Tweener - Timer Based Defold Tween Library

logo

Tweener

Tweener - is a single file Lua module for the Defold game engine. It provides a way to handle manual tweening in your game.

Features

  • Tweening: Create tweens for any action your want.
  • Easing Functions: Provides a set of easing functions for different types of easings.
  • Custom Update Frequency: Option to define update frequency for the tween.
  • Callbacks: Callbacks for each tween update.

Setup

See the Defold-Tweener repository on Github for the Setup, Documentation, API and Use Cases

Usage

You can use Tweener to animate scoring text, for example:

tween_text

This animation can be created using the following code:

tweener.tween(tween_function, from, to, time, callback, [dt])
tweener.tween(gui.EASING_OUTCIRC, 0, 9999, 2.4, function(value, is_final_call)
	gui.set_text(text_score, "Score: " .. math.floor(value))

	if is_final_call then
		gui.set_scale(text_score, vmath.vector3(1.25, 1.25, 1))
		gui.animate(text_score, "scale", vmath.vector3(1, 1, 1), gui.EASING_OUTBOUNCE, 0.5)
	end
end)

You can obtain the value of the tween at any point in time with the tweener.ease function:

tweener.ease(tween_function, from, to, time, time_elapsed)
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0)    -- Returns 0
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.25) -- Returns 38.268343236509
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.5)  -- Returns 70.710678118655
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 0.75) -- Returns 92.387953251129
tweener.ease(gui.EASING_OUTSINE, 0, 100, 1, 1)    -- Returns 100
19 Likes

Once more useful library from you, thanks! :slight_smile:

So you use timer for easing, consider to cancel it when your collections or game objects released. It would be also nice to have an option for using function update(dt)

2 Likes

Thanks!

For manual updating you can use tweener.ease(...) to calculate values by yourself if required

1 Like

Released the Tweener update with custom easings support

Now we can use the array of numbers to describe the easings, example:

local custom_easing = {0, 0.2, 0.4, 0.8, 0.9, 1}

tweener.tween(custom_easing, 0, 100, 1.5, function(value, is_final_call)
	print("Tween value: " .. value)
end)

local value = tweener.ease(custom_easing, 0, 100, 1.5, 0.75)
6 Likes

Loving the ease of use, but is there a way to cancel a tween?

1 Like

Yea, the tweener.tween returns the timer_id handle (GitHub - Insality/defold-tweener: Timer Based Defold Tween Library)

So you can store it and cancel with timer.cancel(timer_id)

2 Likes

Think Tweener is so easy to use, but is there a way to pause a tween?

I think no, currently there is no way to pause and resume the tween

It uses timer.delay and respects only the collection proxy time step (Collection proxy manual)

1 Like

I created a local table in my new version of tweener with the tween_id and is_paused, and checked for this on timer.delay and it works a little treat. Thanks again!

1 Like

Yea, it can be easily modified to make kind of pause

If you wish, you also can make a PR with these changes if you like to improve Tweener

1 Like

Learning how to do a pull request is now on next week’s todo list. Thanks.

2 Likes

This is good intention! And also not so hard to do

In short steps:

  • Fork repository on Github to make your own copy
  • Apply/commit changes your changes
  • Press “contribute” on a page of your repository fork

Then you will be redirected for the PR request and all next discussions can be continued inside. While it will be awesome to keeping the code style and various approaches, it’s not mandatory since we can communicate or improve it together!

4 Likes

Thanks for the easy to follow steps, looking forward to making excuses for my poor coding next week.

1 Like

First attempt at contributing a little (hopefully not the last!). Not the best coder in the world, but it does work :slight_smile:

3 Likes

Thanks for this! I will take a deep look with comments a little bit later
Congratulations with the first PR :slight_smile:

3 Likes