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

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By veraton

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 custom_modules_in_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 godot1_as_root)
  • If I attach the test object as a child node to some other node (see screenshot godot2_as_sub-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 add_child() and set_owner() relevant?

Attachments

a) The object hierachy

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

  • TEST1
    • TEST_SCENE_ROOT
      • 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

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

MXXIV | 2022-10-16 17:49