Nodes get saved and loaded by their properties.
In addition, there is no way to save a “pointer” as is. Pointers are memory addresses, and these can change every time the game runs.
To have a persistent property in your custom node, It needs to be one of the following types:
If you want to save relations between objects, things get a bit more complicated. References are usually saved using specific IDs, or node paths. Sometimes they can even be avoided entirely (using the node hierarchy itself for example). You may have to explain what kind of links you want to save otherwise.
Note: I’m assuming you are creating an engine module, not GDNative.
The ‘native’ types i already use and it works fine, but i need to save some relation between objects, right.
There are class player. I want to save it inside scene.
State - Reference class (empty now). Propably i have to write some saver?
So, there is class with integers which works fine and can be saved in scene.
P.S. I have other class that named Class which saves successfully with object properties to State*. Now i add into Player CTOR nullptr assignment to camera and state, and it is working, but i can not assign some node by property in inspector anyway.
MegaGeneral | 2020-03-17 06:23
You can’t save node types inside properties, because nodes are supposed to be saved along with the rest of the scene through their parent-child relationships, not inside another object.
To get back references to the camera (or any other node which you don’t know its path in advance), you have to proceed indirectly, by exposing its NodePath, so that you can obtain it through get_node later (when _ready). Raw pointers won’t work. Note that if you have only one camera you can get the active camera using get_viewport().get_camera(), so no need to save a reference to it.
Note that the same principles apply to GDScript, so if you have problems doing it in C++, you may want to try make it work in GDScript first.
Zylann | 2020-03-17 19:24
I will use this system not only for camera.
Okay, i undrestood about NodePath.
Then other question: how to organize the work with NodePath’es as properties?
Make property for Object*? Or for NodePath*?
The main problem of godot, i think - low-documented engine code and deficit of tutorials for C++
MegaGeneral | 2020-03-18 08:53
Then other question: how to organize the work with NodePath’es as properties?
Once you have a node path, you can get the node using get_node(), and store it in a variable if you wish.
In GDScript, the pattern of obtaining a node for which the path is determined from inspector would be this:
There is also a very common pattern for non-changing nodes that you actually know the path, which I use a lot because there is no point wiring nodes in the inspector that are children of the same scene anyways, with properly chosen names:
onready var _owned_node = get_node("Path/To/MyNode")
The difference is much more verbose to do the same thing. Also, it can crash if something is wrong (you may use a debugger to have more insight), while the script implementation won’t crash, and will tell you what’s wrong in more details. So it’s a good idea to prototype in script first, and then translate.
The main problem of godot, i think - low-documented engine code and deficit of tutorials for C++
While this is true, I’ll reiterate my last point:
the same principles apply to GDScript, so if you have problems doing it in C++, you may want to try make it work in GDScript first. I’ll also add that the latter is much more documented, because it’s how the engine is primarily meant to be used. This is why there aren’t specifically as many tutorials for C++, it’s not a common entry level. There are still syntax specificities, but when you know how to script the engine, it often just takes looking at existing code to learn how the same things are done.
Note about your original question, to solve a crash, you need to use a C++ debugger, to confirm what’s really happening. It’s almost mandatory when you do C++, as it will point you exactly what call stack failed, and tell you the state of variables.