Improve error message when a GDScript instance fails to be constructed

Co-authored-by: Slashscreen <SlashScreen@users.noreply.github.com>
This commit is contained in:
A Thousand Ships 2024-01-09 13:32:22 +01:00
parent 2ccc5c7575
commit cae3b822c3
No known key found for this signature in database
GPG key ID: 2033189A662F8BD7

View file

@ -162,13 +162,14 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco
_super_implicit_constructor(this, instance, r_error); _super_implicit_constructor(this, instance, r_error);
if (r_error.error != Callable::CallError::CALL_OK) { if (r_error.error != Callable::CallError::CALL_OK) {
String error_text = Variant::get_call_error_text(instance->owner, "@implicit_new", nullptr, 0, r_error);
instance->script = Ref<GDScript>(); instance->script = Ref<GDScript>();
instance->owner->set_script_instance(nullptr); instance->owner->set_script_instance(nullptr);
{ {
MutexLock lock(GDScriptLanguage::singleton->mutex); MutexLock lock(GDScriptLanguage::singleton->mutex);
instances.erase(p_owner); instances.erase(p_owner);
} }
ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance."); ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance: " + error_text);
} }
if (p_argcount < 0) { if (p_argcount < 0) {
@ -179,13 +180,14 @@ GDScriptInstance *GDScript::_create_instance(const Variant **p_args, int p_argco
if (initializer != nullptr) { if (initializer != nullptr) {
initializer->call(instance, p_args, p_argcount, r_error); initializer->call(instance, p_args, p_argcount, r_error);
if (r_error.error != Callable::CallError::CALL_OK) { if (r_error.error != Callable::CallError::CALL_OK) {
String error_text = Variant::get_call_error_text(instance->owner, "_init", p_args, p_argcount, r_error);
instance->script = Ref<GDScript>(); instance->script = Ref<GDScript>();
instance->owner->set_script_instance(nullptr); instance->owner->set_script_instance(nullptr);
{ {
MutexLock lock(GDScriptLanguage::singleton->mutex); MutexLock lock(GDScriptLanguage::singleton->mutex);
instances.erase(p_owner); instances.erase(p_owner);
} }
ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance."); ERR_FAIL_V_MSG(nullptr, "Error constructing a GDScriptInstance: " + error_text);
} }
} }
//@TODO make thread safe //@TODO make thread safe