qbots 239 Share Posted August 12, 2015 I've had a couple people ask me how to add a save/load feature to their scripts, this is a pretty basic way to do it. (I'm pretty bad at explaining so lemme know if something is confusing) First off, this is in no way gonna be the OPTIMAL way to do it, but it isn't a bad way either. First, create a class Settings (Not entirely necessary but up2u) This class will have 2 static methods: saveSettings and loadSettings After you have that class it's probably a good idea to decide how you will need to parse the data, for me I simply put my data into a string simliar to this: 17011|3122|7021 The first item is an object ID, and the next 2 are related to the coordinates. So let's go ahead and save this data: //Frame component will be the class instance containing all your settings you want to save, must extend JFrame public static void saveSettings(Frame component) { final JFileChooser fc = new JFileChooser(); int ret = fc.showSaveDialog(component); //This is where we get the user's input if(ret == JFileChooser.APPROVE_OPTION) { //Make sure they selected a file File file = fc.getSelectedFile(); try { ArrayList<String> strings = new ArrayList<>(); try { //this part will be different based on your script, but this is where you add lines to the strings list to be put into the output file (Rock#toString returns a formatted string as shown above) for (Object r : ((DefaultListModel<Rock>)component.list2.getModel()).toArray()) strings.add(r.toString()); } catch(Exception re) {} //Finally write to the file Files.write(file.toPath(),strings); } catch (IOException e) { e.printStackTrace(); } } } And now, if you look where you saved the settings you should see a file containing the info you put. Now for loading, I'm not gonna give you everything because this will change depending on how your data is set up, but here is a method that loads all the data: public static void loadSettings(Frame component) { final JFileChooser fc = new JFileChooser(); int ret = fc.showOpenDialog(component); File file = fc.getSelectedFile(); try { java.util.List<String> lines = Files.readAllLines(file.toPath()); DefaultListModel<Rock> model = new DefaultListModel<>(); for(String s : lines) { component.m.log("Entry: " + s); //This is where u grab data final Tile t = new Tile(Integer.parseInt(s.split("\\|")[1]),Integer.parseInt(s.split("\\|")[2])); final Rock r = new Rock(component.m.getGameObjects().closest(obj -> obj.getName().equals("Rocks") && obj.getID() == Integer.parseInt(s.split("\\|")[0]) && obj.getTile().equals(t))); //THIS IS WHERE YOU ADD YOUR DATA TO THE CURRENT SCRIPT } } catch (IOException e) { } } There are 2 alternatives to this: (I wont explain these right now though) (thx dog for reminding me of them) http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html Link to comment Share on other sites More sharing options...
Dogerina 330 Share Posted August 12, 2015 aaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Link to comment Share on other sites More sharing options...
qbots 239 Author Share Posted August 12, 2015 aaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy yeah ik. like i said theres definitely better methods lol Link to comment Share on other sites More sharing options...
Dreamlicker 750 Share Posted August 12, 2015 Thanks for the tut Qbot, Haven't seen an implementation of this nature before. I'm a fan of buffered in/out streams. aaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy If you could show us an implementation of that, I'd love to see it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.