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

2013/8/10

Saving games with Greenfoot?

K_wow K_wow

2013/8/10

#
How would one save a game locally with Greenfoot? Does the same method apply for standalone applications made with Greenfoot? Does the same method apply for web applications and scenarios on the Greenfoot website? Also, how would one save a game to a specific user on the Greenfoot website?
danpost danpost

2013/8/11

#
You can use file saving and reading for standalone and locally with Greenfoot; however, not for any web applications on the Greenfoot site. The UserInfo class can be used to save a limited amount of data per user locally with Greenfoot and on the Greenfoot site (I am not sure if it works with standalone; but I do not see why it would not). Being the amount of storage is restricted per user (11 int fields and 5 String fields of 50 characters or less), saving games may require some data encoding or even may not be possible at all (if you need to store more than what you can even condense the data size to).
A good way you could use this for saving levels, is by using one of the strings allowed, and then having a code like this "xxoxooxoox" where o's are unlocked (or "o"pen) levels and x's are locked (cannot "cross") levels. Then every time a level is unlocked, you go through the String, and change the according level slot. For other stats, you could use the 11 int fields, either straight forward like gold or health, or you could use them more differently (specific numbers mean specific things that have happened, like in an RPG, story-changing sort of game). However, the more complex your game gets, the more creative you have to get. A good example is bourne's Workshop: http://www.greenfoot.org/scenarios/8180 Really, It's all up to you, and whether you are doing it locally or online. And to verify: yes, you can use UserInfo offline (All the data is stored on a file called storage.csv) but you still have the same limits. If you want to make a complex game, it is better to write your own files locallly, and have it as a standalone game, then on the greenfoot site. But if it's something as simple as levels, or simple stats and scores, it would work with UserInfo.
K_wow K_wow

2013/8/11

#
Thanks for your help on the UserInfo class, danpost and FlyingRabidUnicornPig! Those tips are really great, but is there any way to read and write specific lines to files, and have each line being for a different variable in the game code? For instance, to write the x coordinates of a player on one line of a file, and the y coordinates on a different line? I believe the java.io.File class is used in reading and writing files, but I just don't know how to use it. Also, can you define a specific file extension when you create a file?
Gevater_Tod4711 Gevater_Tod4711

2013/8/11

#
You could use my File Reader/Writer Demo to save and load files on your computer. When you use the methods I implemented in this demo you can give the Strings you want to save it the method as an array or as a list (java.util). Every String in the array or in the list will be a single line in the file you create. But using files only works when you execute your game offline because while playing online the security will not let you use files.
K_wow K_wow

2013/8/11

#
Gevater_Tod4711 wrote...
You could use my File Reader/Writer Demo to save and load files on your computer. When you use the methods I implemented in this demo you can give the Strings you want to save it the method as an array or as a list (java.util). Every String in the array or in the list will be a single line in the file you create. But using files only works when you execute your game offline because while playing online the security will not let you use files.
Thankyou! So it seems as though the only way I can't have saving and loading progress is with standalone web applications? Well, that's fine. And your program saves in strings only? Then, I could always just convert my integers and booleans to strings, and then back again. Thanks again for your help!
Gevater_Tod4711 Gevater_Tod4711

2013/8/11

#
You're welcome! And yes I think a standalone web application is the only case where you can't save anything. But if you use a standalone jar file it will work.
You need to login to post a reply.