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


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()); } |
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 ; } |