Import objects from user input? (Python)
I am working on a game where the player can have multiple save games at once, and the player has to input the name of the savefile they want. How do I then convert their input into importing their chosen savefile?
What language is this in? (And note that files made by programs on Replit will not persist)
@MuffinsTheCat Since you're not executing any script there, why do you need to save it as a Python file? You could use a custom key/value system:
(a better version would stop keys and values from having "=" in them, and maybe use a dictionary) or you could use a built-in serialisation library like for JSON:
@MuffinsTheCat The other issue with using a Python file to do this is that the file is executed, meaning of someone tried to load a maliciously-crafted save file, unintended arbitrary code execution could take place. For example, if it was made so that "health" was "1;print('hacked')", the savefile would be this:
When imported, this save file will print "hacked".
@MuffinsTheCat If you want to preserve the classes, you may want to look into Pickle, but that has loads of other issues. To save a list, just put it into the dictionary as you would for a regular Python dictionary. You can generate a dictionary from an object with the
__dict__ property, and assign to