Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Saving/Loading Script Settings


    qbots

    Recommended Posts

    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

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.