+1 vote

cross-posted to the forum: Forum Post

Hello there!

I have a very basic problem while starting out with Godot, but maybe someone can help out and in the process also help others while building up some documented knowledge on the topic of C++ usage in Godot.

Thank you and in the following is my problem:

Principal Question

How do I properly create and spawn objects from C++?

Context and Description

I created a custom C++ module (following the documentation 1) where I implemented a test class which derives from "Node" (or "Spatial") from where I intent to procedurally generate a scene.

However the created scene objects are not integrated into the Godot-Editor/-Scene properly:

  • I have a test class derived from Node
  • The test class has a member attribute root_ of type "Spatial" which should act as the visible portion (scene root)
  • A "CSGSphere" object is attached to the root_ node

1 custommodulesin_cpp

Problem

  • If I - in the Godot Editor - add the test object as the scene root everything seems to be as I expect it to be (see screenshot godot1asroot)
  • If I attach the test object as a child node to some other node (see screenshot godot2assub-node):

    • its child-hierachy is not listed in the editor
    • the CSGSphere is rendered inside the viewport
    • the CSGSphere is not selectable from the viewport

Questions

  • How do I properly create/spawn objects via C++?
  • What is my specifc mistake?
  • What exactly is the "owner" property of objects as compared to the "parent-child" relationships?
  • Extension to the previous question: Why is the order of invoking addchild() and setowner() relevant?

Attachments

a) The object hierachy

These are the same for both described cases. Obtained by calling printtreepretty():

  • TEST1

    • TESTSCENEROOT

      • sphere1

b) Source Code

// test.h
class test : public Node {
GDCLASS(test, Node);

private:
    Spatial* root_;
    void _notification(int _p_notification); // <<-- calls _ready()
    void _ready();

protected:
    ...

public:
   ...
};

// test.cpp 
void test::_ready() {
    std::cout << "test::_ready()" << std::endl;

    root_ = memnew( Spatial );
    root_->set_name("TEST_SCENE_ROOT");
    root_->set_visible(1);
    root_->set_process(1);
    add_child(root_);
    root_->set_owner(this);

    CSGSphere* sp = memnew( CSGSphere );
    sp->set_name("sphere1");
    sp->set_radial_segments(24);
    sp->set_visible(1);
    sp->set_radius(1.0f);
    root_->add_child(sp);
    sp->set_owner(this);
}

c) Screenshots

  1. Godot Editor with test object as the scene root: The Hierarchy is as intended and the CSGSphere can be selected in the viewport.
    Godot Editor with test object as the scene root

  2. Godot Editor with the test object beeing a child node: The hierarchy is incomplete. The CSGSphere cannot be selected in the viewport.
    Godot Editor with the test object beeing a child node

asked Jun 8 in Engine by veraton (13 points)
retagged Jun 8 by veraton

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.