Actions Animation Library

Defold Actions

Small action/tween library for Defold: sequences, parallels, tweens for GO/GUI/table, waits, and function steps.

Note: I made this library to control animation timing myself, so I can skip or speed up actions. With Parallel/Sequence, it is much easier to build sequences than with go/gui animate callbacks.

Features

  • Sequence and Parallel actions.
  • Tween actions for GO, GUI, and tables.
  • Easing functions.
  • Optional lerp/slerp for vectors and quaternions.

Setup

Add this project as a Defold library dependency in game.project:

https://github.com/d954mas/defold-actions/archive/master.zip

You can also point to a specific release ZIP.

If you need to save script instances (contexts), add this dependency as well:

https://github.com/d954mas/LuaScriptInstance/archive/refs/heads/master.zip

Usage

Basic flow: create actions in init(), update them in update(dt).

local Sequence = require "actions.sequence_action"
local Parallel = require "actions.parallel_action"
local Wait = require "actions.wait_action"
local TweenGo = require "actions.tween_action_go"
local TWEEN = require "actions.tween"

function init(self)
    self.action = Sequence.new(false)

    local move_and_scale = Parallel.new(false)
    move_and_scale:add_action(TweenGo.new_noctx({
        object = ".",
        property = "position",
        to = vmath.vector3(200, 120, 0),
        time = 0.5,
        easing = TWEEN.easing.outCubic,
    }))
    move_and_scale:add_action(TweenGo.new_noctx({
        object = ".",
        property = "scale",
        to = vmath.vector3(1.2, 1.2, 1),
        time = 0.4,
        easing = TWEEN.easing.outBack,
    }))

    self.action:add_action(move_and_scale)
    self.action:add_action(Wait.new(0.1))
end

function update(self, dt)
    self.action:update(dt)
end

Tween examples (lerp/slerp)

GO position with easing (no lerp):

local TweenGo = require "actions.tween_action_go"
local TWEEN = require "actions.tween"

TweenGo.new_noctx({
    object = ".",
    property = "position",
    from = go.get_position(),
    to = vmath.vector3(200, 120, 0),
    time = 0.5,
    easing = TWEEN.easing.outCubic,
})

GO position/scale with lerp:

local TweenGo = require "actions.tween_action_go"
local TWEEN = require "actions.tween"

TweenGo.new_noctx({
    object = ".",
    property = "position",
    from = go.get_position(),
    to = vmath.vector3(200, 120, 0),
    time = 0.5,
    lerp = true,
})

TweenGo.new_noctx({
    object = ".",
    property = "scale",
    from = go.get_scale(),
    to = vmath.vector3(1.2, 1.2, 1),
    time = 0.4,
    lerp = true,
})

GO rotation with slerp:

local TweenGo = require "actions.tween_action_go"
local TWEEN = require "actions.tween"

TweenGo.new_noctx({
    object = ".",
    property = "rotation",
    from = go.get_rotation(),
    to = vmath.quat_rotation_z(math.rad(180)),
    time = 0.6,
    slerp = true,
})

GUI position with lerp:

local TweenGui = require "actions.tween_action_gui"
local TWEEN = require "actions.tween"

TweenGui.new_noctx({
    object = gui.get_node("w"),
    property = "position",
    from = gui.get_position(gui.get_node("w")),
    to = vmath.vector3(400, 520, 0),
    time = 0.5,
    lerp = true,
})

GUI rotation with slerp:

local TweenGui = require "actions.tween_action_gui"
local TWEEN = require "actions.tween"

TweenGui.new_noctx({
    object = gui.get_node("w"),
    property = "rotation",
    from = gui.get_rotation(gui.get_node("w")),
    to = vmath.quat_rotation_z(math.rad(30)),
    time = 0.4,
    slerp = true,
})

API

Main entry points:

  • actions.sequence_actionSequence.new(save_context)
  • actions.parallel_actionParallel.new(save_context)
  • actions.wait_actionWait.new(time)
  • actions.function_actionFunction.new(fun, save_context)
  • actions.coroutine_actionCoroutine.new(fun, save_context)
  • actions.function_steps_actionFunctionSteps.new(fun, save_context)
  • actions.tween_action_goTweenGo.new_noctx(config) / TweenGo.new_ctx(config)
  • actions.tween_action_guiTweenGui.new_noctx(config) / TweenGui.new_ctx(config)
  • actions.tween_action_tableTweenTable.new_noctx(config) / TweenTable.new_ctx(config)
  • actions.tweenTWEEN.easing.*

Function steps action

actions.function_steps_action calls your function each update with a step counter you can advance manually.
Since action library does not use coroutines internally, it is handy for actions you need to pause and resume, so you can use function_steps_action.

Tween config fields:

{
    object = ".",              -- go/ gui node / table
    property = "position",     -- property name or hash
    time = 0.5,                -- duration
    easing = TWEEN.easing.outCubic,
    from = vmath.vector3(0, 0, 0), -- optional
    to = vmath.vector3(100, 0, 0), -- optional
    by = { x = 10, y = 0, z = 0 }, -- optional
    delay = 0.1,               -- optional
    lerp = true,               -- optional
    slerp = true,              -- optional (quat only)
}

Sample Project

The sample project uses the same ideas:

  • main/main.script uses lerp for GO position/scale and slerp for GO rotation.
  • main/main.gui_script uses lerp for GUI position/scale and slerp for GUI rotation.
8 Likes