[3.x] GDScript: Fix get_method_list for custom functions

This commit is contained in:
Danil Alexeev 2022-11-09 17:59:35 +03:00
parent a81d96c637
commit e922e49b13
No known key found for this signature in database
GPG key ID: 124453E157DA8DC7
3 changed files with 46 additions and 13 deletions

View file

@ -223,11 +223,15 @@ void GDScript::get_script_method_list(List<MethodInfo> *p_list) const {
GDScriptFunction *func = E->get(); GDScriptFunction *func = E->get();
MethodInfo mi; MethodInfo mi;
mi.name = E->key(); mi.name = E->key();
for (int i = 0; i < func->get_argument_count(); i++) {
mi.arguments.push_back(func->get_argument_type(i));
}
mi.return_val = func->get_return_type(); mi.return_val = func->get_return_type();
for (int i = 0; i < func->get_argument_count(); i++) {
PropertyInfo arginfo = func->get_argument_type(i);
arginfo.name = func->get_argument_name(i);
mi.arguments.push_back(arginfo);
}
for (int i = 0; i < func->get_default_argument_count(); i++) {
mi.default_arguments.push_back(func->get_default_argument_value(i));
}
p_list->push_back(mi); p_list->push_back(mi);
} }
@ -276,11 +280,15 @@ MethodInfo GDScript::get_method_info(const StringName &p_method) const {
GDScriptFunction *func = E->get(); GDScriptFunction *func = E->get();
MethodInfo mi; MethodInfo mi;
mi.name = E->key(); mi.name = E->key();
for (int i = 0; i < func->get_argument_count(); i++) {
mi.arguments.push_back(func->get_argument_type(i));
}
mi.return_val = func->get_return_type(); mi.return_val = func->get_return_type();
for (int i = 0; i < func->get_argument_count(); i++) {
PropertyInfo arginfo = func->get_argument_type(i);
arginfo.name = func->get_argument_name(i);
mi.arguments.push_back(arginfo);
}
for (int i = 0; i < func->get_default_argument_count(); i++) {
mi.default_arguments.push_back(func->get_default_argument_value(i));
}
return mi; return mi;
} }
@ -1166,11 +1174,18 @@ void GDScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
const GDScript *sptr = script.ptr(); const GDScript *sptr = script.ptr();
while (sptr) { while (sptr) {
for (Map<StringName, GDScriptFunction *>::Element *E = sptr->member_functions.front(); E; E = E->next()) { for (Map<StringName, GDScriptFunction *>::Element *E = sptr->member_functions.front(); E; E = E->next()) {
GDScriptFunction *func = E->get();
MethodInfo mi; MethodInfo mi;
mi.name = E->key(); mi.name = E->key();
mi.return_val = func->get_return_type();
mi.flags |= METHOD_FLAG_FROM_SCRIPT; mi.flags |= METHOD_FLAG_FROM_SCRIPT;
for (int i = 0; i < E->get()->get_argument_count(); i++) { for (int i = 0; i < E->get()->get_argument_count(); i++) {
mi.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i))); PropertyInfo arginfo = func->get_argument_type(i);
arginfo.name = func->get_argument_name(i);
mi.arguments.push_back(arginfo);
}
for (int i = 0; i < func->get_default_argument_count(); i++) {
mi.default_arguments.push_back(func->get_default_argument_value(i));
} }
p_list->push_back(mi); p_list->push_back(mi);
} }

View file

@ -1628,7 +1628,10 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
codegen.current_line = 0; codegen.current_line = 0;
codegen.call_max = 0; codegen.call_max = 0;
codegen.debug_stack = ScriptDebugger::get_singleton() != nullptr; codegen.debug_stack = ScriptDebugger::get_singleton() != nullptr;
#ifdef TOOLS_ENABLED
Vector<StringName> argnames; Vector<StringName> argnames;
Vector<Variant> default_arg_values;
#endif
int stack_level = 0; int stack_level = 0;
@ -1693,6 +1696,15 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
for (int i = 0; i < p_func->default_values.size(); i++) { for (int i = 0; i < p_func->default_values.size(); i++) {
_parse_expression(codegen, p_func->default_values[i], stack_level, true); _parse_expression(codegen, p_func->default_values[i], stack_level, true);
defarg_addr.push_back(codegen.opcodes.size()); defarg_addr.push_back(codegen.opcodes.size());
#ifdef TOOLS_ENABLED
const GDScriptParser::OperatorNode *assign = static_cast<const GDScriptParser::OperatorNode *>(p_func->default_values[i]);
if (assign->arguments.size() >= 2 && assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) {
const GDScriptParser::ConstantNode *cn = static_cast<const GDScriptParser::ConstantNode *>(assign->arguments[1]);
default_arg_values.push_back(cn->value);
} else {
default_arg_values.push_back(Variant());
}
#endif
} }
defarg_addr.invert(); defarg_addr.invert();
@ -1742,6 +1754,7 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
gdfunc->arg_names = argnames; gdfunc->arg_names = argnames;
gdfunc->default_arg_values = default_arg_values;
#endif #endif
//constants //constants
if (codegen.constant_map.size()) { if (codegen.constant_map.size()) {

View file

@ -264,6 +264,7 @@ private:
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
Vector<StringName> arg_names; Vector<StringName> arg_names;
Vector<Variant> default_arg_values;
#endif #endif
List<StackDebug> stack_debug; List<StackDebug> stack_debug;
@ -336,12 +337,16 @@ public:
ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName()); ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
return arg_names[p_idx]; return arg_names[p_idx];
#else #else
return StringName(); return StringName("arg" + itos(p_idx));
#endif #endif
} }
Variant get_default_argument(int p_idx) const { Variant get_default_argument_value(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant()); #ifdef TOOLS_ENABLED
return default_arguments[p_idx]; ERR_FAIL_INDEX_V(p_idx, default_arg_values.size(), Variant());
return default_arg_values[p_idx];
#else
return Variant();
#endif
} }
Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = nullptr); Variant call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state = nullptr);