- Home /
Saving and loading level/game state
Hi I'm quite a noob with unity and I'm trying to create a dungeon crawler game in the style of Dungeon Master et al.
My problem is saving the actions the player has performed. I need this as its possible for a player to not only go forward through the levels but also revisit levels they've already passed through.
For example: If the player unlocks and opens a door before proceeding to the next level I want that door to be unlocked and open when the player revists the level.
I know about playerPrefs but would prefer not to use this as the game will be Windows based(and hence clog the registery with tonnes of info).
I'm not looking for ready made scripts but just advice and maybe a few pointers on how I can do this.
Thanks for reading
Answer by AlucardJay · Mar 10, 2012 at 02:49 PM
have a look at Object.DontDestroyOnLoad .
This is from the Unity Script Reference :
static function DontDestroyOnLoad (target : Object) : void
Description
Makes the object target not be destroyed automatically when loading a new scene.
When loading a new level all objects in the scene are destroyed, then the objects in the new level are loaded. In order to preserve an object during level loading call DontDestroyOnLoad on it. If the object is a component or game object then its entire transform hierarchy will not be destroyed either.
// Make this game object and all its transform children
// survive when loading a new scene.
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}
So maybe you could create an empty game object, and save all your variables to it before you do Application.LoadLevel .
So for example , let's call the object Logger :
public var Health : float = 0.0; public var Gold: int = 0; public var Backpack: Array = ["Stamina Potion, "Gold key"];
function Awake () { DontDestroyOnLoad (transform.gameObject); }
Then on the Player Script :
function UpdateLogger () {
var Logger = GameObject.Find("Logger_Object").GetComponent(Logger_Script);
Logger.Health = myHealth;
Logger.Gold = myGold;
Logger.Backpack = myBackpack;
}
This is just a basic answer as I am a noob myself. Personally I would put the =>
var DontDestroy = GameObject.Find("Logger_Object").GetComponent(Logger_Script);
<= statement in a Start or Awake function, so it only searches once to load the Logger Script. Hope this helps.
Thanks but I am already using a game$$anonymous$$anager script thats persistent. Its the saving/loading of the data that im having problems with as i dont want to use PlayerPrefs.
Sorry I read youre answer wrongly the 1st time.I am already using a game$$anonymous$$anager script thats persistent. Its the saving/loading of the data that im having problems with as i dont want to use PlayerPrefs.
DOH!
Thanks alucardj I was looking at it from the game$$anonymous$$anager Object. Loading all the states then calling all the classes methods to update them. I didnt think of doing it that way. I told you I was making it more difficult than it should ber :D
Thanks again man.
Haha, @alucardj the accept rate is pointless ... accepts are just so people who go looking here can easily find which solution worked best. $$anonymous$$arma is where it's at man!