- Home /
Problem in loading and saving the game
Hello all,
While making save game script i took the following attributes
1)Current level name 2)player location and ammo count
while loading I loaded the saved level name and set the player location and ammo. And its working properly but the loaded game is not lasting even for a second and the level with the default settings and variables.
Here is the code:
import System.IO;
private var buttonWidth : int;
private var buttonHeight : int;
private var groupWidth : int;
private var groupHeight : int;
var paused : boolean;
var character : GameObject;
var shootObject : GameObject;
var xpos : float;
var ypos : float;
var zpos : float;
function Start ()
{
buttonWidth = 200;
buttonHeight = 50;
groupWidth = 200;
groupHeight = 500;
Screen.lockCursor = true;
Time.timeScale = 1;
paused = false;
//shootObject = GameObject.FindGameObjectWithTag("Gun");
}
function OnGUI()
{
if(paused)
{
GUI.BeginGroup(new Rect(((Screen.width/2) - (groupWidth/2)), ((Screen.height/2) - (groupHeight/2)), groupWidth, groupHeight));
if(GUI.Button(new Rect(0, 0, buttonWidth, buttonHeight),"Main Menu"))
{
Application.LoadLevel(0);
}
if(GUI.Button(new Rect(0, 60, buttonWidth, buttonHeight),"Save Game"))
{
var path : String = "C:/Aditya/TestSaves";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if(!File.Exists(path + "/SaveSlot1.txt"))
{
var create_text = File.CreateText(path + "/SaveSlot1.txt");
create_text.Close();
}
var loadedfile = new StreamWriter(path + "/SaveSlot1.txt");
loadedfile.WriteLine(saveData());
loadedfile.Close();
}
if(GUI.Button(new Rect(0, 120, buttonWidth, buttonHeight),"Load Game"))
{
loadData();
}
if(GUI.Button(new Rect(0, 180, buttonWidth, buttonHeight),"Quit"))
{
Application.Quit();
}
GUI.EndGroup();
}
}
function Update ()
{
if(Input.GetKeyUp(KeyCode.Escape))
{
if(Time.timeScale == 0)
{
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = true;
Screen.lockCursor = true;
Time.timeScale = 1;
paused = false;
}
else
{
GameObject.Find("First Person Controller").GetComponent(MouseLook).enabled = false;
Screen.lockCursor = false;
Time.timeScale = 0;
paused = true;
}
}
}
function saveData()
{
var data : String;
xpos = character.transform.position.x;
ypos = character.transform.position.y;
zpos = character.transform.position.z;
data += "LevelName:" + Application.loadedLevelName + "\n";
data += "X-Position:" + xpos.ToString() + "\n";
data += "Y-Position:" + ypos.ToString() + "\n";
data += "Z-Position:" + zpos.ToString() + "\n";
data += "Ammo:" + shootObject.GetComponent(shoot).ammo + "\n";
return data;
}
function loadData()
{
var path : String = "C:/Aditya/TestSaves";
if(File.Exists(path + "/SaveSlot1.txt"))
{
var loadeddata = File.OpenText(path + "/SaveSlot1.txt");
var readdata = loadeddata.ReadLine();
while(readdata != null)
{
var getdata = readdata.ToString().Split(":" [0]);
//if(getdata[0] == "LevelName") Application.LoadLevel(getdata[1]);
if(getdata[0] == "X-Position") character.transform.position.x = float.Parse(getdata[1]);
if(getdata[0] == "Y-Position") character.transform.position.y = float.Parse(getdata[1]);
if(getdata[0] == "Z-Position") character.transform.position.z = float.Parse(getdata[1]);
if(getdata[0] == "Ammo") shootObject.GetComponent(shoot).ammo = float.Parse(getdata[1]);
readdata = loadeddata.ReadLine();
}
loadeddata.Close();
}
}
Someone help me as soon as possible.
Thanks, Aditya
Do you get any error? Also, don't you want to use PlayerPrefs ins$$anonymous$$d? It does pretty much the same as you do but in one line.
Not to mention the fact that you save as plain text and can then be modified really easy.
or check out this tutorial on just saving classes with your data in them:
Your answer
Follow this Question
Related Questions
How can I save a character during runtime using script? 3 Answers
Saving Game Confussion 1 Answer
Saving frequently throughout game lifecycle (Android) 1 Answer
System.IO Help! 2 Answers
Loading a saved Game 1 Answer