Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | hughes1992 |
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.
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.