Godot Version
v4.4.dev.custom_build
Question
I have a very simple animation state machine. I use for transitions between scenes:
“slash in” state is played before the level is loaded. After that I call “slash out” and it works fine.
But I want to add more animations and expand this state machine.
Ideally I want to call _playback.next()
instead of _playback.travel("slash out")
, so the code is more general and I don’t need to know the name of the next state. But looks like it doesn’t work as I need.
What is the purpose of _playback.next()
? Am I trying to use it right?
Here’s my setup and code:
extends Node2D
const _green = "res://scenes/green.tscn"
const _yellow = "res://scenes/yellow.tscn"
const _blue = "res://scenes/blue.tscn"
const _red = "res://scenes/red.tscn"
var _loading_scene
var _current_scene_name = _green
var _animation_in_process = false
@onready var _anim_tree: AnimationTree = $Transitions/AnimationTree
@onready var _current_scene: Node2D = $Green
@onready var _loader: AnimationPlayer = $Loading/Anim
@onready var _playback: AnimationNodeStateMachinePlayback = _anim_tree.get("parameters/playback")
func _process(delta: float) -> void:
if _animation_in_process:
return
if _loading_scene == null:
_loader.stop()
if _current_scene_name != _green and Input.is_action_pressed("green"):
load_scene(_green)
elif _current_scene_name != _yellow and Input.is_action_pressed("yellow"):
load_scene(_yellow)
elif _current_scene_name != _blue and Input.is_action_pressed("blue"):
load_scene(_blue)
elif _current_scene_name != _red and Input.is_action_pressed("red"):
load_scene(_red)
else:
match ResourceLoader.load_threaded_get_status(_loading_scene):
ResourceLoader.THREAD_LOAD_FAILED:
push_error("scene failed to load: " + _loading_scene)
_loading_scene = null
_loader.stop()
ResourceLoader.THREAD_LOAD_LOADED:
_current_scene_name = _loading_scene
change(ResourceLoader.load_threaded_get(_loading_scene))
_loading_scene = null
_animation_in_process = true
_playback.travel("slash out") # ideally use .next() here
_loader.stop()
func load_scene(scene: String) -> void:
_anim_tree.active = true
_animation_in_process = true
_playback.travel("Start") # ideally use .next() here
_loader.play("loading")
_loading_scene = scene
ResourceLoader.load_threaded_request(scene)
func change(resource: Resource) -> void:
if resource is PackedScene:
var scene = resource.instantiate()
add_child(scene)
_current_scene.queue_free()
_current_scene = scene
func _anim_finished(anim_name: StringName) -> void:
_animation_in_process = false