I have a node with two exported variables and the default value of "Num" is 0. I added this node to a scene.
When I change the value of "Num" to 2 in the inspector, the values of the export variables are permanently stored in the scene. My question is: Is it possible to change the value of the export variable using code?
Because currently when I change the value of the export variable "Num" to 3, when I go to another scene and return, the value of "Num" changes back to 2, which is not what I expected.
The reason I'm asking this question is because I want to use export variables to "store" the state of the trees in the map. For example, I have an export variable for the state of the trees (1 for spring, 2 for summer, 3 for fall, 4 for winter) so if the season changes, I can change the state of the trees and save the map so that I don't need to add additional logic to store the information.
Also, is it a bad idea to use export variables to store things?
Is it possible to change the value of an exported variable using code?
The question is what do you mean by "go to scene"? Switching to a different scene at runtime via change_scene_to_file or change_scene_to_packed? If that is the case all state of the previous scene is lost. Scene state is not persistent over scene changes. That has nothing to do with export. All (non-static) variables behave that way.
If you need state to persist across scene changes use autoloads.
CrayonApe Oh I think I understand.
Right, your own exported properties are just like the properties that you see in the inspector from build-in classes like the transform of a Node2D or visible
from a CanvasItem. Open your .tscn
file in a text editor and you'll see that their values are stored in your scene files. So if you change them at runtime (like you would position
of a Node2D) they won't get saved in your scene files.
CrayonApe So I should never consider it as a way to "store" persist data.
Correct.
Toxe Yeah, the "editable" and "persistent" nature of exported variables had me thinking in the wrong direction.
So if I want to keep my data "persistent" between scenes, I should use the autoload feature to save the global variables I want to track. And if I want to keep my data "persistent" between different game sessions, I should use local files(Json/ Resource/ Xml) to save the data. Right?
- Edited
CrayonApe So if I want to keep my data "persistent" between scenes, I should use the autoload feature to save the global variables I want to track.
There are multiple ways to do this. Global shared Node, Dictionary, some object or Resource, singleton...
CrayonApe And if I want to keep my data "persistent" between different game sessions, I should use local files(Json/ Resource/ Xml) to save the data. Right?
Right, saved somewhere in user://
.