Jump to content

Trying to Save a Class Javascript


ItsYaBoiWesley
 Share

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?

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...