In my game, I save the current state by converting all the objects to JSON and then saving that to a file. Some objects, like enemies, have functions on them, but JSON can’t save functions! Is there an alternative or a solution?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
the
.toJSONmethod is a standard interface ofJSON.stringifyit will look this method and call it if it exists, it will stringify the returned object.The
.fromJSONmethod is just a named constructor for your Enemy object.Concrete example JSfiddle
Meta-option:
A meta option would be to write the class name to the object
Then when you load all your json data you just do
var instance = Game[json.class].fromJSON(json);