Earlier today I wrote about the new Greenfoot features. I forgot one: Saving the world.
In previous editions of Greenfoot, there was one annoying aspect: every time you had to recompile your classes, your interactively created objects disappeared. (This was not a bug: it is necessary, because the class implementing these objects does not exist anymore.)
This could be overcome by writing an initialiser method in the world class that creates and inserts all the objects you want to have in the world. This solves the problems, since objects get recreated automatically. However, writing these initialiser methods can be tedious.
So we come to the rescue: Saving the World.
You can now insert the objects you initially want in the world interactively, and then save the visible state of the world as the starting state of your program. Let’s look at an example.
Let us assume I want a scenario where the initial objects at the start of the game are a hamburger and three pigs. (Don’t ask). Let us further assume that I have just created the pigs and the hamburger interactively and placed them into my world. Figure 1 shows my world at this stage.
This is all well so far – with the problem that the actors will disappear (and I have to manually recreate them) after every compile.
Enter Save the World.
In Greenfoot 2.0, I can now select Save the World from the world context menu (Figure 2).
When I choose this function, Greenfoot writes the source code for a method called “prepare” into the class of the world object. This method contains code to create and insert objects just as they are in the world, and a call to this method from the world constructor (Figure 3).
The effect is that next time, when I recompile my class and the world is re-created, the objects in the world are re-created as well. The status of the world is saved.
There are a few characteristics worth noting about this mechanism:
- We consciously chose to create source code (rather than, say, serialising the world objects) because it can serve as a good learning instrument. Students can study the generated code and learn from it.
- The source code of the automatically generated ‘prepare’ method may be freely edited by hand. There is not much special about this method, and you are welcome to adapt it in the editor in any way you feel like.
- When the world was already saved, and you save it again, the prepare method will be adapted appropriately.
- The prepare method will include code to recreate exactly what you did to construct the world. For example, if you create and place an object, and then move it, the prepare method will add the object at first at the same position where you first added it, and then move it. This is necessary, because the sequence may have had side effects. If you are only interested in the final position, you can streamline the prepare method manually by editing it (and taking out any unnecessary calls).
- After the scenario has run, the world cannot be saved anymore.
(All screenshots in this post are produced by the author and are hereby placed in the public domain.)
Aaah it is realy useful! i didnt have to save the world condition in an int array 😀 like in my scenario http://greenfootgallery.org/scenarios/1178
How about method saveAsLevel()? so its not only in the prepare() method. But its also level1(), level2(), … or level(int lv) ?
So you can save the world as a level to be called sometimes.