Nomis - Trailer/Screenshots

Keep up to date!
Keep up to date!
Keep up to date!


Keep up to date!

Designed by:
SiteGround web hosting Joomla Templates
Saving players position
Written by Jeff Brown   
Share

Save/Load players position


Save/Load feature in Platform Starter Kit. (Saves the player's position, so you can start where you left off)


In this tutorial I'm going to create a hotkey (S) that saves the player's current position into a text file. You can then use this text file when loading a level to have your player start where you left off. You could even combine this with the checkpoint tutorial, so if you save and quit you'll load at the last checkpoint you hit. Splicing things together is an awesome way to learn (for me at least) so I'll leave that to you.


At the top of Player.cs we need to add the following using statements:

using System.IO;

In Player.cs add the following after Vector2 position:
Vector2 loadedPosition;

 

Now in the Player constructor, add the following if/else statement, overwritting the current Reset method:

//What this does is check to see if you have a save file, and load it if you DO have one.
//Otherwise it loads your player as it did before.
if (File.Exists("playerPosition.txt"))
{
     LoadPosition("playerPosition.txt");
     Reset(loadedPosition);
}
else
     Reset(position);


Now in the GetInput method we'll add a hotkey to save our game:
//This should drop right in as long as you place it under KeyboardState keyboardState...
//This shouldn't be the final version of your save, because if you hold down the S button
//the save function will be called a lot of times... which usually isn't good. :D
if(keyboardState.IsKeyDown(Keys.S))
     SavePosition("playerPosition.txt");

At the very bottom, I added the next couple methods, right after Draw().
private void SavePosition(string path)
{
    //using the default Platform Starter Kit, the players position can be accessed using 'position'
    //so when using position below in the save function, a Vector2 will be saved with the
    //current player's position
    using (StreamWriter writer = new StreamWriter(path))
    {
        //We call this twice, and save the X variable on the 1st line, and the Y on the second.
        //This makes things really easy when loading the values in LoadPosition().
        writer.WriteLine(position.X);
        writer.WriteLine(position.Y);
    }
}

private void LoadPosition(string path)
{
     using (StreamReader reader = new StreamReader(path))
     {
          //Reads one line each time we call it, and since it's only 2 lines the following
          //should do fine for now.
          string line = reader.ReadLine();
          loadedPosition.X = int.Parse(line);

          string line2 = reader.ReadLine();
          loadedPosition.Y = int.Parse(line2);
     }
}

As always, drop me a line if something doesn't work or you'd like to suggest a tutorial or just need some help. JeffBrown@RobotFootGames.com

Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.