godotengine / godotengine/godot-cpp

Member variable doesn't initialize

Open
#599 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

[game_manager.zip](https://github.com/godotengine/godot-cpp/files/7026656/game_manager.zip)

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("m_server_build", &GameManager::m_server_build, false);
register_property("m_server_address", &GameManager::m_server_address, "127.0.0.1");
register_property("m_server_port", &GameManager::m_server_port, 24536);
register_property("m_max_clients", &GameManager::m_max_clients, 1000);
register_property>("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
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.