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

2012/12/23

How to save and load Worlds using methods.

finnland finnland

2012/12/23

#
For my level Editor I need a method which saves and loads Worlds. Would be greate if you have a solution. Or in german ;) : Ich brauche eine Funktion, die eine Welt speichert, wenn ich eine Methode ausführe. Das geht leider nicht über die Standardfunktion (Rechtsklick auf die Welt und dan "save the World"), da ich das für meinen Leveleditor brauche. Wäre super, wenn ihr eine Antwort habt. (Ich bin erst Java-Anfänger ;) )
finnland finnland

2013/1/9

#
Does nobody have an idea?? I simply need a method who saves all actors in the world (most best would be in a String). And than I need another method to reload the saved data (with addObject for example.)
Leaderchicken Leaderchicken

2013/1/9

#
Du könntest versuchen dir über world.getObjects(Class) die Liste aller Objekte in der Welt zu holen. Die Objekte brauchen dann noch eine Methode die Ihre wichtigen Werte(Position, Rotation) in eine Datei schreiben. Guck dir dazu mal java.io.FileWriter und Reader an.
danpost danpost

2013/1/9

#
Just be aware that you cannot write to a file on the Greenfoot site. The best you can do is use the UserInfo class to save any information (which would be 11 ints and 5 Strings of 50 characters each, per user).
finnland finnland

2013/1/14

#
i have saved a String inside the game. As an example: I called this string "save". In this String i saved the content: "2 5 Wand". 2 is the x.coordinate, 5 the y-coordinate and "Wand" is german for a class called Wand (Wall). Now i want to call the Method addObject() and I want to implement the String in it. In the end there will have to stand: addObject(new Wand(), 2, 5). My question is: How can I use the Method addObject by implementing a string into it??
danpost danpost

2013/1/14

#
You just need to be able to parse the string.
String save = "2 5 Wand";
int x = Integer.valueOf(save.substring(0, save.indexOf(" ")));
save = save.substring(save.indexOf(" ")+1);
int y = Integer.valueOf(save.substring(0, save.indexOf(" ")));
save = save.substring(save.indexOf(" ")+1);
if ("Wand".equals(save)) addObject(new Wand(), x, y);
// add an equivalent 'if' for each actor type that can be added
There may be a way to do this a little more simply, but this should get the job done.
finnland finnland

2013/1/15

#
Thank you verry much :) Thats exactly what I was looking for. But now I have a 14x9 Cell game. That would be a total ammound of 126 Cells. So there can be a max. of 126 lines in the "save" String. Like: 2 5 Wand 1 1 Wand 2 6 Wand 11 9 Wand 10 4 Wand and so on... Can I do this Code above withe a "while" of "for". Because without it would be a hell of a code. Sorry for this many questions (and the bad english) but I am only a beginner (and german) ;)
danpost danpost

2013/1/15

#
It would depend on where you are getting the strings from and/or where they are located. They could be in an array, where you would use a 'for' loop, or they could be being read from a file, where you would use a 'while' loop checking for 'no more records to read in'.
finnland finnland

2013/1/15

#
I used this code to save the location of the walls:
String save = "";
    public void save()  
    {  
      save="";
     int Wand = getWorld().getObjects(Wand.class ).size();  
     for(int i=0; i<Wand; i++)
     {  
        Wand t = (Wand) getWorld().getObjects(Wand.class).get(i); 
        save = save + t.getX() +" "+ t.getY() + " " + "Wand"  ;    
         save = save+"\n"  ;
     } 
  System.out.println(save);
}
When I call the Method the Terminal (for example) gives me this: 4 6 Wand 5 7 Wand 6 6 Wand 7 5 Wand 7 4 Wand But if I understood your code right, than we only would take the first line and "load" it.
danpost danpost

2013/1/15

#
Yes, the code I gave was just an example of how to parse the string. Now that I know you have just one string with each object seperated with the '\n' sequence, we can now write the whole code. BTW, I took notice that your string also ends with that sequence, which is perfectly fine and even helpful (see the condition for the 'while').
while(save.indexOf("\n")>-1)
{
    int x = Integer.valueOf(save.substring(0, save.indexOf(" ")));
    save = save.substring(save.indexOf(" ")+1);
    int y = Integer.valueOf(save.substring(0, save.indexOf(" ")));
    save = save.substring(save.indexOf(" ")+1);
    String actor = save.substring(0, save.indexOf("\n");
    save = save.substring(save.indexOf("\n")+2);
    if ("Wand".equals(actor)) addObject(new Wand(), x, y);
    // add an equivalent 'if' for each actor type that can be added
}
finnland finnland

2013/1/16

#
Thank you very much!! That it is!
danpost danpost

2013/1/16

#
Your string could be 1260 characters or more. Your next challenge, if your are up to it and have time for it, is to condense the data to 250 characters or less, so you can use the UserInfo storage to save and retrieve that data. I can show you a way to condense the data to 126 characters.
finnland finnland

2013/1/16

#
That would be very nice but first I need to implement the code and work on the editor. But as i said before: It works perfek (http://www.greenfoot.org/scenarios/6835) :)
Chun2019 Chun2019

2013/11/1

#
What is the load method code?? I am also dealing with the same problem but I don't know how to write the load method.
You need to login to post a reply.