Sign in to follow this  
ItsYaBoiWesley

Trying to Save a Class Javascript

Recommended Posts

Hi!

I'm trying to save game data in JavaScript, using "localStorage".

I have a class called "Entity" with its own variables and functions. I have an array called "loadedEntities" which contains instances of the Entity class. For example

var zombie = new Entity(args)
var spider = new Entity(args)

loadedEntities = [zombie, spider]

I want to save the entirety of "loadedEntities." To do this, I use JSON.stringify(loadedEntities). (I save the entire array in one big gulp) Then, when I want to load the data back into the game, I use JSON.parse(). (My load function sets loadedEntities equal to whatever JSON.parse() returns.)

Now, whenever I try to run a function on an entity, (The entity class has its own functions. So, loadedEntities[0].move() or loadedEntities[1].attack()) I get the following error:

loadedEntities[i].attack is not a function

//i is a good and proper variable. Everything works fine when a "New Game" is selected. The issue definitly has to do with saving

My theory is that when I save instances of the Entity class, the data might get saved but the functions, like "attack()" don't get saved or something. I don't know, what do you guys think?

Share this post


Link to post
Share on other sites

You can't serialize an objects methods, only the primitives.

For example:

var obj = {
  foo: 'bar',
  get: function () {
    console.log('hello', this.foo)
  }
}

JSON.stringify(obj)
// "{foo: 'bar'}"

Parsing that object back won't return the methods (functions).

You'd need to write your own thing so that when you get the saved data back (which is just strings) you instantiate a new instance of the object based on those properties that are serializable. As methods are generally static this isn't too much of a problem. Gets a little more complicated when iterating over an array, but only slightly.

This only works at all because JSON.stringify does some work, you can write your own serializer (stringify) if you really need to, but danger lies that way.

Also, I really wouldn't be tempted to `toString` on functions, you'd end up having to eval on the other end when you want to use it. Again, lots of danger lies this way.

Share this post


Link to post
Share on other sites
On 12/7/2018 at 6:44 AM, mattstyles said:

You can't serialize an objects methods, only the primitives.

For example:


var obj = {
  foo: 'bar',
  get: function () {
    console.log('hello', this.foo)
  }
}

JSON.stringify(obj)
// "{foo: 'bar'}"

Parsing that object back won't return the methods (functions).

You'd need to write your own thing so that when you get the saved data back (which is just strings) you instantiate a new instance of the object based on those properties that are serializable. As methods are generally static this isn't too much of a problem. Gets a little more complicated when iterating over an array, but only slightly.

This only works at all because JSON.stringify does some work, you can write your own serializer (stringify) if you really need to, but danger lies that way.

Also, I really wouldn't be tempted to `toString` on functions, you'd end up having to eval on the other end when you want to use it. Again, lots of danger lies this way.

Thanks! I got it now.

I added an exportString and importString methods to my classes. They simply take all relevant data about an entity, stick it in an array, and stringify it. Then this data is easy to transport.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.