Skip to content
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

Core: Fix Object::has_method() for script static methods #82767

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,16 @@ bool Object::has_method(const StringName &p_method) const {
}

MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
if (method != nullptr) {
return true;
}

return method != nullptr;
const Script *scr = Object::cast_to<Script>(this);
if (scr != nullptr) {
return scr->has_static_method(p_method);
}

return false;
}

Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
Expand Down
3 changes: 3 additions & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ class Script : public Resource {
virtual PropertyInfo get_class_category() const;
#endif // TOOLS_ENABLED

// TODO: In the next compat breakage rename to `*_script_*` to disambiguate from `Object::has_method()`.
virtual bool has_method(const StringName &p_method) const = 0;
virtual bool has_static_method(const StringName &p_method) const { return false; }

virtual MethodInfo get_method_info(const StringName &p_method) const = 0;

virtual bool is_tool() const = 0;
Expand Down
1 change: 1 addition & 0 deletions core/object/script_language_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void ScriptExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_class_icon_path);

GDVIRTUAL_BIND(_has_method, "method");
GDVIRTUAL_BIND(_has_static_method, "method");
GDVIRTUAL_BIND(_get_method_info, "method");

GDVIRTUAL_BIND(_is_tool);
Expand Down
1 change: 1 addition & 0 deletions core/object/script_language_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ScriptExtension : public Script {
#endif // TOOLS_ENABLED

EXBIND1RC(bool, has_method, const StringName &)
EXBIND1RC(bool, has_static_method, const StringName &)

GDVIRTUAL1RC(Dictionary, _get_method_info, const StringName &)
virtual MethodInfo get_method_info(const StringName &p_method) const override {
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/ScriptExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@
<description>
</description>
</method>
<method name="_has_static_method" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="method" type="StringName" />
<description>
</description>
</method>
<method name="_inherits_script" qualifiers="virtual const">
<return type="bool" />
<param index="0" name="script" type="Script" />
Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ bool GDScript::has_method(const StringName &p_method) const {
return member_functions.has(p_method);
}

bool GDScript::has_static_method(const StringName &p_method) const {
return member_functions.has(p_method) && member_functions[p_method]->is_static();
}

MethodInfo GDScript::get_method_info(const StringName &p_method) const {
HashMap<StringName, GDScriptFunction *>::ConstIterator E = member_functions.find(p_method);
if (!E) {
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class GDScript : public Script {

virtual void get_script_method_list(List<MethodInfo> *p_list) const override;
virtual bool has_method(const StringName &p_method) const override;
virtual bool has_static_method(const StringName &p_method) const override;
virtual MethodInfo get_method_info(const StringName &p_method) const override;

virtual void get_script_property_list(List<PropertyInfo> *p_list) const override;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GH-79521

class_name TestStaticMethodAsCallable

static func static_func() -> String:
return "Test"

func test():
var a: Callable = TestStaticMethodAsCallable.static_func
var b: Callable = static_func
prints(a.call(), a.is_valid())
prints(b.call(), b.is_valid())
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
Test true
Test true