+2 votes

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


  • 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


  • 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?


a) The object hierachy

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

  • TEST1


      • sphere1

b) Source Code

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

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



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

    root_ = memnew( Spatial );

    CSGSphere* sp = memnew( CSGSphere );

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

in Engine by (14 points)
retagged by

Did you figure it out? I am just starting and I have no clue how to do this...

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.