0 votes

There is a raycast colliding with another area2d, which is freed, because the unit was killed and removed by calling queue_free() apparently in the very time between line 3 and 4.

i would really like to use the colliding object in my code, so what can I do or is there a better way for doing this?

0:my code is the following:
1:func shoot():
2: if shootray.iscolliding():
3: var collider = shootray.getcollider()
4: if collider.getgroups().has("vulnerable"):
5: collider.cause
damage(amount)

in Engine by (12 points)

1 Answer

0 votes

I'd need to see more code to understand how that situation is arising, but on initial view, the question would be, why not queue_free() the collider after causing the damage?

I'm not the foremost expert in the inner workings of Godot, but I was under the impression that the queue_free() worked on a frame basis, so should only be actioned at the end of the frame, after all the scripts had completed, so I'm not sure how it's possible to happen between lines of the code.

Perhaps another avenue to explore is to check Object.is_queued_for_deletion() on the collder before trying to call it. However, this feels like a hack, I'd want to understand how it's possible that the collider can disappear between lines of code.

by (323 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.