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

2013/10/24

Saving a Game

RedCreeper RedCreeper

2013/10/24

#
I'm am making a RPG game and I would like to find out how to be able to save the current stated if the world, like what the player has equipped, in their inventory, where they are, who they have defeated. Please help! Thank you
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
Therefore you need to write down the data you have. You can do this either in a file when you are playing offline or in the UserInfo while playing online (here you have not much space for writing). To write in a file or in the UserInfo you can use some methods I road for this.
/**
     * Saves the Strings given as the second to the last parameter in the file named like given in filename.
     * 
     * @param filename
     *      The name of the file where the Strings should be saved.
     * 
     * @param addToExistingFile
     *      If you want to add the text to an existing file this variable has to be true;
     * 
     * @param fileText
     *      The strings that should be saved in the file.
     * 
     * @return
     *      Returns true if the file was successfully createt. False if not.
     */
    public boolean saveFile(String filename, boolean addToExistingFile, String ... fileText) {
        List<String> existingText = loadFile(filename);
        BufferedWriter file = null;
        try {
            file = new BufferedWriter(new FileWriter(filename));
            if (addToExistingFile) {
                for (String output : existingText) {
                    file.write(output);
                    file.write('\n');
                }
            }
            for (String output : fileText) {
                file.write(output);
                file.write('\n');
            }
            file.close();
        }
        catch (IOException ioe) {
            //ioe.printStackTrace();
            return false;
        }
        finally {
            try {
                file.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch (NullPointerException npe) {
                //npe.printStackTrace();
            }
        }
        return true;
    }
    
    /**
     * Saves the Strings given as the second to the last parameter in the file named like given in filename.
     * 
     * @param filename
     *      The name of the file where the Strings should be saved.
     * 
     * @param addToExistingFile
     *      If you want to add the text to an existing file this variable has to be true;
     * 
     * @param fileText
     *      The strings that should be saved in the file as a list.
     * 
     * @return
     *      Returns true if the file was successfully createt. False if not.
     */
    public boolean saveFile(String filename, boolean addToExistingFile, java.util.List<String> fileText) {
        List<String> existingText = loadFile(filename);
        BufferedWriter file = null;
        try {
            file = new BufferedWriter(new FileWriter(filename));
            if (addToExistingFile) {
                for (String output : existingText) {
                    file.write(output);
                    file.write('\n');
                }
            }
            for (String output : fileText) {
                file.write(output);
                file.write('\n');
            }
            file.close();
        }
        catch (IOException ioe) {
            //ioe.printStackTrace();
            return false;
        }
        finally {
            try {
                file.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch (NullPointerException npe) {
                //npe.printStackTrace();
            }
        }
        return true;
    }
    
    /**
     * Loads the text of the file whith the given filename.
     * 
     * @param filename
     *      The name of the file that should be loaded.
     * 
     * @return
     *      Returns a list of Strings consisting of the text of the file.
     *      Each line of the file is a new element of the list.
     */
    public java.util.List<String> loadFile(String filename) {
        ArrayList<String> fileText = new ArrayList<String>();
        BufferedReader file = null;
        try {
            file = new BufferedReader(new FileReader(filename));
            String input;
            while ((input = file.readLine()) != null) {
                fileText.add(input);
            }
        }
        catch (FileNotFoundException fnfe) {
            //fnfe.printStackTrace();
            return null;
        }
        catch (IOException ioe) {
            //ioe.printStackTrace();
            return null;
        }
        finally {
            try {
                file.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch (NullPointerException npe) {
                //npe.printStackTrace();
            }
        }
        return fileText;
    }
    
    /**
     * Deletes the content of a file.
     * 
     * @param filename
     *      The name of the file that should be deleted.
     * 
     * @return
     *      Returns true if the file has ben deleted or if the file didn't exist.
     *      Returns false if the file couldn't be deleted.
     */
    public boolean deleteFile(String filename) {
        File file = new File(filename);
        if(!file.delete()) {
            BufferedWriter fileWriter = null;
            try {
                fileWriter = new BufferedWriter(new FileWriter(filename));
                fileWriter.write("");
                fileWriter.close();
            }
            catch (FileNotFoundException fnfe) {
                //fnfe.printStackTrace();
                return true;
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
                return false;
            }
            finally {
                try {
                    fileWriter.close();
                }
                catch (IOException ioe2) {
                    ioe2.printStackTrace();
                }
                catch (NullPointerException npe) {
                    //npe.printStackTrace();
                }
            }
            return true;
        }
        return true;
    }
    
    /**
     * Check whether a file with the given name is currently existing.
     * 
     * @param filename
     *      The name of the file that should be checked.
     * 
     * @return
     *      Returns true if the file is existing.
     *      Returns false if the file is not found or if there was a IOException.
     */
    public boolean fileExisting(String filename) {
        BufferedReader file = null;
        try {
            file = new BufferedReader(new FileReader(filename));
        }
        catch (FileNotFoundException fnfe) {
            //fnfe.printStackTrace();
            return false;
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
        finally {
            try {
                file.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
            catch (NullPointerException npe) {
                //npe.printStackTrace();
            }
        }
        return true;
    }
    
    /**
     * Returns a list of Strings concerning the names of all existing files in the choosen directiory.
     */
    public java.util.List<String> getExistingFileNames(String path) {
        java.io.File file;
        List<java.io.File> files;
        ArrayList<String> fileNames = new ArrayList<String>();
        file = new java.io.File(path);
        files = Arrays.asList(file.listFiles());
        for (java.io.File temp : files) {
            fileNames.add(temp.getName());
        }
        return fileNames;
    }
    
    /**
     * Returns a list of all files in the choosen directory.
     */
    public java.util.List<java.io.File> getExistingFiles(String path) {
        java.io.File file = new java.io.File(path);
        return Arrays.asList(file.listFiles());
    }
Also this method can be helpfull to check whether you are currently playing online or offline:
/**
     * Check whether the game is played online (on the Greenfoot webside) or offline.
     * 
     * @return
     *      Returns true if the game is currently played online.
     */
    public boolean playingOnline() {
        try {
            BufferedWriter file = new BufferedWriter(new FileWriter("checkingFile_dh48ch30ch3c5.txt"));
            file.write("Test String");
            file.close();
            deleteFile("checkingFile_dh48ch30ch3c5.txt");
        }
        catch (Exception e) {
            //ioe.printStackTrace();
            return true;
        }
        return false;
    }
Hope this helps you.
RedCreeper RedCreeper

2013/10/25

#
Exactly what does this save I see that is saves a string but like does it save the current state of the game
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
The strings you save should be the data you need to load the current state. You need to put all the information in a String and then save this string in a file or the UserInfo.
RedCreeper RedCreeper

2013/10/26

#
How would I save a characters location
Gevater_Tod4711 Gevater_Tod4711

2013/10/26

#
You can choose your own conventions for doing this. You e.g. could save his name and his current coordinates in the world. Or also save in which level he is ...
You need to login to post a reply.