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
17 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 (https://github.com/Insality/defold-tweener?tab=readme-ov-file#tweenertween)

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

2 Likes