• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving objects to files  RSS feed

Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a small game and at the moment, I have a problem concerning saving my objects.

I have a class "Task", which handles the quests for the player:

I don't want to write the tasks in the sourcecode, but to use a seperate file to store them. This should be made to add, modify and remove tasks easily.
The file in which the tasks are saved should be readable for the user and usable by the program.

I would like the file to look a bit like this:

name: task 1
type: task type A
text: dummy text 1
probability: 0.75

name: task 2
type: task type B
text: dummy text 2
probability: 1.0


I did some research and found out that xml or json might suit my needs, but I don't find a real tutorial for it which fits my use case.
I was able to create a json file with methods from org.json, but I don't understand how I could write more than one object to a file and how to read them back in. Do I have to create an own file for each instance of task? During runtime, the tasks are saved in an ArrayList<Task>. I would prefer to just save the list.

It might be important to notice that this game should be used on Android, which seems to restrict the use of some xml instructions. At least it was not possible for me to create an xml file out of my test object.

Can anybody give me a hint what to do? Is there maybe amother possibility I did not recognize yet? I am pretty lost at the moment...
Posts: 2461
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jan,

unfortunately, I know nothing about Android, and my knowledge about xml
is pretty limited, so I can't help you with these.

But an alternative is simply to write out the members as text, just like you
already mentioned. Maybe start a Task with an easy to reckognize identifier,
somthing like


and end such a class with <EndTask>.

Reading of these classes should be straightforward.

It has also a bonus of easy to create or edit by hand, using any
text processor.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!