Godot Version
Player:
extends CharacterBody2D
class_name Player
## Onready variables.
@onready var sprite : Sprite2D = $Sprite2D
@onready var bodyCol : CollisionShape2D = $BodyCollision
@onready var hitbox : Area2D = $Hitbox
@onready var healthComp : Node = $"Components/Health Component"
@onready var staminaComp : Node = $"Components/Stamina Component"
@onready var state_machine : State_Machine = $"State Machine"
## Export variables.
@export var SS : State
## Variables.
# Mouse stuff.
var mouse_pos : Vector2
# Movement.
var speed_walk : float = 300.0
var speed_run : float = 500.0
## Functions.
func _ready() -> void:
state_machine.init(self)
return
func _physics_process(delta : float) -> void:
mouse_pos = get_global_mouse_position()
state_machine.state_process()
move_and_slide()
return
Base State Machine:
extends Node
class_name State_Machine
var CS : State
## Put parent here. Create an init function that sets
## the parents for the State Machine and States.
var parent
## Change new state, if current state is different to new state,
## then apply exit rules, change current state to new state, and apply enter rules.
func state_change(state: State) -> void:
if CS:
CS.exit()
print(state)
CS = state
CS.enter()
## Apply state rules.
func state_process() -> void:
var NS : State = CS.process_events()
if NS:
state_change(NS)
NS = CS.process_physics(get_process_delta_time())
if NS:
state_change(NS)
return
## Force state change.
## NOTE: use only for switching to death states and things like that.
## Possibly could result in problems.
func state_forcechange(state: State) -> void:
CS = state
return
Player State Machine:
extends State_Machine
func init(parent: Player):
for child in get_children():
child.parent = parent
CS = parent.SS
Player Reg state:
extends State
func exit() -> void:
return
func enter() -> void:
return
func process_physics(delta) -> State:
return null
func process_events() -> State:
var running = parent.movement(true)
parent.look_at(parent.position + parent.velocity.normalized())
return null
Question
My state machine which worked before in another project, doesnt work when i use the init function to set the parent for the states.