Hi, I’m just starting a new project which will have multiple scenes and I noticed that (in the example at least) by loading a new scene the complete hierarchy of the previous scene is destroyed.
So, my question is how do I maintain a set of game wide variables that can exist between scenes? Do I need to keep a separate (Manager) object outside of the root which contains the variables and make sure I never destroy it or is there a cleaner way?
I couldn’t find any information in the manual regarding global variables or variable scope but maybe I missed it?
You can put variables in the global js scope by just declaring them outside of script object you create. Being a bit paranoid about breaking other js elements potentially loaded on the page, I typically stay away from it.
As you mentioned in your post, I prefer to have a manager that moves with each scene change taking all of the script that need to follow with it. That way I can keep things that need to be persistant, persistant, and everything else gets destroyed.
Ultimately it’s about your comfort level working in the global scope.
I believe the scene loading is additive … it just adds entities into the hierarchies. If you see the old hierarchy getting removed, that means a code to do so is called. You can simply not delete some initial scene and keep persistent scripts / data in it.
…and if i want to go back to a previous scene is there a way of loading it but excluding certain objects ie, my manager object, or would I have to manually delete the duplicate? That sounds like it could get quite messy.
Actually, what I do is have a scene that is only loaded once and let my manager and all of my persistent scripts live there. That way, I can always go back to any scene I want: