Skip to content
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

Equivalent for await $SomeTimer.timeout? #432

Closed
WHMHammer opened this issue Sep 28, 2023 · 6 comments
Closed

Equivalent for await $SomeTimer.timeout? #432

WHMHammer opened this issue Sep 28, 2023 · 6 comments
Labels
question Not a problem with the library, but a question regarding usage.

Comments

@WHMHammer
Copy link

I am following the Godot "Your first 2D game" tutorial. Part of the logic is to wait for a timer to timeout to proceed to the next step. Below are the GDScript codes:

func show_game_over():
    show_message("Game Over")
    # Wait until the MessageTimer has counted down.
    await $MessageTimer.timeout

    $Message.text = "Dodge the\nCreeps!"
    $Message.show()
    # Make a one-shot timer and wait for it to finish.
    await get_tree().create_timer(1.0).timeout
    $StartButton.show()

However, I did not find any equivalent function in the godot-rust Timer documentation. I also checked the example codes in this repo, which does not have this wait for timeout logic at all. The same issue exists on SceneTreeTimer.

What are the equivalents for await $SomeTimer.timeout in godot-rust? Thanks!

@Bromeon
Copy link
Member

Bromeon commented Sep 29, 2023

This is not possible at the moment. It would need async support, which is unfortunately quite a pain in Rust.

gdnative supports it though, it looks like this:
https://godot-rust.github.io/book/gdnative/recipes/async-tokio.html

So maybe we can reuse prior art here, but it's not a priority at the moment.

@jrockett6
Copy link
Contributor

jrockett6 commented Oct 3, 2023

@WHMHammer If you don't mind defining an extra #[func] that handles the awaited logic you can do this as a workaround for this particular case:

fn show_game_over(&self) {
    ...
    let mut timer = self.base.get_tree().unwrap().create_timer(1.0).unwrap();
    timer.connect("timeout".into(), self.base.callable("show_start_button"));
}

#[func]
fn show_start_button(&self) {
    self.get_node_as::<Button>("StartButton").show()
}

@Bromeon
Copy link
Member

Bromeon commented Oct 3, 2023

@jrockett6 that's actually a great idea. And in other news, I added Callable::from_fn() a few weeks ago, so you don't even need a #[func] but could technically pass any closure.

It's a bit more manual at the moment (you only get a variant slice), so #[func] might be easier, but eventually from_fn() should also support such signatures.

@WHMHammer
Copy link
Author

@jrockett6 @Bromeon I created additional Timers in Godot editor and set their timeout handlers to the logics after the delays. Thank you for your alternative solutions though!

@WHMHammer WHMHammer closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
@Bromeon Bromeon added the question Not a problem with the library, but a question regarding usage. label Oct 5, 2023
github-merge-queue bot pushed a commit that referenced this issue Dec 6, 2023
Implement timers called out in #432
@musjj
Copy link

musjj commented Mar 30, 2024

Shouldn't this stay open until we have proper async support?

@Bromeon
Copy link
Member

Bromeon commented Mar 30, 2024

@musjj There's #640 as well. But we haven't decided yet if we want to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Not a problem with the library, but a question regarding usage.
Projects
None yet
Development

No branches or pull requests

4 participants