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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
     * 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
     * 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.