GDScript: Fix get_method_list for custom functions

This commit is contained in:
Danil Alexeev 2022-11-09 17:59:35 +03:00 committed by Relintai
parent 92a8e047e0
commit 0c92de12c1
3 changed files with 46 additions and 13 deletions

View File

@ -222,11 +222,15 @@ void GDScript::get_script_method_list(List<MethodInfo> *p_list) const {
GDScriptFunction *func = E->get();
MethodInfo mi;
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();
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);
}
@ -275,11 +279,15 @@ MethodInfo GDScript::get_method_info(const StringName &p_method) const {
GDScriptFunction *func = E->get();
MethodInfo mi;
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();
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;
}
@ -1165,11 +1173,18 @@ void GDScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
const GDScript *sptr = script.ptr();
while (sptr) {
for (RBMap<StringName, GDScriptFunction *>::Element *E = sptr->member_functions.front(); E; E = E->next()) {
GDScriptFunction *func = E->get();
MethodInfo mi;
mi.name = E->key();
mi.return_val = func->get_return_type();
mi.flags |= METHOD_FLAG_FROM_SCRIPT;
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);
}

View File

@ -1630,7 +1630,10 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
codegen.current_line = 0;
codegen.call_max = 0;
codegen.debug_stack = ScriptDebugger::get_singleton() != nullptr;
#ifdef TOOLS_ENABLED
Vector<StringName> argnames;
Vector<Variant> default_arg_values;
#endif
int stack_level = 0;
@ -1695,6 +1698,15 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
for (int i = 0; i < p_func->default_values.size(); i++) {
_parse_expression(codegen, p_func->default_values[i], stack_level, true);
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();
@ -1742,6 +1754,7 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
#ifdef TOOLS_ENABLED
gdfunc->arg_names = argnames;
gdfunc->default_arg_values = default_arg_values;
#endif
//constants
if (codegen.constant_map.size()) {

View File

@ -264,6 +264,7 @@ private:
#ifdef TOOLS_ENABLED
Vector<StringName> arg_names;
Vector<Variant> default_arg_values;
#endif
List<StackDebug> stack_debug;
@ -336,12 +337,16 @@ public:
ERR_FAIL_INDEX_V(p_idx, arg_names.size(), StringName());
return arg_names[p_idx];
#else
return StringName();
return StringName("arg" + itos(p_idx));
#endif
}
Variant get_default_argument(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), Variant());
return default_arguments[p_idx];
Variant get_default_argument_value(int p_idx) const {
#ifdef TOOLS_ENABLED
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);