Skip to content
Open
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
3 changes: 3 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,9 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
"\tGroupID add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());"
)

if class_name == "SceneTree":
result.append("\tstatic SceneTree *get_singleton();")

if class_name == "Object":
result.append("\ttemplate <typename T>")
result.append("\tstatic T *cast_to(Object *p_object);")
Expand Down
8 changes: 8 additions & 0 deletions src/classes/low_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/file_access.hpp>
#include <godot_cpp/classes/image.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/worker_thread_pool.hpp>
#include <godot_cpp/classes/xml_parser.hpp>

#include <godot_cpp/core/object.hpp>

#include <godot_cpp/godot.hpp>

namespace godot {
Expand Down Expand Up @@ -64,4 +68,8 @@ const uint8_t *Image::ptr() {
return ::godot::gdextension_interface::image_ptr(_owner);
}

SceneTree *SceneTree::get_singleton() {
return Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop());
}

} // namespace godot
3 changes: 3 additions & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ func _ready():
# Test that we can access an engine singleton.
assert_equal(example.test_use_engine_singleton(), OS.get_name())

# Test that we can access the SceneTree singleton.
assert_equal(example.test_use_scene_tree_singleton(), true)

if godot_target_version["minor"] >= 4:
assert_equal(example.test_get_internal(1), 1)
assert_equal(example.test_get_internal(true), -1)
Expand Down
7 changes: 7 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/multiplayer_peer.hpp>
#include <godot_cpp/classes/os.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

using namespace godot;
Expand Down Expand Up @@ -266,6 +267,7 @@ void Example::_bind_methods() {
GDVIRTUAL_BIND(_do_something_virtual_with_control, "control");

ClassDB::bind_method(D_METHOD("test_use_engine_singleton"), &Example::test_use_engine_singleton);
ClassDB::bind_method(D_METHOD("test_use_scene_tree_singleton"), &Example::test_use_scene_tree_singleton);

ClassDB::bind_method(D_METHOD("test_get_internal_class"), &Example::test_get_internal_class);

Expand Down Expand Up @@ -759,6 +761,11 @@ String Example::test_use_engine_singleton() const {
return OS::get_singleton()->get_name();
}

bool Example::test_use_scene_tree_singleton() const {
SceneTree *scene_tree = SceneTree::get_singleton();
return scene_tree != nullptr && scene_tree == get_tree();
}

String Example::test_library_path() {
String library_path;
::godot::gdextension_interface::get_library_path(::godot::gdextension_interface::library, library_path._native_ptr());
Expand Down
1 change: 1 addition & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class Example : public Control {
GDVIRTUAL1(_do_something_virtual_with_control, Control *);

String test_use_engine_singleton() const;
bool test_use_scene_tree_singleton() const;

static String test_library_path();

Expand Down