From e922e49b138a445a1605b9ad735f3054b3397aaf Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Wed, 9 Nov 2022 17:59:35 +0300 Subject: [PATCH] [3.x] GDScript: Fix `get_method_list` for custom functions --- modules/gdscript/gdscript.cpp | 33 +++++++++++++++++++------- modules/gdscript/gdscript_compiler.cpp | 13 ++++++++++ modules/gdscript/gdscript_function.h | 13 ++++++---- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index bde5cb1deaa..c55715df3fc 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -223,11 +223,15 @@ void GDScript::get_script_method_list(List *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); } @@ -276,11 +280,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; } @@ -1166,11 +1174,18 @@ void GDScriptInstance::get_method_list(List *p_list) const { const GDScript *sptr = script.ptr(); while (sptr) { for (Map::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); } diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 044e6394612..89a2d2b8b82 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -1628,7 +1628,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 argnames; + Vector default_arg_values; +#endif 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++) { _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(p_func->default_values[i]); + if (assign->arguments.size() >= 2 && assign->arguments[1]->type == GDScriptParser::Node::TYPE_CONSTANT) { + const GDScriptParser::ConstantNode *cn = static_cast(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()) { diff --git a/modules/gdscript/gdscript_function.h b/modules/gdscript/gdscript_function.h index de34f1b91da..a77094c31fd 100644 --- a/modules/gdscript/gdscript_function.h +++ b/modules/gdscript/gdscript_function.h @@ -264,6 +264,7 @@ private: #ifdef TOOLS_ENABLED Vector arg_names; + Vector default_arg_values; #endif List 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);