Save scene #633
Unanswered
SinicinDenis
asked this question in
Q&A
Save scene
#633
Replies: 1 comment 1 reply
-
I dont think there is a way to do it in Ursina I have searched the whole documentation couldn't find an easy way other than using basic I/O to a JSON file save the player info i.e. state, position etc. Otherwise, my approach would be through class Player:
def __init__():
# init player info
self.player_pos = Vect3(0,0,0)
self.score = 0
def to_dict(self):
# return a dict of all player state
return {'player_pos': self.player_pos, 'score': self.score}
def save(self, file_path):
with open(file_path, 'w+') as file:
json.dump(self.to_dict(), file)
@classmethod
def load(cls, file_path):
with open(file_path, 'r') as file:
data = json.load(file)
return cls(**data) # Reconstruct the class instance using the loaded data to Load loaded_class = Player.load('savegame.json') Using this approach you can save as many classes as you want to a file |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, how do I save a scene for later loading?
Beta Was this translation helpful? Give feedback.
All reactions