From d3ad99d3d1adbb08649b41fc1d07425a5f2fd79b Mon Sep 17 00:00:00 2001 From: rune-scape Date: Sat, 21 Sep 2024 21:56:11 -0700 Subject: [PATCH] GDScriptNativeClass: Allow getting static function as callable --- modules/gdscript/gdscript.cpp | 11 +++++++++-- .../features/native_static_method_as_callable.gd | 8 ++++++++ .../features/native_static_method_as_callable.out | 3 +++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.out diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 7b9aa70686a..6979373ad34 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -76,9 +76,16 @@ bool GDScriptNativeClass::_get(const StringName &p_name, Variant &r_ret) const { if (ok) { r_ret = v; return true; - } else { - return false; } + + MethodBind *method = ClassDB::get_method(name, p_name); + if (method && method->is_static()) { + // Native static method. + r_ret = Callable(this, p_name); + return true; + } + + return false; } void GDScriptNativeClass::_bind_methods() { diff --git a/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.gd b/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.gd new file mode 100644 index 00000000000..63d5935d1e6 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.gd @@ -0,0 +1,8 @@ +func get_parse_string(t: Variant): + return t.parse_string + +func test(): + var a: Callable = JSON.parse_string + var b: Callable = get_parse_string(JSON) + prints(a.call("{\"test\": \"a\"}"), a.is_valid()) + prints(b.call("{\"test\": \"b\"}"), b.is_valid()) diff --git a/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.out b/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.out new file mode 100644 index 00000000000..a2cb4b9a071 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/native_static_method_as_callable.out @@ -0,0 +1,3 @@ +GDTEST_OK +{ "test": "a" } false +{ "test": "b" } false