{"id":286,"date":"2010-10-05T21:59:20","date_gmt":"2010-10-05T20:59:20","guid":{"rendered":"http:\/\/blogs.kent.ac.uk\/mik\/?p=286"},"modified":"2020-05-15T15:45:58","modified_gmt":"2020-05-15T15:45:58","slug":"you-can-save-the-world","status":"publish","type":"post","link":"https:\/\/blogs.kcl.ac.uk\/proged\/2010\/10\/05\/you-can-save-the-world\/","title":{"rendered":"You can Save The World"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/saving-the-world1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-288\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/saving-the-world1.jpg\" alt=\"\" width=\"240\" height=\"163\" \/><\/a>Earlier today I wrote about the <a href=\"http:\/\/blogs.kent.ac.uk\/mik\/2010\/10\/05\/greenfoot-2-0-the-new-features\/\">new Greenfoot<\/a> features. I forgot one: Saving the world.<\/p>\n<p>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.)<\/p>\n<p>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.<\/p>\n<p>So we come to the rescue: Saving the World.<\/p>\n<p>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&#8217;s look at an example.<\/p>\n<p>Let us assume I want a scenario where the initial objects at the start of the game are a hamburger and three pigs. (Don&#8217;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.<\/p>\n<div id=\"attachment_289\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/pigs.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-289\" class=\"size-full wp-image-289\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/pigs.png\" alt=\"\" width=\"580\" height=\"458\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs.png 580w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs-300x237.png 300w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs-380x300.png 380w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-289\" class=\"wp-caption-text\">Figure 1: Three pigs and a hamburger<\/p><\/div>\n<p>This is all well so far \u2013 with the problem that the actors will disappear (and I have to manually recreate them) after every compile.<\/p>\n<p>Enter <em>Save the World<\/em>.<\/p>\n<p>In Greenfoot 2.0, I can now select Save the World from the world context menu (Figure 2).<\/p>\n<div id=\"attachment_290\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/pigs2.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-290\" class=\"size-full wp-image-290\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/pigs2.png\" alt=\"\" width=\"580\" height=\"458\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs2.png 580w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs2-300x237.png 300w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/pigs2-380x300.png 380w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-290\" class=\"wp-caption-text\">Figure 2: Saving the world<\/p><\/div>\n<p>When I choose this function, Greenfoot writes the source code for a method called &#8220;prepare&#8221; 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).<\/p>\n<div id=\"attachment_291\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/editor1.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-291\" class=\"size-full wp-image-291\" src=\"http:\/\/blogs.kcl.ac.uk\/proged\/wp-content\/blogs.dir\/192\/files\/2010\/10\/editor1.png\" alt=\"\" width=\"580\" height=\"433\" srcset=\"https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/editor1.png 580w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/editor1-300x224.png 300w, https:\/\/blogs.kcl.ac.uk\/proged\/files\/2010\/10\/editor1-402x300.png 402w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-291\" class=\"wp-caption-text\">Figure 3: The generated &#039;prepare&#039; method<\/p><\/div>\n<p>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.<\/p>\n<p>There are a few characteristics worth noting about this mechanism:<\/p>\n<ul>\n<li>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.<\/li>\n<li>The source code of the automatically generated &#8216;prepare&#8217; 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.<\/li>\n<li>When the world was already saved, and you save it again, the prepare method will be adapted appropriately.<\/li>\n<li>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).<\/li>\n<li>After the scenario has run, the world cannot be saved anymore.<\/li>\n<\/ul>\n<p><em><span style=\"color: #888888\">(All screenshots in this post are produced by the author and are hereby placed in the public domain.)<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blogs.kcl.ac.uk\/proged\/2010\/10\/05\/you-can-save-the-world\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":179,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,11],"tags":[53],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-greenfoot-software-tools","category-software-tools","tag-greenfoot-software-tools"],"_links":{"self":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/users\/179"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":1,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":1023,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/posts\/286\/revisions\/1023"}],"wp:attachment":[{"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.kcl.ac.uk\/proged\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}