Safely check properties (stupid race conditions, grr)

Godot Version

4.3 rc3

Question

How do you check a property of a non-existent object?

When you kill an enemy i hide the sprite, play the death screams, wait for the screaming to end then queue_free. My shooting code looks like this:

func _physics_process(_delta: float):
    if target and target.i_am:

9 times out of 10 this works great. But that 10th time is all Invalid access to property or key 'i_am' on a base object of type 'previously freed'.

Stupid thing is dying in the middle of a line. i verified that and in GDScript short circuits but just to be safe i also tried breaking that into two lines to forcefully ensure short circuiting and it’s not that.

Unsurprisingly, this only happens on my fast tower (10 shots/second).

How do people handle this?

I’m not 100% sure, but I believe this it what is_instance_valid(object) is for:

Would like to hear if this solves your issue.

1 Like

Let’s stress this out. Instead of 1 fast tower, use 5, placed point blank on the spawn spot. New code:

func _physics_process(_delta: float) -> void:
	#if target and target.i_am:
	if is_instance_valid(target) and target.i_am:

Repeat 6 times new code, 6 times old.

…well hot damn, that worked! Both versions work the majority of the time but the second one hasn’t failed yet. Good catch, trizZzle!

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