+2 votes

I'm trying to get the hang of using C++ modules. I want to create a custom engine module which handles character behavior/movement, so I can easily drag-and-drop a "character" node into a scene and have everything "just work" with minor tweaks. Essentially, my goal is to get this particular step of the FPS tutorial working in a C++ module.

The issue is that the engine's outliner doesn't match the nodes I've set up in the C++ constructor:

 Character::Character() {
    _body_collision = memnew(CollisionShape);
    _body_collision->set_name("Body_Collision");
    _body_collision->set_shape(body_shape);
    add_child(_body_collision);

    _feet_collision = memnew(CollisionShape);
    _feet_collision->set_name("Feet_Collision");
    add_child(_feet_collision);

    _rotation_helper = memnew(Spatial);
    _rotation_helper->set_name("Rotation_Helper");
    add_child(_rotation_helper);

    _model_base = memnew(Spatial);
    _model_base->set_name("Model");
    _rotation_helper->add_child(_model_base);

    _character_camera = memnew(Camera);
    _character_camera->set_name("Camera");
    _rotation_helper->add_child(_character_camera);

    print_tree_pretty();
 }

An empty scene, with the C++ hierarchy in the output.

As you can see, the scene just has the "Character" node, with nothing accessible outside of the body_shape property I exposed to see if I could get things working. In the console output, though, you can see the actual chain of how nodes are set up internally. If you wanted to add a child node to _rotation_helper you couldn't do it very easily without going into scripts.

Is there any way to get this chain to appear in the editor somehow? Or am I approaching this entire thing the wrong way?

asked Jan 12, 2019 in Engine by Jay2645 (32 points)

1 Answer

+3 votes
Best answer

Figured it out. You need to call set_owner, like so:

Character::Character() {
    _body_collision = memnew(CollisionShape);
    _body_collision->set_name("Body_Collision");
    _body_collision->set_shape(body_shape);
    add_child(_body_collision);
    _body_collision->set_owner(this);

    _feet_collision = memnew(CollisionShape);
    _feet_collision->set_name("Feet_Collision");
    add_child(_feet_collision);
    _feet_collision->set_owner(this);

    _rotation_helper = memnew(Spatial);
    _rotation_helper->set_name("Rotation_Helper");
    add_child(_rotation_helper);
    _rotation_helper->set_owner(this);

    _model_base = memnew(Spatial);
    _model_base->set_name("Model");
    _rotation_helper->add_child(_model_base);
    _model_base->set_owner(this);

    _character_camera = memnew(Camera);
    _character_camera->set_name("Camera");
    _rotation_helper->add_child(_character_camera);
    _character_camera->set_owner(this);

    print_tree_pretty();
}
answered Jan 12, 2019 by Jay2645 (32 points)
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.