Quantcast
Channel: 2D Latest Topics
Viewing all articles
Browse latest Browse all 623

Trying to Save a Class Javascript

$
0
0

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?


Viewing all articles
Browse latest Browse all 623

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>