godot-cpp
godot-cpp copied to clipboard
Member variable doesn't initialize
Error happens in both mono, and standard version 3.3.2. Both master branch and 3.3 branch. Scenario: Given a class with:
// Godot will call to find out which methods can be called on our NativeScript and which properties it exposes.
static void _register_methods();
// Called after Godot has properly set up our object. It has to exist even if you don't place any code in it.
void _init();
GameManager(); // Constructor
~GameManager(); // Destructor.
The script initializes with the following order: _register_methods gets called. GameManager gets called. _init gets called.
inside _register_methods i wrote
register_property<GameManager, bool>("m_server_build", &GameManager::m_server_build, false);
register_property<GameManager, String>("m_server_address", &GameManager::m_server_address, "127.0.0.1");
register_property<GameManager, int>("m_server_port", &GameManager::m_server_port, 24536);
register_property<GameManager, int>("m_max_clients", &GameManager::m_max_clients, 1000);
register_property<GameManager, Ref<PackedScene>>("m_character_scene", &GameManager::m_character_scene, NULL);
and constructor:
godot::GameManager::GameManager()
: m_server_build(false), m_server_address(""), m_server_port(0), m_max_clients(0), m_character_scene(nullptr)
{
Godot::print("GameManager methods get called");
}
When return to editor _register_methods gets called and initialize variable inside it. But when running the game the constructor gets called and _init follow afterward. and reset everything I have set up inside _register_methods. If I remove the construction in Constructor the variables get trash value. the order of methods gets called by editor is:
_register methods get called
GameManager methods get called
_init methods get called
A good track: when changing values from the editor, it calls set_value, and then I run the game, I get the value as expected. But when reset values to given default value, it's not initialized by the editor and this error happens. With this, I expect it will be fixed easily. @Calinou