Save/Load System
-
- Posts: 17
- Joined: Sat Jan 23, 2016 1:35 pm
Save/Load System
I'm using the Game Saver component and trying to figure out how to save the state of a game. It's able to keep track of the level the player was last on, but I just want to know, how do I save the state of the game at the exact moment it was saved at, because it doesn't keep track of stuff like the player's current health for example, which is stored in another script on the player or if the player was carrying a weapon. When I close the game and start it again and load my data, it brings me back to the level I saved but it doesn't keep track of stuff like health or weapon the player has equipped.
Re: Save/Load System
Hi,
Add persistent data components to the GameObjects that have data you want to save.
For example, if you add a Persistent Position Data component to a GameObject, it will save the position and rotation.
For your own custom (or third party) scripts, write your own persistent data component by making a copy of Scripts/Templates/PersistentDataTemplate.cs and filling in the two methods (one for saving, one for loading).
The included third party support packages generally contain their own persistent data components. For example, if you've imported and configured UFPS support, this will save UFPS data such as the player's health and inventory, and which enemies have been killed.
Add persistent data components to the GameObjects that have data you want to save.
For example, if you add a Persistent Position Data component to a GameObject, it will save the position and rotation.
For your own custom (or third party) scripts, write your own persistent data component by making a copy of Scripts/Templates/PersistentDataTemplate.cs and filling in the two methods (one for saving, one for loading).
The included third party support packages generally contain their own persistent data components. For example, if you've imported and configured UFPS support, this will save UFPS data such as the player's health and inventory, and which enemies have been killed.
-
- Posts: 17
- Joined: Sat Jan 23, 2016 1:35 pm
Re: Save/Load System
I got the PersistentDataTemplate working so I can now keep track of stuff like the player's health. I'm just having trouble getting it to work with keeping track of the player's weapon. I have a WeaponManager class which keeps track of the player's current weapon by using a List called 'WeaponList' and an int called 'weaponNumber'. Every weapon in my game has a 'WeaponControl' script. In the 'WeaponManager' script, I make the 'WeaponControl' script public like so:
public WeaponControl ActiveWeapon;
The 'WeaponManager' script determines which weapon the player is carrying in the Update method:
ActiveWeapon = WeaponList[weaponNumber].GetComponent<WeaponControl>();
I'm just not sure how to keep the 'ActiveWeapon' persistent in the 'PersistentDataTemplate' class.
public WeaponControl ActiveWeapon;
The 'WeaponManager' script determines which weapon the player is carrying in the Update method:
ActiveWeapon = WeaponList[weaponNumber].GetComponent<WeaponControl>();
I'm just not sure how to keep the 'ActiveWeapon' persistent in the 'PersistentDataTemplate' class.
Re: Save/Load System
Hi,
In OnRecordPersistentData can you store weaponNumber?
And then in OnApplyPersistentData retrieve it and apply it?
In OnRecordPersistentData can you store weaponNumber?
Code: Select all
void OnRecordPersistentData() {
DialogueLua.SetVariable("weaponNumber", weaponNumber);
}
Code: Select all
void OnApplyPersistentData() {
weaponNumber = DialogueLua.GetVariable("weaponNumber").AsInt;
ActiveWeapon = WeaponList[weaponNumber].GetComponent<WeaponControl>();
// Add any code here needed to put the weapon GameObject in the player's hands, etc.
}
-
- Posts: 17
- Joined: Sat Jan 23, 2016 1:35 pm
Re: Save/Load System
It keeps track of the weapon number now when I save and load, but the actual weapon prefab that is inside my WeaponList isn't carried over into the saved game.
Here's screenshots of my WeaponManager component which shows the state of the component before saving and after loading:
http://imgur.com/a/zNfzG
The top image shows the state of the WeaponManager after loading the saved game.
The bottom image is the state of the WeaponManager before saving my game.
As you can see, the weapon number is kept track of when the game is loaded, but the 'shotgun' prefab is missing in the WeaponList when I load the game.
Here's screenshots of my WeaponManager component which shows the state of the component before saving and after loading:
http://imgur.com/a/zNfzG
The top image shows the state of the WeaponManager after loading the saved game.
The bottom image is the state of the WeaponManager before saving my game.
As you can see, the weapon number is kept track of when the game is loaded, but the 'shotgun' prefab is missing in the WeaponList when I load the game.
Re: Save/Load System
Since your WeaponList is dynamic, you'll also need to save and load its current state.
Below is a theoretical example. Since I don't know how you're actually adding new items to WeaponList, the actual code you need for your project may be different.
For this example, let's assume you instantiate a prefab by name from a Resources folder.
When we save the game, we need to save the size of WeaponList and the names of the prefabs:
You'll end up with these saved variables:
This isn't copy-and-paste code. You'll definitely need to compare the logic of the code above to your own script's needs. Your code might work slightly differently.
Below is a theoretical example. Since I don't know how you're actually adding new items to WeaponList, the actual code you need for your project may be different.
For this example, let's assume you instantiate a prefab by name from a Resources folder.
When we save the game, we need to save the size of WeaponList and the names of the prefabs:
Code: Select all
void OnRecordPersistentData() {
DialogueLua.SetVariable("weaponNumber", weaponNumber);
DialogueLua.SetVariable("WeaponListCount", WeaponList.Count);
for (int i = 0; i < WeaponList.Count; i++) {
DialogueLua.SetVariable("WeaponList_" + i, WeaponList[i].name);
}
}
- weaponNumber: Stores your script's weaponNumber value (1).
- WeaponListCount: Stores the size of WeaponList (2).
- Weapon_0: Stores the name of WeaponList[0] ("Unarmed").
- Weapon_1: Stored the name of WeaponList[1] ("Shotgun_Final").
Code: Select all
void OnRecordPersistentData() {
weaponNumber = DialogueLua.GetVariable("weaponNumber").AsInt;
// Clear the old list:
for (int i = 0; i < WeaponList.Count; i++) {
Destroy(WeaponList[i]);
}
WeaponList.Clear();
// Create the new list from the saved data:
var count = DialogueLua.GetVariable("WeaponListCount").AsInt;
for (int i = 0; i < count; i++) {
var prefabName = DialogueLua.GetVariable("WeaponList_" + i).AsString;
var weapon = Instantiate(Resources.Load(prefabName)) as GameObject;
WeaponList.Add(weapon);
}
}
-
- Posts: 17
- Joined: Sat Jan 23, 2016 1:35 pm
Re: Save/Load System
Hey, sorry for the late response.
Managed to get the saving working with the weapons. Thanks for the help
Managed to get the saving working with the weapons. Thanks for the help