Skip to content

Wait until object is freed #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

WebF0x
Copy link
Contributor

@WebF0x WebF0x commented Mar 11, 2024

I think it could be useful. For example, in my game I want to test that when I hit an enemy, then it dies and is freed. But I found it awkward to await on signals on this soon-to-be null object

Before

enemy.set_name('node_name')
enemy.set_linear_velocity(Vector2.LEFT * 1000)

# Have to wait, but it's hard to guess for how long and slows test runs
wait_seconds(10)
# Or create a custom signal just for the test. It works but gives this awkward error message ==> ERROR: Parameter "obj" is null.
await wait_for_signal(enemy.damaged, 10)

assert_freed(enemy, 'node_name')

After

enemy.set_name('node_name')
enemy.set_linear_velocity(Vector2.LEFT * 1000)

await wait_until_freed(enemy, 10)

assert_freed(enemy, 'node_name')

Copy link
Owner

@bitwes bitwes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great PR. Thank you. I was a little on the fence as to whether this feature was actually needed, but you've done ALL the work so I think this will make a nice addition. I have one question about the use of hash. Once that is squared away I'll be good to merge this.

@@ -22,6 +24,10 @@ func _physics_process(delta):
if(_elapsed_frames >= _wait_frames):
_end_wait()

var is_waiting_for_object_to_be_freed := hash(_object_waiting_to_be_freed) != hash(null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the use of hash when checking for null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With hindsight it does look weird. I probably hit a case where I needed to differentiate null and freed objects like mentioned in this forum post.

If _object_waiting_to_be_freed != null still passes all tests, it would sound simpler. Otherwise documenting why it has to be weird will help future readers!

@WebF0x
Copy link
Contributor Author

WebF0x commented Apr 27, 2024

Thanks! Hey let's hold on for this PR until we resolve #585, as it might be a more generic solution.
I don't have permission for it, but you can mark it as a "draft PR"

@WebF0x
Copy link
Contributor Author

WebF0x commented May 18, 2024

Closing in favor of: #609

@WebF0x WebF0x closed this May 18, 2024
@WebF0x WebF0x deleted the wait_until_object_is_freed branch May 18, 2024 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants