Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | DocoDan |
When my StateMachine is ready it enters “Idle” state which is set in the initial_state Variable.
“Idle”-State’s enter() function calls the Animation-Player from the FSM-parent, which is Player, playing the respective Animation.
But this is only possible if the FSM-enter_state() method which calls the enter_state() of the Child-State waits until Player is ready.
Otherwise the Animation-play function causes a Nill-Error.
I think I am missing some basic idea of the construction of a State Machine here.
Why is Parents Animation Player not ready by the time the Child State wants to
access it?
How could I get rid of the yield Statement?
class_name StateMachine
onready var target : Node = get_parent() as Node
export var initial_state : String
func _ready():
if initial_state != '':
current_state=get_node(initial_state)
for state in get_children():
state.pl = target
_states[state.name] = state
for state in get_children():
state._states = self._states
#Is there a way to avoid this?
#If I remove it I get nill for play function
yield(get_parent(),"ready")
enter_state()
func enter_state() -> void:
current_state.enter()
################################
#Idle State
extends State
var pl : KinematicBody2D
func enter():
next_state=null
pl._animation("Idle")
################################
#Player = parent
extends KinematicBody2
onready var anim = get_node("AnimationPlayer")
func _animation(name : String):
anim.play(name)