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:
https://docs.godotengine.org/en/3.1/tutorials/networking/http_client_class.html

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.
    http.poll()
    print("Requesting...")
    if not OS.has_feature("web"):
        OS.delay_msec(500)
    else:
        # 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:
    http.poll()
    print("Connecting...")
    OS.delay_msec(500)

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

asked Jul 25 in Engine by hughes1992 (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.

answered Jul 25 by omggomb (231 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 How to use this Q&A? before posting your first questions.