I am trying to make a save feature in a game so that a player could save, then quit the game, then open the window again to select proflie and play from when they left off. Thanks!!
Jacklittle01 -3 Light Poster
Recommended Answers
Jump to PostHere is an example. The program creates a dictionary containing the current state of the game, using simple data types, then saves this snapshot on disk in a json file:
import json if __name__ == "__main__": state = { "player_name" : "bob", "level" : 3, "elapsed_time" : …
Jump to PostSuppose you have a global variable NSCREENS with integer value, you can write
state['NSCREENS'] = NSCREENS
before you save the state on disk. When you load a previously saved state, you can restore the variable with
globals()['NSCREENS'] = state['NSCREENS']
Jump to Posthow would I load the state?
Read the code previously posted.
Jump to PostSuppose you have 3 global variables named foo, bar, baz,
you can writestate['globals'] = {} for varname in [ 'foo', 'bar', 'baz', ]: state['globals'][varname] = globals()[varname]
The global variables can then be restored with
for varname, value in state['globals'].items(): globals()[varname] = value
All 13 Replies
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Jacklittle01 -3 Light Poster
Gribouillis 1,391 Programming Explorer Team Colleague
james.lu.75491856 0 Junior Poster
Jacklittle01 -3 Light Poster
TrustyTony 888 ex-Moderator Team Colleague Featured Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Jacklittle01 -3 Light Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Jacklittle01 -3 Light Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Jacklittle01 -3 Light Poster
james.lu.75491856 0 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.