Godot 2.D Dungeon RPG course: Getting error after player death

When the player dies, I’m getting debugger errors on enemy AI trying to chase the missing player. This is my last GD.Print logs that I’m seeing trying to narrow down the issue:

My PlayerDeathState script which I don’t think is the issue as it’s fairly straightforward.

public partial class PlayerDeathState : PlayerState
{
    protected override void EnterState()
    {
        characterNode.AnimPlayerNode.Play(GameConstants.ANIM_DEATH);
        characterNode.AnimPlayerNode.AnimationFinished += HandleAnimationFinished;
    }
    private void HandleAnimationFinished(StringName animName)
    {
        GD.Print(characterNode.Name + " died!");
        characterNode.QueueFree();
    }
    protected override void ExitState()
    {
        characterNode.AnimPlayerNode.AnimationFinished -= HandleAnimationFinished;
    }
}

The GD print logs of the transition of states and enemy target value during said states.

Chasing: Player
Exiting Chase statePlayer
Entering Enemy attack state!
Enemy deals 50 damage! Player Health left: 50!
Enemy deals 50 damage! Player Health left: 0!
Enemy deals 50 damage! Player Health left: 0!
Player died!
Exiting Enemy attack state! Last target: Player
Chasing: Player

The chase enters state method:

protected override void EnterState()
    {
        characterNode.ChaseAreaNode.GetOverlappingBodies().Clear();
        target = characterNode.ChaseAreaNode.GetOverlappingBodies().First() as CharacterBody3D;
        if (target == null)
        {
            GD.Print("No chase target at enter state");
            characterNode.StateMachineNode.SwitchState<EnemyReturnState>();
        }

        GD.Print("Entering Chase state: " + target.Name);
        characterNode.AnimPlayerNode.Play(GameConstants.ANIM_MOVE);
        chaseTimerNode.Timeout += HandleChaseTimeOut;
        characterNode.AttackAreaNode.BodyEntered += HandleAttackAreaBodyEntered;
        characterNode.ChaseAreaNode.BodyExited += HandleChaseAreaBodyExited;
    }

This is the debugger error repeatedly as Enemy AI keep ‘chasing the player’

E 0:00:06:0155   GodotObject.base.cs:73 @ nint Godot.GodotObject.GetPtr(Godot.GodotObject): System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Player'.
  <C# Error>     System.ObjectDisposedException
  <C# Source>    /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs:73 @ nint Godot.GodotObject.GetPtr(Godot.GodotObject)
  <Stack Trace>  GodotObject.base.cs:73 @ nint Godot.GodotObject.GetPtr(Godot.GodotObject)
                 Node3D.cs:506 @ Godot.Vector3 Godot.Node3D.GetGlobalPosition()
                 Node3D.cs:232 @ Godot.Vector3 Godot.Node3D.get_GlobalPosition()
                 EnemyChaseState.cs:39 @ void EnemyChaseState.HandleChaseTimeOut()
                 Callable.generics.cs:39 @ void Godot.Callable.<From>g__Trampoline|1_0(object, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 DelegateUtils.cs:62 @ void Godot.DelegateUtils.InvokeWithVariantArgs(nint, System.Void*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant*)

Not sure why Player is still being detected despite being freed. I’ve been trying to narrow down why this is happening. Any help or assistance would be appreciated. Thanks!

https://github.com/notme84/DungeonRPG I’ll just share the whole project if there is something else that could be causing it.

Whelp after a few hours of debugging and comparing to the lecture project code. I had extra signal for when player leaves the attack area that was source of the issue which I was using for my transition to Chase state; but it was not necessary. So removing that signal transition fixed my issue.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms