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

2015/3/25

Saving and loading

ninjasperm99 ninjasperm99

2015/3/25

#
im having quite a bit of trouble with undersanding how to go about making a save system for a game that im making i need it for saving the actors position. it needs to be local so i was thinking of using a txt file like others on this site have done but im lost as far to how to go abou making this save and load system
fejfo fejfo

2015/3/25

#
I made an IoHelp class for save and loading but it doesn't work on the greenfoot site for sucurety reasons
import java.io.*;
import java.util.*;
import greenfoot.*;

public class IoHelp implements Serializable
{
    String main,save,Class,object,image,objectX,objectY,objectRotation;

    public IoHelp() {
        makeSaveFolders();
    }   

    public void makeSaveFolders() {
       //comment the ones u don't need
        main = "main/"; // prefeebly swap this by the project name
        save = main + "save/";
        //Class = main + "class/";
        //object = main + "object/";
        image = main + "image/";
        //objectX = object + "x/";
        //objectY = object + "y/";
        //objectRotation = object + "rotation/";
        makeFolder(main);
        makeFolder(save);
        //makeFolder(Class);
        //makeFolder(object);
        makeFolder(image);
        //makeFolder(objectX);
        //makeFolder(objectY);
        //makeFolder(objectRotation);
    }

    public void makeFolder(String folderName) {
        File folder = new File( folderName );            
        folder.mkdir();
        //folder.close();
    }

    public void saveFile( ArrayList<String> lines, String fileName )
    {
        try  {
            File file = new File(fileName); 
            //file.mkdir();
            file.createNewFile();

            PrintWriter out = new PrintWriter(file);
            for(String text: lines) {
                out.println(text);
            }
            out.close();
            //println("Done writing to " + fileName); //For testing 
        }
        catch(IOException e)
        {
            System.out.println("IoHelp: saveFile Error: " + e);
            e.printStackTrace( );
            //out.close();
        }
    } 
    
    public void println(String line) {
       System.out.println(line);
    }

    public ArrayList<String> loadFile(String fileName) {        
        ArrayList<String> list = new ArrayList<String>();
        try {
            File file = new File( fileName );
            Scanner in = new Scanner(file);
            try {
                while(true) {
                    list.add(in.nextLine());
                }                
            } catch(NoSuchElementException e) {
                in.close();
            }
        } catch(IOException ex) {
            println("IoHelp: loadFile ERROR: " + ex + "making file");
            try {
                File file = new File(fileName); 
                file.createNewFile();
            } catch (IOException e) {
                println("IoHelp: saveFile Error: " + e);
                e.printStackTrace( );
            }
        }

        return list;
    }

    //Greenfoot only
    public void saveActor(java.util.ArrayList<greenfoot.Actor> objects,String fileName) {
        //saving object
        try {            
            FileOutputStream file = new FileOutputStream(object+fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(Actor o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveActor ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
        //saving lockation
        //make arrays with all the lockations and rotation
        ArrayList<String> x = new ArrayList();
        ArrayList<String> y = new ArrayList();
        ArrayList<String> ro = new ArrayList();
        for(Actor o: objects) {
            if(o.getWorld() != null) {
                x.add(Integer.toString(o.getX()));
                y.add(Integer.toString(o.getY()));
                ro.add(Integer.toString(o.getRotation()));
            } else {
                x.add("not in world");
                y.add("not in world");
                ro.add(Integer.toString(0));
            }
        }  
        //save lockations
        saveFile(x,objectX+fileName);
        saveFile(y,objectY+fileName);
        saveFile(ro,objectRotation+fileName);
    }

    public void saveWorld(ArrayList<World> objects,String fileName) {
        try {
            FileOutputStream file = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(World o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveWorld ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
    }

    public ArrayList<Actor> loadActor(String fileName,World caller) {
        ArrayList<Actor> list = new ArrayList<Actor>();
        try {
            FileInputStream file = new FileInputStream(object + fileName); 
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((Actor) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load Actor class ERROR: " + cnfex);
                    }
                } 
            } catch(java.io.EOFException e) {
                //ignore this 
            }     
            in.close();
        } catch(IOException ex) {
            if(ex.toString() == "java.io.EOFException") {
                //ingnore it
            }else { 
                println("IoHelp: load Actor file ERROR: " + ex);
                try {
                    File file = new File(object + fileName); 
                    file.createNewFile();
                } catch (IOException e) {
                    println("IoHelp: saveFile Error: " + e);
                    e.printStackTrace( );
                }  
            }
        }
        //got the actors adding them to the world if they were in the world
        ArrayList<String> textX = loadFile(objectX + fileName);
        ArrayList<String> textY = loadFile(objectY + fileName);
        ArrayList<String> textRo = loadFile(objectRotation + fileName);
        for(int i = 0; i < list.size(); i++) {
            Actor o = list.get(i);
            try {
                int x = Integer.parseInt(textX.get(i));
                int y = Integer.parseInt(textY.get(i));
                int ro = Integer.parseInt(textRo.get(i));
                caller.addObject(o,x,y);
                o.setRotation(ro);
            } catch(NumberFormatException NFEx) {
                //println("object: " + " wasn't in world");
            }
        }

        return list;        
    }

    public ArrayList<World> loadWorld(String fileName) {
        ArrayList<World> list = new ArrayList<World>();
        try {
            FileInputStream file = new FileInputStream(fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((World) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load World class ERROR: " + cnfex);
                    }
                } 
            } catch(EOFException e) {
                //ignore this 
            }
            in.close();
        } catch(IOException ex) {
            println("IoHelp: load World file ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //in.close();
        }
        return list;
    }
}
Super_Hippo Super_Hippo

2015/3/25

#
You don't have to write such a file yourself. You can use the 'UserInfo' class which comes with Greenfoot. Double-click on the 'World' class. It will open the Greenfoot API. Go one class back and you will find the 'UserInfo' class. It can be used to store a maximum of 11 integers and five Strings.
fejfo fejfo

2015/3/25

#
that is to save on the Greenfoot site but he wants to save in a file and I have already wrote it so why don't allow him to use it ?
Super_Hippo Super_Hippo

2015/3/25

#
I didn't say to not allow it, but the UserInfo class also creates a file. It's a .csv (Excel) file and not a .txt file but it works as well (without having a new large class). It's simply an easier way which usually works, too. And it also can be used online if it is wanted in the future.
ninjasperm99 ninjasperm99

2015/3/26

#
fejfo wrote...
I made an IoHelp class for save and loading but it doesn't work on the greenfoot site for sucurety reasons
import java.io.*;
import java.util.*;
import greenfoot.*;

public class IoHelp implements Serializable
{
    String main,save,Class,object,image,objectX,objectY,objectRotation;

    public IoHelp() {
        makeSaveFolders();
    }   

    public void makeSaveFolders() {
       //comment the ones u don't need
        main = "main/"; // prefeebly swap this by the project name
        save = main + "save/";
        //Class = main + "class/";
        //object = main + "object/";
        image = main + "image/";
        //objectX = object + "x/";
        //objectY = object + "y/";
        //objectRotation = object + "rotation/";
        makeFolder(main);
        makeFolder(save);
        //makeFolder(Class);
        //makeFolder(object);
        makeFolder(image);
        //makeFolder(objectX);
        //makeFolder(objectY);
        //makeFolder(objectRotation);
    }

    public void makeFolder(String folderName) {
        File folder = new File( folderName );            
        folder.mkdir();
        //folder.close();
    }

    public void saveFile( ArrayList<String> lines, String fileName )
    {
        try  {
            File file = new File(fileName); 
            //file.mkdir();
            file.createNewFile();

            PrintWriter out = new PrintWriter(file);
            for(String text: lines) {
                out.println(text);
            }
            out.close();
            //println("Done writing to " + fileName); //For testing 
        }
        catch(IOException e)
        {
            System.out.println("IoHelp: saveFile Error: " + e);
            e.printStackTrace( );
            //out.close();
        }
    } 
    
    public void println(String line) {
       System.out.println(line);
    }

    public ArrayList<String> loadFile(String fileName) {        
        ArrayList<String> list = new ArrayList<String>();
        try {
            File file = new File( fileName );
            Scanner in = new Scanner(file);
            try {
                while(true) {
                    list.add(in.nextLine());
                }                
            } catch(NoSuchElementException e) {
                in.close();
            }
        } catch(IOException ex) {
            println("IoHelp: loadFile ERROR: " + ex + "making file");
            try {
                File file = new File(fileName); 
                file.createNewFile();
            } catch (IOException e) {
                println("IoHelp: saveFile Error: " + e);
                e.printStackTrace( );
            }
        }

        return list;
    }

    //Greenfoot only
    public void saveActor(java.util.ArrayList<greenfoot.Actor> objects,String fileName) {
        //saving object
        try {            
            FileOutputStream file = new FileOutputStream(object+fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(Actor o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveActor ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
        //saving lockation
        //make arrays with all the lockations and rotation
        ArrayList<String> x = new ArrayList();
        ArrayList<String> y = new ArrayList();
        ArrayList<String> ro = new ArrayList();
        for(Actor o: objects) {
            if(o.getWorld() != null) {
                x.add(Integer.toString(o.getX()));
                y.add(Integer.toString(o.getY()));
                ro.add(Integer.toString(o.getRotation()));
            } else {
                x.add("not in world");
                y.add("not in world");
                ro.add(Integer.toString(0));
            }
        }  
        //save lockations
        saveFile(x,objectX+fileName);
        saveFile(y,objectY+fileName);
        saveFile(ro,objectRotation+fileName);
    }

    public void saveWorld(ArrayList<World> objects,String fileName) {
        try {
            FileOutputStream file = new FileOutputStream(fileName);
            ObjectOutputStream out = new ObjectOutputStream(file);
            for(World o: objects) {
                out.writeObject(o);
            }
            out.close();
        } catch(IOException ex) {
            println("IoHelp: saveWorld ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //out.close();
        }
    }

    public ArrayList<Actor> loadActor(String fileName,World caller) {
        ArrayList<Actor> list = new ArrayList<Actor>();
        try {
            FileInputStream file = new FileInputStream(object + fileName); 
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((Actor) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load Actor class ERROR: " + cnfex);
                    }
                } 
            } catch(java.io.EOFException e) {
                //ignore this 
            }     
            in.close();
        } catch(IOException ex) {
            if(ex.toString() == "java.io.EOFException") {
                //ingnore it
            }else { 
                println("IoHelp: load Actor file ERROR: " + ex);
                try {
                    File file = new File(object + fileName); 
                    file.createNewFile();
                } catch (IOException e) {
                    println("IoHelp: saveFile Error: " + e);
                    e.printStackTrace( );
                }  
            }
        }
        //got the actors adding them to the world if they were in the world
        ArrayList<String> textX = loadFile(objectX + fileName);
        ArrayList<String> textY = loadFile(objectY + fileName);
        ArrayList<String> textRo = loadFile(objectRotation + fileName);
        for(int i = 0; i < list.size(); i++) {
            Actor o = list.get(i);
            try {
                int x = Integer.parseInt(textX.get(i));
                int y = Integer.parseInt(textY.get(i));
                int ro = Integer.parseInt(textRo.get(i));
                caller.addObject(o,x,y);
                o.setRotation(ro);
            } catch(NumberFormatException NFEx) {
                //println("object: " + " wasn't in world");
            }
        }

        return list;        
    }

    public ArrayList<World> loadWorld(String fileName) {
        ArrayList<World> list = new ArrayList<World>();
        try {
            FileInputStream file = new FileInputStream(fileName);
            ObjectInputStream in = new ObjectInputStream(file);
            try {
                while(true) {
                    try {
                        list.add((World) in.readObject());
                    } catch(ClassNotFoundException cnfex) {
                        println("IoHelp: load World class ERROR: " + cnfex);
                    }
                } 
            } catch(EOFException e) {
                //ignore this 
            }
            in.close();
        } catch(IOException ex) {
            println("IoHelp: load World file ERROR: " + ex + " file: '"+ fileName +"' probely doesn't exist");
            //in.close();
        }
        return list;
    }
}
using this code how do i go about making it load?
ninjasperm99 ninjasperm99

2015/3/26

#
like how do i access it while the code is running do i need to make a button for it or right click etc?
You need to login to post a reply.