godot-cpp icon indicating copy to clipboard operation
godot-cpp copied to clipboard

get_node() does not work at the start of _ready()

Open KasperJSdeVries opened this issue 2 years ago • 1 comments

Godot version

4.0.3.stable.official.5222a99f5

godot-cpp version

3a9118cb0dafef74773216c47a030c3a4a490747

System information

Ubuntu 22.10 x86_64

Issue description

It seems like the ready notification gets sent slightly too early, causing the scene tree to not be instiantiated fully when _ready() is called.

I was trying to rewrite the "Squash the Creeps" tutorial in c++ when I stumbled upon this. The following code was the problem:

void Main::_ready()  {
    auto retry = get_node<ColorRect>("UserInterface/Retry");
    retry->hide();
}

When I call something like print_tree() before get_node() it gets the node successfully.

Steps to reproduce

  1. Set up project with godot-cpp
  2. add an empty node to the scene
  3. add a child to that node
  4. create a new class in c++ that uses get_node() to get the child in the _ready() function
  5. change the type of the parent node to the custom class
  6. watch godot crash

Minimal reproduction project

N/A

KasperJSdeVries avatar Jun 02 '23 00:06 KasperJSdeVries

Thanks!

However, I'm not able to reproduce this issue. I tested by applying this patch to the test project in godot-cpp:

diff --git a/test/src/example.cpp b/test/src/example.cpp
index 6c5e736..ec7e3de 100644
--- a/test/src/example.cpp
+++ b/test/src/example.cpp
@@ -360,3 +360,8 @@ void Example::_input(const Ref<InputEvent> &event) {
                emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
        }
 }
+
+void Example::_ready() {
+       Label *label = get_node<Label>("Label");
+       label->hide();
+}
diff --git a/test/src/example.h b/test/src/example.h
index 7f1b5b2..ddf3026 100644
--- a/test/src/example.h
+++ b/test/src/example.h
@@ -132,6 +132,7 @@ public:
        // Virtual function override (no need to bind manually).
        virtual bool _has_point(const Vector2 &point) const override;
        virtual void _input(const Ref<InputEvent> &event) override;
+       virtual void _ready() override;
 };
 
 VARIANT_ENUM_CAST(Example::Constants);

But it still runs just fine for me!

Can you share a minimal reproduction project?

dsnopek avatar Jun 09 '23 21:06 dsnopek