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

2012/4/9

List.toString() then String.toList()

Razzo Razzo

2012/4/9

#
I want to make a level saving system that does,
List x = getWorld().getObjects(null);
String c = List.toString()
How would I turn the string back into a list? then Into objects?
Razzo Razzo

2012/4/9

#
Bump Please help me!"
tkiesel tkiesel

2012/4/9

#
You'll need to write a method to convert your level into a String, and a method that can read a string and build a level out of it. Check out Java's BufferedReader, which has nice ways to read text files. Then, BufferedWriter will let you write to a file. Once you've got code that can read from a txt file and write to it, then you can implement your methods that turn your current world into a String and vice versa. I suggest something like this: id - Object type x - x location y - y location separated by commas. Different objects separated by semicolons. So if object ids are Then this String:
2,20,40;1,45,45;3,150,20
would represent a car at (20,40), a block at (45,45) and a tree at (150,20) Writing the method to read this string off and make the objects, and the method to read out the objects into a String will be good, rewarding think work for you! :)
tkiesel tkiesel

2012/4/9

#
I should mention that while the work to write something like this can be finicky and tough, once you have it, writing new levels for your game becomes so much easier, and for really complex levels, your world file can shrink from an enormous monstrosity to something far more manageable! :) Two high school students of mine (with some help on file IO from me) used this basic system to write a Galaga-type space shooter, and their level Strings even included timing data. Cool stuff.
You need to login to post a reply.