- Home /
SaveLoad Profile c# to xml
welp , i changed the stone system for a bit more gems
here what im trying to do , i just want to bein able to save and load the number of gems of the player , if he have the equipment on , and how mutch shield , his name , and his code
i did read the tutorial about the monsters in xml but the fact im french dont realy help in the translation . i have limited vocabulary and i have no idea who you must use the script to load and save
here what i got so far
the GameSystem.cs
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
public class GameSystem : MonoBehaviour
{
//--------------------------------------------------------------//
[XmlAttribute("Profile")]
public string Name = "Name";
public int emerald = 0;
public int sapphire = 0;
public int ruby = 0;
public int hellstone = 0;
public int Token = 0;
public int shields = 0;
public int equips = 0;
public bool god = false;
public bool jetpack = false;
public int code = 0;
//-------------------------------------------------------------//
// Use this for initializationname
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
}
__________________________________________________ ____________
and the ProfileContainer.Cs
__________________________________________________ ____________
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[XmlRoot("MonsterCollection")]
public class ProfileContainer
{
[XmlArray("Gems"),XmlArrayItem("Gem")]
public Gem[] Gems;
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(ProfileContainer));
using(var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
public static ProfileContainer Load(string path)
{
var serializer = new XmlSerializer(typeof(ProfileContainer));
using(var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as ProfileContainer;
}
}
public static ProfileContainer LoadFromText(string text)
{
var serializer = new XmlSerializer(typeof(ProfileContainer));
return serializer.Deserialize(new StringReader(text)) as ProfileContainer;
}
}
i did more than asking for everything like the last question but since here , i know there still problems and i dont know how to save them things and how to use the save function and load function from the container
if someone can just explain me how it work so i understand for when ill be to saving the unlocked level (they will be bool)
Exactly Just Use PlayerPrefs
Example: PlayerPrefs.SetInt("Gems", numberOfGems);
and whenever you want them back PlayerPrefs.GetInt("Gems");
i have no idea how it work , im not familliar with sending things from script to another and due to my primary language , looking trough huge totorials whitout valid translation of some things just aint helping me.
i kind of need help for that part so i can learn and bein able to do it my self remembering what do what .. for the moment all i have is the core of my game working and the shop and the menu , all i need to do is save , characther selection and level selection + adding models and animations ... its a simple game including only 1 button to play so the codding was kind of simple for the system but now im at the part where my knowledge aint visited yet
@GameFreak ... tank you but where do i put this code ... and how i do for the name and for the booleans?
See its really really simple.
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
This is the documentation.
Now when you need to save something. for example you have
private int gems;
//gems are the number of gems so after all gems have been added use
if (gems> PlayerPrefs.GetInt("GE$$anonymous$$")) { PlayerPrefs.SetInt("GE$$anonymous$$", gems);
PlayerPrefs.Save();
}
and whenever you need the number of gems use
private var gems1 = PlayerPrefs.GetInt ("GE$$anonymous$$");
So you ll get the number of gems you stored earlier
Answer by DaveA · Mar 15, 2012 at 09:53 PM
Props to GameFreak. More info: You can do booleans by using this script: http://unifycommunity.com/wiki/index.php?title=BoolPrefs
Names like PlayerPrefs.GetString ("PlayerName");
Answer by GameFreak · Mar 15, 2012 at 10:04 PM
Here. See its really really simple.
http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
This is the documentation.
Now when you need to save something. for example you have
private int gems;
//gems are the number of gems so after all gems have been added use
if (gems> PlayerPrefs.GetInt("GEM")) { PlayerPrefs.SetInt("GEM", gems);
PlayerPrefs.Save();
}
and whenever you need the number of gems use
private var gems1 = PlayerPrefs.GetInt ("GEM");
So you ll get the number of gems you stored earlier
About the position. Just place it wherever the gems are? Like exactly if you are using this you wont use xml right? So whenever you want to save gems use SetInt and whenever you want them use getInt
As said above bools can be saved by SetString And GetString
For new script use something like-
var gems= Script1.gems1;
if (something happens)
{
var gems +=1;
}
if (gems> PlayerPrefs.GetInt("GEM")) { PlayerPrefs.SetInt("GEM", gems);
PlayerPrefs.Save();
}
Now in the first scirpt (script1)of the game use void Start () {
private var gems1 = PlayerPrefs.GetInt ("GEM");
}
sorry to be redunant and anoying but i still got a bunch of errors :/ i know i should understand all that but .. well maybe im just having a too low comprehention of english and programation of variable sending to get this done properly.
could it be posible you give me an exemple using all 3 gems (emeral ruby and sapphire , and for the jetpack and for the name in what script.cs and another one who call them .. if it need another script to call them .. i dont even get that part right too D:
very sorry to bother you to do that mutch for me but i realy need that save and load system that load automaticly at each time the player open the game
still not working , after plenty of test ... i have no idea what i do wrong in the first place ,:c
Answer by quakeman00 · Mar 16, 2012 at 12:00 AM
learned some more .. tank you , but once again lost with some translation and aint working how i would expect ... maybe im just too retarded to understand
can you please make some premade of what i have as variable up there because when try it it gime me errors about variable assignment D:
sorry to be anoying and redundant ... i just need more secificated and almost premade code so i can understand what do what :c
Answer by GameFreak · Mar 16, 2012 at 09:58 AM
Its tough posting it as a comment so I ll post it as a new answer
Script.cs private int ruby = script1.ruby1;
private int saffire = script1.saffire1;
private int emarald = script1.emerald1;
private int jetpack = script 1.jetpack1;
void OnCollisionEnter(col:Collision)
{
If (col.gameObject.tag == "Ruby"){
ruby += 1;
}
Else If (col.gameObject.tag == "Saffire") {
saffire += 1;
}
Else If (col.gameObject.tag == "Emerald") {
Emerald += 1;
}
Else if (col.gameObject.tag == "Jetpack"
{
Jetpack += 1;
}
void Update ()
{
If (ruby != PlayerPrefs.GetInt("Ruby") {
PlayerPrefs.SetInt ("Ruby",ruby);
}
If (ruby != PlayerPrefs.GetInt("Saffire") {
PlayerPrefs.SetInt ("Saffire",saffire);
}
If (ruby != PlayerPrefs.GetInt("Emerald") {
PlayerPrefs.SetInt ("Emerald",emerald);
}
If (ruby != PlayerPrefs.GetInt("Jetpack") {
PlayerPrefs.SetInt ("Jetpack",jetpack);
}
Now script1.cs (To be placed wherever the game begins)
private int ruby1;
private int saffire1;
private int emerald1;
private int jetpack1;
void Start ()
{
ruby1 = PlayerPrefs.GetInt ("Ruby");
saffire1 = PlayerPrefs.GetInt ("Saffire");
emerald1 = PlayerPrefs.GetInt ("Emerald");
jetpack1 = PlayerPrefs.GetInt ("Jetpack");
}
Untested but should work
didnt worked at first , had to make all variables static and public
also this
http://desmond.imageshack.us/Himg684/scaled.php?server=684&filename=51401904.png&res=medium
and jetpack is a bool D:
Your answer
Follow this Question
Related Questions
Save and Load data from Local XML file won't work 1 Answer
Save and Load from XML help 1 Answer
Saving Game Problem 1 Answer
Questions about XML save/load 0 Answers
Editing an XML File 1 Answer