Skip to content

GDScript: Fix Callable call error text #99150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dalexeev
Copy link
Member

extends Node

func _ready() -> void:
    print(load.bind([]).call())

Before:

Invalid type in function '@GDScript::load (Callable) (Callable)'. Cannot convert argument 1 from Callable to String.

After:

Invalid type in function '@GDScript::load (Callable)'. Cannot convert argument 1 from Array to String.

GDScriptFunction::_get_call_error() is an extension of Variant::get_call_error_text(), so a Callable version is needed for GDScript, just like Variant::get_callable_error_text(). Perhaps in the future we should refactor this.

godot/core/variant/variant.cpp

Lines 3629 to 3684 in cb411fa

String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
String err_text;
if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
int errorarg = ce.argument;
if (p_argptrs) {
err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected));
} else {
err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
}
} else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
} else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
} else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
err_text = "Method not found";
} else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
err_text = "Instance is null";
} else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
err_text = "Method not const in const instance";
} else if (ce.error == Callable::CallError::CALL_OK) {
return "Call OK";
}
String base_text;
if (p_base) {
base_text = p_base->get_class();
Ref<Resource> script = p_base->get_script();
if (script.is_valid() && script->get_path().is_resource_file()) {
base_text += "(" + script->get_path().get_file() + ")";
}
base_text += "::";
}
return "'" + base_text + String(p_method) + "': " + err_text;
}
String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
Vector<Variant> binds;
p_callable.get_bound_arguments_ref(binds);
int args_unbound = p_callable.get_unbound_arguments_count();
if (p_argcount - args_unbound < 0) {
return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
} else {
Vector<const Variant *> argptrs;
argptrs.resize(p_argcount - args_unbound + binds.size());
for (int i = 0; i < p_argcount - args_unbound; i++) {
argptrs.write[i] = p_argptrs[i];
}
for (int i = 0; i < binds.size(); i++) {
argptrs.write[i + p_argcount - args_unbound] = &binds[i];
}
return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
}
}

@dalexeev dalexeev added this to the 4.4 milestone Nov 12, 2024
@dalexeev dalexeev requested review from a team as code owners November 12, 2024 22:18
@dalexeev dalexeev force-pushed the gds-fix-callable-call-errror-text branch from 5def201 to c3155b2 Compare November 13, 2024 07:22
@Repiteo Repiteo modified the milestones: 4.4, 4.5 Feb 24, 2025
@Repiteo Repiteo merged commit 86415f1 into godotengine:master Jun 9, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Jun 9, 2025

Thanks!

@dalexeev dalexeev deleted the gds-fix-callable-call-errror-text branch June 9, 2025 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants