Godot Version
4.3
Question
I’m working on a GDExtension addon and need to use HTTP Requests. How can I simulate async calls like in C# or GDScript?
I have been trying and am still trying cpr. The basic async calls do not behave asynchronously at least in Godot. The callbacks do work but are very limited as I can’t access global variables or functions.
So that has resorted me to using Godot’s HTTP client class. I just need a way to pause a frame asynchronously in a while loop. I can’t find anything other than just making and using my own threads (yikes).
Example of the default (non-callback) request.
void AuthenticationService::sign_in_with_username_password(const String &username, const String &password)
{
String url = AUTH_URL.utf8() + "/v1/authentication/usernamepassword/sign-in";
Dictionary dict;
dict["username"] = username;
dict["password"] = password;
String json_string = JSON::stringify(dict);
cpr::AsyncResponse response = cpr::PostAsync(cpr::Url{url.utf8()},
cpr::Header{{"ProjectId", project_id.utf8()},
{"UnityEnvironment", environment.utf8()},
{"Content-Type", "application/json"}},
cpr::Body{json_string.utf8()});
auto result = response.get(); // freezes game (response.wait() also)
if (result.status_code == 200)
{
sign_in_response->from_dict(JSON::parse_string(result.text.c_str()));
save_cache();
emit_signal("on_signed_in");
signed_in = true;
}
else
{
Ref<CoreExceptionContent> parsed_content = memnew(CoreExceptionContent(result.text.c_str()));
ERR_FAIL_MSG(parsed_content->get_message());
}
}