- Home /
Load/Save Game Issues, can't create new game.
I have a world manager script that controls pretty much everything, right now it all works and allows me to play through and checkpoint etc.. problem is: when I get to the last level and want to restart the game by death it loads my last checkpoint in level 3. im not sure how to re-write the variables for the new game state to make it start at level 1 again once they have been saved. I'm pretty new to StreamWriter so im stumped!
here is my code for the world manager
///////////////////////////////////////////////////////////////
// SAVE / LOAD SYSTEM
///////////////////////////////////////////////////////////////
static public function SaveGame( )
{
// open the file for writing
var file = new StreamWriter( Application.dataPath + "/Saves/SavedGame.txt");
file.WriteLine( currentLevel ); // write the level the Player is on to the file
file.WriteLine( CurrentSpawnPointIndex ); // write the Player SpawnPoint to the file
// check if if the player exists so the information can be stored in the file
if (GameObject.FindWithTag("Player"))
{
// grab the Stat file to grab the information
var PlayerStats:PlayerStats = (GameObject.FindWithTag("Player").GetComponent("PlayerStats"));
file.WriteLine(PlayerStats.GetHealth());// write the health of the Player to the file
file.WriteLine(PlayerStats.GetAmmo(0));// write the Primary Ammo of the Player to the file
file.WriteLine(PlayerStats.GetAmmo(1));// write the Secondary Ammo of the Player to the file
}
// write the Mission status to the file
file.WriteLine( Missions[0] ); //level1
file.WriteLine( Missions[1] ); //level2
file.WriteLine( Missions[2] ); //level3
// make sure to write the information to the file
file.Flush();
// close the file
file.Close();
}
public function SetStats( PlayerHealth : int ,AmmoPrime : int, AmmoAlt : int)
{
var PlayerStats : PlayerStats = GameObject.FindWithTag("Player").GetComponent("PlayerStats");
PlayerStats.AddHealth(PlayerHealth, 0);
PlayerStats.AddAmmo(0,AmmoPrime, 0);
PlayerStats.AddAmmo(1,AmmoAlt, 0);
}
public function Initialize()
{
// set the state to Playing
SetLevelState("Playing"); // used when working with coded buttons
// load saved gameinfo if exists
if( !Directory.Exists( "Assets/Saves/" ) )
{
// if the directory doesnt exist, create it
Directory.CreateDirectory( "Assets/Saves/" );
}
// check to see if the file exists before tryign to read information
if (File.Exists(Application.dataPath + "/Saves/SavedGame.txt"))
{
// file exists, open the file for reading
var file = new StreamReader( Application.dataPath + "/Saves/SavedGame.txt");
// read the information stored in the file and store them in variables
currentLevel = parseInt(file.ReadLine());
CurrentSpawnPointIndex = parseInt(file.ReadLine());
PlayerHealth = parseInt(file.ReadLine());
AmmoPrime = parseInt(file.ReadLine());
AmmoAlt = parseInt(file.ReadLine());
Missions[0] = parseInt(file.ReadLine());
Missions[1] = parseInt(file.ReadLine());
Missions[2] = parseInt(file.ReadLine());
// close the file
file.Close();
}
// file doesn exists, so create it with default values (New Game)
else
{
// set default values for the New Game being created
// open the file for Writing
// write the information to the file
}
// load levels with the default or loaded values
LoadingLevels();
while(1)
{
var SpawnPlace : GameObject = GameObject.FindWithTag("spawnPoint" + CurrentSpawnPointIndex);
if(Application.GetStreamProgressForLevel(currentLevel) == 1 && SpawnPlace != null)
{
SpawnPlayer(CurrentSpawnPointIndex);
SetStats(PlayerHealth, AmmoPrime, AmmoAlt);
break;
}
else
{
yield WaitForSeconds(1);
}
}
}
//function used to load levels when game starts
function LoadingLevels()
{
// check mission 1 status, if its not done, load level 1
if(!Missions[0])
{
Application.LoadLevel(currentLevel);
}
// check mission 2 status, if its not done, load level 1 and 2
else if(!Missions[1])
{
Application.LoadLevel(currentLevel);
Application.LoadLevelAdditiveAsync(currentLevel+1);
Debug.Log("Loading Level 1");
}
// check mission 3 status, if its not done, load level 1, 2 and 3
else if(!Missions[2])
{
Application.LoadLevel(currentLevel);
Application.LoadLevelAdditive(currentLevel - 1);
Debug.Log("Loading Level 2");
}
}
//frontend
function OnGUI()
{
if (Application.loadedLevel == 0 ) // start a New Game
{
// Create the Start button through code.
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "New Game/ Continue"))
{
// start the game from where the player last did well
Initialize();
}
}
else if (levelState == "Dead") // is the Player dead
{
// Create the Load Last Checkpoint button through code
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "Load last checkpoint"))
{
// start the game from where the player last did well
Initialize();
}
// Create the Go to Main Menu button through code
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2 + 30, 180, 20), "Go To Main Menu"))
{
// load the Start Screen
Application.LoadLevel(0);
}
}
}
static public function MissionStatusCheck (missionIndex : int)
{
// what is the status of the mission
return Missions[missionIndex];
}
// function is used to change the mission status, was it passed?
static public function SetMissionStatus( missionIndex : int, status : int)
{
// status in the array is updated
Missions[missionIndex] = status;
}
//changing level states
static function SetLevelState(newState : String)
{
levelState = newState;
}
static public function SetCheckPoint( newCheckPoint : int )
{
CurrentSpawnPointIndex = newCheckPoint;
}
static public function SetCurrentLevel (newLevel : int)
{
currentLevel = newLevel;
}
Comment
Your answer
Follow this Question
Related Questions
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer
Save and Load Questions 0 Answers
Null Reference Exception even after check 1 Answer
Create a terrain prefab? Saving a terrain? No? 1 Answer
nice Save but wrong load 0 Answers