• Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving game  RSS feed

 
Geralt Rivea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone i have a problem.  Im creating a simple text based game but I don't know how to save the state of the game e.g. All the players choices and preferences. I have checked out serialization but I can't seem to understand it. Is there any other way i can save everything or is there a simple serialization tutorial that i can check out?
Thanks.
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few questions to help us understand a bit more:
  • Are you using any frameworks? If so which ones?
  • Will you post the code that you have tried and tell us why it is not working?
  • Can you post the code for your objects, even if you are not able to post the full code for the program?

  • The truth is there are many ways to save data to an external source ranging from simple text files to XML sources to advanced databases.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you thought of writing the details of the game into a text file?
     
    Carey Brown
    Saloon Keeper
    Posts: 3309
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suggest creating a class that encapsulates the concept of the state of your game. If your game is simple this class can hold all the attributes of your game's state. For more complicated games, this class may refer to other classes in order to get the complete picture of your game's state. Have this one class responsible for the reading and writing of the state to a file is ideal. This also allows you to change the file format (eg properties vs serialization) without affecting the main set of code for your game.

    If you decide to go the serialization route I suggest you look into Externalizable. This involves writing some lower level code to read and write but it has one big advantage: you can write out a version number to your file. This is useful because normal serialization is brittle, you change one parameter and your existing files will no longer load. By writing a version number out as the first field you can later read the field back in and decide how you want to read the rest of the serialized file based on the version you read back in.
     
    Carey Brown
    Saloon Keeper
    Posts: 3309
    46
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's an example of an Externalizable class. Note the "DumpableInterface" and dump() methods are something I created for debugging.

    To write the object out to a file:

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