I would like to have the following confirmation:
It seems that the value of a variable of a global object is not shared between the different scenes of a game.
I noticed it in my last game.
I know how to get around the problem but I would like someone to confirm (or deny) my statement…
Thank you in advance.
Exactly.
You can easily test that creating 2 scenes and a global object with 1 variable.
In the first scene, you give an value (number like 10)
In second scene, you affect the value of the variable of the global object, in a new variable (local or global: no importance)
And in second scene, you display this new variable and you’ll see that this last egal 0 (and no 10)
Ok. To fix that, simply use a global variable which will contain, at the end of fist scene, a copy of the value of the variable of the global object. In the second scene, you’ll affect this global variable in the variable of the global object.
But it is astonish that global object don’t share their variable(s) between scenes.
Hi Xeira …the object variables of global objects are reset to default when you change scenes …if you want the player health from one scene to carry over you need to use a global variable or storage. Would you rather it wasn’t like that and the values carried over to the next scene?
Hi Dave!
Coming from “classic” programming, I find it quite strange that an object with global scope does not transmit its variables throughout the game.
The variables of the global object should in my opinion inherit the global scope.
Yes I think you’re right …they should persist between scenes. But it’s being deleted and created again. I don’t know how you’d change it now to a different system without breaking everything thats been made so far.
ai says…
Understanding Global Object Variables
Global objects are shared across all scenes, meaning their properties (including variables) persist across scenes.
However, object variables tied to a global object may reset to their default values when a new scene is loaded unless explicitly managed.
…which seems contradictory to me!
Its not really a bug though - it’s just the way it is. You get used to managing it but I agree that logically a global objects properties should persist.
Global objects are identical to regular objects. The difference is that they’re available to be added to any scene without having to be first setup. That part is global. The objects themselves get created in each scene with all the default settings just like a scene object. Each object is a new copy.
You first add them to a scene or project if they’re global. They get added as a sort of a preset or an ingredient for a recipe and then you can add them to a scene. I’m sure there’s a more specific way to say it but you basically add them twice. (Add and then create)
Another problem is how would you handle multiple instances. If there were multiple player objects then you would need some way to link the objects from scene to scene.
Another challenge would be that all of the behaviors would also have to retain their values. The variables would be helpful but for an object to be truly global it would have to retain everything. How would you handle something more complex like a custom object? It’s basically a scene within an object.
Maybe instead of modifying the global object. There needs to be a way to save/load an objects state which would include everything, variables, behaviors, etc. OK, now I’m dreaming. That’s a lot and would require a lot of work.
If the data was saved to a single variable maybe in the Json format then that data could be saved/, loaded using another variable or storage or a json file or some other external location.
Even that is confusing me because what if the player moves between scenes then the data would be needed to be updated each time. But would you want everything to be updated? If the player was running or jumping in 1 scene should it still be running in the other scene. There are times when things would be scene specific.
save/load object state is a good idea keith - aren’t they currently redoing the save system - to save the game state - could it be an addition to that?
Thanks a lot for all these informations well precise.
My needs, hopefully, are more simple: it is just to share the player score.
I deduce from this that the GD team wanted Global Objects to work in this way.
I would like to make a small clarification: i think that the term GLOBAL should not qualify this type of object.
A name like “Universal Objects” or better “Inter-Scene Objects” would be better because the word global can be misleading regarding the variables of this type of object (for me, it was the case)
The name global has always been sort of imprecise and a bit confusing. But there are 2 steps to adding an object. The word global only applies to the first part but it’s tough to capture that whole process while keeping the name short.
Reminder that objects, once in the scene itself, are instances and are no longer global.
Variables on instances are scene specific.
Global objects are just templates for creating instances in any scene without having to duplicate them. (Scene objects are just templates for creating instances in that specific scene).
variables on a global object are just defaults that are created when an instance is created, they do not sync back to the template or other scenes.
Yes, I think I understood correctly.
It doesn’t seem to me that the documentation mentions this.
But thank you for all these explanations which have the merit of clarifying things.