mustachebutter / GodotAutoSurvival

0 stars 0 forks source link

[BUG] Projectile object doesn't destroy itself when collided with the character #19

Closed mustachebutter closed 2 months ago

mustachebutter commented 4 months ago

Describe the bug Projectiles work fine for the first couple of shots. But the more it tries to hit the target consecutively, something didn't trigger and makes the projectile indestructible. Meaning the projectile will stuck colliding with the character while not applying any effects.

Expected behavior Should Dequeue itself properly.

Screenshots If applicable, add screenshots to help explain your problem.

https://github.com/mustachebutter/GodotAutoSurvival/assets/43947255/d4be7ec6-6067-48b8-9813-c8f5289d2f04

mustachebutter commented 2 months ago

When the bug happened, this is being called repeatedly. So start debugging from here BaseCharacter.cs#68

        if(IsDead && !_hasTriggeredOnDead)
        {
            GD.Print("In IS DEAD and should only be triggered once!");
            OnCharacterDeadEvent?.Invoke();
            _hasTriggeredOnDead = true;
        }
mustachebutter commented 2 months ago

So currently, it triggers more often when switching to another weapon (from Fireball to Zap). The theory is that, when Zap dealing damage, it could have lowered it enough for any Burn stacks left ticking and killing it (triggering the OnTargetDied). However, when trying to set the burn explosion's position to the target position. The Target is null and caused NullReferenceException, which shouldn't be possible since it would be QueueFree() after the fact.

mustachebutter commented 2 months ago

Further thoughts lead me to thinking:

mustachebutter commented 2 months ago
# Logs
Dead event: False
Removed enemy: True, Enemy
Dead event - 2: True
Dead event: False
Removed enemy: True, @CharacterBody2D@2
Dead event - 2: True
Dead event: False
Removed enemy: True, @CharacterBody2D@4
Dead event - 2: True
Dead event: False
Removed enemy: True, @CharacterBody2D@3
Dead event: False
Dead event: False
Dead event: False
mustachebutter commented 2 months ago

The error occurred because the status effect when expired only got removed from the list of status effect on the character rather than being unsubscribed to events that it had. When other types of weapons/damages dealt damage to the character. This will trigger OnCharacterDeadEvent -> also calling the OnTargetDied handler method in Burn because it is not being unsubscribed. So the godot editor was throwing NullException, causing the code that destroy the projectile freeze at the time