Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:42:54+00:00 2026-05-26T11:42:54+00:00

In my game, I save the current state by converting all the objects to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:42:54+00:00Added an answer on May 26, 2026 at 11:42 am
    var Enemy = {
      toJSON: function () {
        // pack it up
      },
      fromJSON: function (json) {
        // unpack it.
      },
      /* methods */
    };
    
    var e = Object.create(Enemy);
    var json = JSON.stringify(e);
    var same_e = Enemy.fromJSON(json);
    

    the .toJSON method is a standard interface of JSON.stringify it will look this method and call it if it exists, it will stringify the returned object.

    The .fromJSON method is just a named constructor for your Enemy object.

    Concrete example JSfiddle

    var Enemy = {
      constructor: function(name, health) {
        this.health = health || 100;
        this.name = name;
      },
      shootThing: function (thing) { },
      move: function (x,y) { },
      hideBehindCover: function () {},
      toJSON: function () { 
        return {
          name: this.name,
          health: this.health
        };
      },
      fromJSON: function (json) {
        var data = JSON.parse(json);
        var e = Object.create(Enemy);
        e.health = data.health;
        e.name = data.name;
        return e;
      }
    }
    
    var e = Object.create(Enemy);
    e.constructor("bob");
    var json = JSON.stringify(e);
    var e2 = Enemy.fromJSON(json);
    console.log(e.name === e2.name);
    

    Meta-option:

    A meta option would be to write the class name to the object

    Game.Enemy = {
      ...
      class: "Enemy"
    };
    

    Then when you load all your json data you just do

    var instance = Game[json.class].fromJSON(json);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A game that I play stores all of its data in a .DAT file.
I am working on a game that makes use of a save/load feature. I
I want to save object state and reuse it after some time. I got
I'm building an XNA game and I'm trying to save game/map etc. state completely,
I am looking for a solution to save my game data so that even
I have a game I'm writing in java, it has the functionality to save
Boy, I've got everything down-pat save the word referencing. The game crashes because of
I am porting a game, that was originally written for the Win32 API, to
I have a complex Clojure data structure that I would like to serialize -
I'm making a game and writing save files into the document directory on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.