Talk:Complete Roguelike Tutorial, using python+libtcod, part 10

From RogueBasin
Jump to navigation Jump to search

Saving and loading

I didn't want to make a major change to the tutorial without checking, but the method given to deal with the duplication of 'player' seems unnecessarily complicated. pickle (which shelve uses internally) knows how to deal with duplicate object references as long as you pickle them in one go. So you can save 'objects' and 'player' correctly like this:

    file['objects_and_player'] = objects, player

then load them using:

    objects, player = file['objects_and_player]

Personally, I find it easier to put all the objects you want to save into a single dict or list, and just save the whole thing using pickle.dump. For example:

    def save_game():
        with open('savegame', 'wb') as file:
            pickle.dump([map, objects, player, inventory, game_msgs, game_state], file)

    def load_game():
        global map, objects, player, inventory, game_msgs, game_state
        with open('savegame', 'rb') as file:
            map, objects, player, inventory, game_msgs, game_state = pickle.load(file)

Though maybe there is some advantage to using shelve that I'm not aware of. James (talk) 21:09, 5 July 2014 (CEST)

Camera View

I found this in the wiki about making a moving camera that follows the player. Complete Roguelike Tutorial, using python+libtcod, extras scrolling code

It would go in after the step 10 to keep things simple but still have an impact on the possibilities of a scrolling map view.
I'm a very new programmer so I don't know that I'd be able to break it down into as simple of steps for a tutorial.