Callable.is_valid() is unreliable for native static method Callables and breaks Callable.bind()
Tested versions
- Reproducible in: Godot Engine v4.7.2.stable.official [ed1daf0bf]
- Not tested: earlier stable versions
- Not tested: development snapshots
Reproduced using an official Godot build without GDExtensions, C++ modules, or editor plugins.
System information
Godot v4.7.2.stable - Windows 11 (build 26200) - Multi-window, 2 monitors - Direct3D 12 (Forward+) - dedicated AMD Radeon RX 6900 XT (Advanced Micro Devices, Inc.; 32.0.21043.12001) - AMD Ryzen 9 5900X 12-Core Processor (24 threads) - 63.90 GiB memory - WASAPI (48000 Hz, Stereo/mono)
Issue description
I found that the result returned by Callable.is_valid() is unreliable. A Callable that can actually be called can return false.
This seems to happen specifically when Callable.get_object() returns a GDScriptNativeClass and the target method is static, such as the static methods in FileAccess.
var callable: Callable = FileAccess.file_existsThis is a Callable that can be called successfully, but is_valid() returns false.
Then, if we bind an argument to it:
callable.bind("res://icon.svg")the resulting Callable changes from callable to uncallable. Calling call() on it produces an error.
I found that the reason is that Callable::is_valid() uses the following code:
return get_object() && get_object()->has_method(get_method());For a GDScriptNativeClass object, has_method() returns false for this native static method.
However, Callable::callp() does not rely on is_valid(). Instead, it uses ClassDB::get_method("FileAccess", "file_exists") to find the method and execute it. This is why the original Callable can still be called successfully.
After binding arguments, the Callable changes to a custom Callable, so bound.is_custom() == true.
At this point, Callable::callp() checks is_valid() for the custom Callable and reports an error. As a result, the Callable with bound arguments can no longer be called.
My conclusion is that the result of Callable.is_valid() is not reliable. It does not accurately reflect whether a Callable can actually be called. Furthermore, this unreliable result causes a Callable that has arguments bound with bind() to become uncallable.
Minimal reproduction project
A minimal reproduction project is attached to this issue.
Source: godotengine/godot