Hi everyone,
I am looking for an example of what would be needed to create a save game. I am giving a shot at my first project, a little text based RPG, i dont know what i need to put in the save game, how to save it and how to load it back.
Can anyone help ?
Thanx alot
How to create save games and load them !
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike GoldRank #50 · 30.6K pointsPosted Jun 5, 2007, 1:25 PM EDT
What's a save game? Do you mean how do you persist the information inside your RPG game so that you can reload it later?
You could save your RPG data in an XML file. In this case, you can use the XMLDocument class do to all of your persistence. Use the Load Method to load the data and the Save method to save the data.
Best,
-Mike
Christian Parent11 pointsPosted Jun 5, 2007, 2:15 PM EDT
I really dont know what i have to do.... Do i have to save those info with the character name as the file name, save all the variables that have those info (saving the value of int gold). Then after, how do i get that info back from the file and put it back in the variable ?
If i could find a code for this kind of game, and those kind of functions, i could easily analyse it and get my answers (I usualy learn more that way)
Former memberPosted Jun 5, 2007, 7:55 PM EDT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ExamplesApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
//Declare variables
string name, address, weight;
//Load variables
name = nameBox.Text;
address = addressBox.Text;
weight = weightBox.Text;
//Save information to a text file, each individual segment of information
//is separated by comma's used as delimiter's for later splitting on load.
FileStream fsInput = new FileStream("info.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter swInput = new StreamWriter(fsInput);
swInput.WriteLine("{0},{1},{2}", name, address, weight);
swInput.Close();
fsInput.Close();
}
private void loadButton_Click(object sender, EventArgs e)
{
//Declare variables
string name, address, weight;
//Open file containing info
FileStream fsInput = new FileStream("info.txt", FileMode.Open, FileAccess.Read);
StreamReader swReader = new StreamReader(fsInput);
//Read data from file, then split it up into individual variables
string nextLine = swReader.ReadLine();
string[] inputData = nextLine.Split(Convert.ToChar(","));
name = inputData[0];
address = inputData[1];
weight = inputData[2];
//Finally, put variables to boxes
nameBox.Text = Convert.ToString(name);
addressBox.Text = Convert.ToString(address);
weightBox.Text = Convert.ToString(weight);
}
}
}
Hope this helps you out or its at least something you can work with. Itll save data to a file, and then load it later. Good luck and if you need any more help, dont be afraid to ask!
-Scrap