nskins / goby

Command-line role-playing game framework
MIT License
122 stars 56 forks source link

Simple save feature #73

Closed nskins closed 7 years ago

nskins commented 7 years ago

One should be able to type 'save' and the game data will be saved to file. It should then be loaded automatically on normal startup.

Create two functions in lib/util.rb:

  1. save_game(Player): this function will generate the JSON representation of the argument and write it to a file (since it's simple for now, just name it player.txt, I guess).
  2. load_game: this function reads the file player.txt and parses the JSON into a Player object. It then returns the new player object to the function from which it was called.

JSON: http://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html Files: https://ruby-doc.org/core-2.2.0/File.html

There will be a little more work required for the automatic loading (lib/main.rb) and for the new default command 'save' (lib/world_command.rb). Also, just since I'm thinking of it, try to see if there's any way to detect faulty data that doesn't parse correctly during load_game. If this happens, catch the error, return nil, and then, in lib/main.rb, create the default player data (which is specified by the programmer).

EDIT: If you have any other ideas about the implementation, feel free to pursue them.