Page 1 of 1
Save/Load System
Posted: Wed Feb 17, 2016 8:49 am
by kilfeather94
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
Posted: Wed Feb 17, 2016 9:59 am
by Tony Li
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.
Re: Save/Load System
Posted: Thu Feb 18, 2016 8:04 am
by kilfeather94
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.
Re: Save/Load System
Posted: Thu Feb 18, 2016 9:49 am
by Tony Li
Hi,
In OnRecordPersistentData can you store weaponNumber?
Code: Select all
void OnRecordPersistentData() {
DialogueLua.SetVariable("weaponNumber", weaponNumber);
}
And then in OnApplyPersistentData retrieve it and apply it?
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.
}
Re: Save/Load System
Posted: Thu Feb 18, 2016 6:28 pm
by kilfeather94
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.
Re: Save/Load System
Posted: Thu Feb 18, 2016 8:22 pm
by Tony Li
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:
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);
}
}
You'll end up with these saved variables:
- 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").
When we load the game, we need to clear the list and rebuild it using the saved data:
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);
}
}
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.
Re: Save/Load System
Posted: Sun Feb 28, 2016 8:33 am
by kilfeather94
Hey, sorry for the late response.
Managed to get the saving working with the weapons. Thanks for the help
Re: Save/Load System
Posted: Sun Feb 28, 2016 8:50 am
by Tony Li
Great! Glad to help!