This site requires JavaScript, please enable it in your browser!
Greenfoot back
Crabbattle
Crabbattle wrote ...

2013/9/2

Easy save/ load game

Crabbattle Crabbattle

2013/9/2

#
Hi there! I'm working on a rpg with greenfoot but there is a huge problem i have. A rpg is normally a game in which your actions causes a shift in the enviroment or in the "phase" the npc are in ( i mean that every quest bring you much further in a row of quests / locations) Is there a easy way to maybe "freeze" the game , save it and unfreeze it after you restarted it? it should save EVERYTHING .. not just the objects and their locations or something.. i'd be very greatful for andy help :)
SPower SPower

2013/9/2

#
Well, it won't work on the gallery, but I guess you could write everything to a .txt file. Something like this I guess:
// at the top of a class:
import java.io.*;

// where you save:

// 1) setup:
// I don't know for sure if the file specified here has to already exist, you may need to research on that.
try {
    BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));

    // 2) Create data to store

    // 3) Actually store the data.
    // You can do this by doing this:
    bw.write(<string to save>+"\n");
    bw.newLine();
    // for every line to save. Replace <string to save> with a String to save in the file.

    // 4) Close it:
    bw.close();
} catch (Exception e) {
    System.err.println("Error: "+e.getMessage());
}
SPower SPower

2013/9/2

#
And then, you can load it doing:
try {
    InputStream is = getClass().getClassLoader().getResourceAsStream(FOLDER+file);
    if (is == null) {
        System.err.println("The file: "+file+" does not exist.");
        return;
    }
    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    String l = r.readLine();
    while (l != null) { // the line will be null when the end of the file is reached
        // process the current line

        l = r.readLine(); // load the next line
    }
    r.close(); // close it
} catch (Exception e) {
    System.err.println("Error: "+e.getMessage());
}
Crabbattle Crabbattle

2013/9/2

#
i alsoe thought about saving with textfiles. but how i write for EVERY object and npc in which phase it is? for example when you first talk to an npc after loading the game he should not say the same thing as the first time youve talked to him after starting the game the first time .. also npcs probably move away or the whole world changed..
SPower SPower

2013/9/2

#
Well, you'll need to set ALOT of properties on your 'npc's and save and load them in your files, which won't be that easy. Considering that, creating a 'scripting egine' to create an enviroment, and setting the properties of your npcs using that. For an example, visit the Nightmare emergence scenario of Builderboy2005, which has a very good one. But if you want to create one yourself, you can use the code I just gave you (it's from my scripting engine). From experience, I can tell you it's not very easy. It will at least take a lot of time.
You need to login to post a reply.