• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Saving objects to files

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
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...
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

<Task>

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.

Greetz,
Piet
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic