derkork / godot-statecharts

A state charts extension for Godot 4
MIT License
679 stars 33 forks source link

Nonexistant function "_run_transition" #89

Closed mrjshzk closed 4 months ago

mrjshzk commented 4 months ago

Hello! Sorry to bother you. I'm trying to make my enemy's AI using this plugin. Instead of having a big script in the root node or the character node, I am extending from the AtomicStates to manage my behaviours.

This is currently my wander code:

extends AtomicState

@onready var root :StateChart= %Root
@export var enemy : Enemy
@export var nav_agent : NavigationAgent3D
@onready var player : CharacterBody3D = get_tree().get_first_node_in_group("Player")
@onready var navmesh : NavigationRegion3D = get_tree().get_first_node_in_group("Navmesh")

func _ready():
    state_entered.connect(state_started)
    state_physics_processing.connect(physics_processing)
    nav_agent.navigation_finished.connect(finished_wander)

func state_started() -> void:
    print("started: ", self.name)
    var random_pos := Vector3(randf_range(-30,30), randf_range(-30,30), randf_range(-30,30)) + enemy.global_position
    enemy.set_target(random_pos)

func physics_processing(delta: float) -> void:
    if enemy.is_seen() == GameManager.SEEN_STATES.NOT_SEEN:
        print("not seeing player")
    else:
        print("was seen")
        root.send_event("was_seen")

func finished_wander() -> void:
    enemy.set_target(Vector3(randf_range(-30,30), randf_range(-30,30), randf_range(-30,30)) + enemy.global_position)

image image image

This send event is crashing, could you help me out if I'm doing anything incorrectly? Thanks.

derkork commented 4 months ago

The state classes are not designed or tested for being subclassed by user code, so you will need to be extra careful when working this way to not accidentally break functionality in the base classes.

Your particular problem seems to be caused by overriding _ready. The base State class also has a _ready implementation and if you override this it will not be called. This in turn skips some necessary initialization which leads to the error you are seeing. You should be able to fix this problem by calling super in your _ready function:

func _ready():
        super()  # call the base implementation to make sure everything is initialized
    state_entered.connect(state_started)
    state_physics_processing.connect(physics_processing)
    nav_agent.navigation_finished.connect(finished_wander)
mrjshzk commented 4 months ago

that was it :) thank you so much! and what a dumb little mistake :p