- Home /
Save and Load plus a few other stuff
I am having a little trouble with some stuff at Unity. First of all, what is the best way to save and load a PC Plataform game?
If it's XML, can someone show me a good and simple example? Because the one I made I got nullreferences at them. If needed, I'll post the code down.
Next issue: When loaded, how to set those values to the right places? Put them through the Start from the codes? Isn't there an easier way? Like a Data Bind, or something?
I am getting null in these:
`currentHealth = InfoHolder.Instance.CurrentHP;`
And on the SaveData below, that is the InfoHolder.
> using UnityEngine; using
> System.Collections;
>
> public class InfoHolder :
> MonoBehaviour {
> public static InfoHolder Instance;
>
> //Preencher com os mesmos valores que
> vão ser usados no SaveData. Quando o
> jogo for carregado, serão passados
> para cá os valores e vice-versa quando
> salvo.
> //Procure e preencha daqui os valores.
>
> public int CurrentHP = 100;
> public int MaxHP = 100;
> public int CurrentMP = 100;
> public int MaxMP = 100;
> public int Fruits = 0;
> public bool LevelChocLabEnter = false;
>
> public void Bezier()
> {
> switch (Application.loadedLevel)
> {
> case 1:
> LevelChocLabEnter = true;
> break;
> }
> }
>
> public void CallSave()
> {
> SaveData.Instance.CurrentHP = CurrentHP;
> SaveData.Instance.MaxHP = MaxHP;
> SaveData.Instance.CurrentMP = CurrentMP;
> SaveData.Instance.MaxMP = MaxMP;
> SaveData.Instance.Fruits = Fruits;
> SaveData.Instance.levelReached = Application.loadedLevel; //Essa variável pega o último level em que o
> personagem pisou.
> SaveData.Instance.LevelChocLabEnter =
> LevelChocLabEnter;
>
> SaveLoad.Save();
> } }
Thanks in advance, Pedro Loures.
You should split these into separate questions so they get answered faster. You can google for saving and loading game data in Unity (spoiler: it's up to you)
Answer by Caiuse · Nov 15, 2011 at 11:37 AM
Great community made XML parser :
http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML
a good class structure for the data is the best way of storing the data in the XML
Hope this helps - C
Answer by Blackice000 · Nov 15, 2011 at 12:38 PM
I've seen that, and that did help, what I wanted was to understand where can I set what else do I want to save. Since that seems to save only the player position, and I wanna save lots of variables.
And for some reason, I dunno why, the InfoHolder.Instance.Bezier(), for example, is returning as null, as I can't do it like that, even though I did it just like it before with my Third Person Control.
Only classes that can be serialised can be "saved"
I would suggest storing all your information in a class then serialising that class.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.aspx
Answer by Blackice000 · Nov 15, 2011 at 02:22 PM
I know how to use the serialization, but ok, I made it work. I couldn't use instance because they weren't in the same object. I then had to made the methods public and static. With that I could acess them. Saving and Loading was easier to understand once that worked out.
Thanks for the help, Caius1!
Your answer
Follow this Question
Related Questions
Xml Saving and Loading Problems 1 Answer
Save/Load game using XML on Browser/Web Player 2 Answers
Saving Game Problem 1 Answer
Editing an XML File 1 Answer