in Scene A, many things happens, there are 10 NPC in different states, some switches ON and some others OFF… there is an exit, player goes there and loads scene B…
you are now in B, in B there is nothing important, just some trees around and so on… you decide return to A…
now the logic…
How do you do to set everything in A the same way when the player leave ? … if I´m on B, and then load A, everything will be returned to default sets…
Actually i´m using global vars… but i´m not fully sure if this is the best way since i must to store the configuration of everything before load B and this is tedious… any other way ?
Static global variables would be the way actually, as far as I know. I never delved into the really deeper functions, so I may be missing something. Globals would be what I’d do though. You can automate it with a simple script that is placed on every object. Perhaps using an array in some way. Then retrieving on enter, at that values location in the array. Instead of using hundreds of variables.
They may well be involved, but I wouldn’t suggest storing the data directly in them. It’ll work, but it’s a kludgy and often inflexible solution. (Though if it gets the job done and nothing more is needed, that’s perfectly fine.)
A few things I’d look at:
DontDestroyOnLoad() lets you persist GameObjects and their Components between scenes. Any data stored on these can be accessed as per usual - Find(), GetComponent(), etc.
Remember you have a full Mono/.NET library at your disposal. Not everything needs to be a MonoBehaviour. You can store whatever data you want outside of a scene, and use something like a static class or a singleton to make the references available within your scene.
File IO. Depending on what you need and how much if it there is, this is often a great solution.
PlayerPrefs. Don’t abuse it - as per the name it’s for storing preferences, ie: trivial amounts of data. But again, if that’s all you need, go for it. (It’s essentially a super light file IO system.)
Very true, I should really get into more .NET. Only used some basic concepts like ticks and such. Never made a finished complex idea that needed this stuff, but my next project I’m working on well definitely require a little more then I’ve become accustomed to. Good suggestions though. Forgot about DontDestroyOnLoad(). My last mobile game was done complete in one scene, but I’ve moved to PC/Mac now, things like these will come in handy.
Separation of concerns is a good habit to be in. Your data doesn’t have to be in the same place as your behaviour. If you want to persist the data without keeping the objects that use it, don’t store it in those objects.
Manager with DontDestroyOnLoad in first scene, to keep track/set states and load/save to file.
Scripts on objects to check/load state on start, do whatever and update the manager.
This makes it easier to keep track of chain of events from several objects(a quest) and load/save data to file.
ohh im understanding you penguin and murdoc… you keep the vars (data) on an empty that will not be destroyed on load, the script attached to this empty will record every data in scene…
nice way!!.. with this i save memory by not using global vars directly …
my game is segmented, the world 1 has nothing to do with world 2… if i am in world 2, then never will return to 1… so data in 1 can be deleted, this means that i can use simple vars with no IO filie since next time player plays the game will continue with a password starting from scratch (in the world 2)… this serves me to save a lot of save/load scripting…