0 votes

Hello all, I'm wondering how to make async call requests that do not block the threads and execution of code in GDScript.

I'm making a plugin/addon that connects to my API server, however upon making the requests the whole game freezes and is not a proper feature in my opinion to have as part of my addon/plugin.

I've based my code on this from the docs:

I've seen other similar posts using low level threading nodes and using yield and resume however I can't find any good resources on how to correctly make async requests without pausing the execution/threads.

There is this while loop that has the line yield(Engine.get_main_loop(), "idle_frame") which doesn't seem to do anything different than not having it there.

while http.get_status() == HTTPClient.STATUS_REQUESTING:
    # Keep polling for as long as the request is being processed.
    if not OS.has_feature("web"):
        # Synchronous HTTP requests are not supported on the web,
        # so wait for the next main loop iteration.
        yield(Engine.get_main_loop(), "idle_frame")

I think the main issue are these while loops which is why I have a hunch that using a yield may be the best bet to get this fixed.

# Wait until resolved and connected.
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:

If you have any reference code on how to do async/ non-blocking HTTPClient requests, please share it.

in Engine by (18 points)

1 Answer

+1 vote

There's a node that does it, it's called HTTPRequest. You just connect to request_completed signal and call request() on it. All the async stuff is handeled there.

The code you posted will only use yield when exporting for WebGL so that might be why it does not make any difference.

by (1,511 points)

The connection stage of HTTPRequest.request() seems like synchronous.
In slow networks, on unreachable nodes etc., I have significant delays.

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.