Python text game: how to make a save feature? -
i in process of making text based game python, , have general idea down. going make game in depth point where, take longer 1 sitting finish it. want able make game where, on exit, save list of variables (player health, gold, room place, etc) file. if player wants load file, go load menu, , load file.
i using version 2.7.5 of python, , on windows.
thanks in advance. :)
if understand question correctly, asking way serialize objects. easiest way use standard module pickle:
import pickle player = player(...) level_state = level(...) # saving open('savefile.dat', 'wb') f: pickle.dump([player, level_state], f, protocol=2) # loading open('savefile.dat', 'rb') f: player, level_state = pickle.load(f)
standard python objects , simple classes level of nesting can stored way. if classes have nontrivial constructors may necessary hint pickle
@ needs saving using corresponding protocol.
Comments
Post a Comment