WHAT ARE ACTIONS?
Actions are
Nodes, that execute a function, when they are called.
Actions can be normal members of Nodes, which can be assigned in the editor or via code.
Typically, a Node would call an Action when an event happened.
For instance a Node could process input and when a special input occurs, it calls the Action. Imagine
a sword attack on button press:
OnSensor
sensor: CombineSensor = CombineSensor "Sword Button"
* onStart: Action = ActionList "On Sword Attack"
* onActive: Action
* onEnd: Action
* onDouble: Action
Inspector of the OnSensor Node.
The highlighted members are Actions.
On Sword Attack is a special Action called ActionList.
When this Action is triggered, it triggers all other direct child Actions in it's hierarchy.
So when the sword button pressed, the ActionList On Sword Attack executes
Play Particles, Play Sound and the Collision Shape Sequence
OnSensor: Sword Button Interaction (OnSensor)
ActionList: On Sword Attack
PlayParticles: Play Particles
PlaySound: Play Sound
ActionSequence: Collision Shape Sequence
Action: Activate Sword Collision Shape
Delay: Keep Shape Active for 0.5 sec
Action: Deactivate Sword Collision Shape
Example scene hierarchy of nested actions. On Sword Attack does not need be a child of
Sword Button Interaction, but it is easier to understand.
WHEN SHOULD I USE ACTIONS?
Actions are useful to separate logic from state changes/effects. Every time a game object does something
purely as side effect, for instance as feedback, Actions are a good fit. This includes visual fx and
audio fx, but also tweens and async sequences to animate things.
Another use case can be simple state changes, like opening a door or loading the next level, marking something as complete.
A key rule is that the purpose should become clear by just looking on the scene hierarchy and good labels for the actions.
In the example below, almost every body should be able to understand what will happen, without knowing the implementation details.
GOOD EXAMPLE
CharacterBody3D: Enemy
ActionList: On Hit Actions
Flash: White Blinking
PlaySound: Play Death Sound
PlayParticles: Animate Death Cloud
ActionSequence: Removal Hook
SetPhysicsState: Disable Physics
Delay: Wait a bit for the fx
RemoveNode: Remove Enemy Root Node from Scene
Do not implement logic with Actions. Although they can manage states, logic should be done
in code or other tools.
Some other pit fal when using Actions is the possibility to make too many indirections for the targets of the
performant action. Sometimes you have to use abstract/run-time references, but ideally it would be resolved early, so
that is clear, what an Action does.
BAD, DON'T DO THIS!
Action: Complex State Change
ActionList: Unnamed action list
Action: Set Manager Variable
Action: Increment Variable
ActionReference: * On Increment
Action: On Increment
ConditionalAction: If Recursive
ActionReference: * Unnamed action list
HOW DO I USE ACTIONS?
Actions are created in the scene hierarchy and than assigned by dragging an Action
to a Node.
Adding actions to a node is straight forward by adding an Action member
and calling the Trigger method of the Action.
C#:
using Godot;
using Rokojori;
[Tool, GlobalClass]
public partial class MyLogic:Node
{
[Export]
public Action onSomethingHappened;
public override void _Process( double delta )
{
if ( SomethingHappend() )
{
onSomethingHappened.Trigger();
}
}
}
GDScript:
@tool
class_name MyLogic extends Node
@export
var onSomethingHappened: Action;
func _process( delta: float ) -> void:
if something_happened():
onSomethingHappened.Trigger();
## ---
WHERE CAN I FIND ACTIONS?
Actions can be found in the Scene Hierarchy with Add Child Node... and then
searching for Actions. They usually have a yellow icon, often a star. Here some examples:
ActionList
ActionSequence
Parallel
Repeat
PlayParticles
PlaySound
PlayMusic
RemoveNode
The Rokojori documentation has filters to check for
various types, there's also one for all Actions and for special ones.
Here are some examples of the Animation filter:
Highlight
PlayCameraEffect
Flash
TweenAudio
TweenLight
TweenMaterial
TweenParticles
TweenPosition
HOW CAN I WRITE MY OWN ACTIONS?
You can write custom Actions by extending the Action class and implementing
the function _OnTrigger, which is called when the action is triggered.
There's also a dedicated page with code examples:
How to Write Custom Actions