#How should I delete something in Godot?

13 messages · Page 1 of 1 (latest)

tired sky
#

I have a problem where whenever I wish to delete something, the game will crash. I get this error:
Attempt to call function 'is_in_group' in base 'null instance' on a null instance.
It occurs at this line:

if rayCast.is_colliding() and rayCast.get_collider().is_in_group("Zombie"):

The code doing the "deleting" in question is this:

func disable_collisions():
    if self.get_parent().has_node("HitboxComponent"):
        var hitbox = self.get_parent().get_node("HitboxComponent")
        if hitbox.get_child(0):
            await get_tree().create_timer(1.0).timeout
            get_parent_node_3d().queue_free()
vestal tendon
#

Try this:

if rayCast.is_colliding():
  if rayCast.get_collider().is_in_group("Zombie"):
tired sky
#

still crashes with same error

vestal tendon
strong rain
#

is_colliding() only updates to false after an object is deleted when the whole raycast is updated (either automatically in the next physics tick, or by calling force_raycast_update() to do it immediately)

#

if you are checking it in _process or something else that isn't tied to physics ticks (especially as _process can run more often than the physics 60 ticks per second), the information in the raycast may be out of date or inaccurate, like by it still thinking it's hitting something that doesn't exist anymore

#

if you need a workaround, check get_collider() != null instead of is_colliding()

#

since even if it thinks it's hitting an object, if it can't find that object to return it, it will just return null as if it's not colliding

tired sky
#

Thank you it worked! The Peashooter is still firing though but I think that's because I don't have any logic for firing to stop.

tired sky
strong rain
#

that's strange, it should always be up to date as raycasts update themselves immediately before _physics_process starts running in nodes

#

ah yeah the queue_free() is running after awaiting on a timer, so I guess it can end up deleting the between the raycast update and the script _physics_process stage