Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1055616 Posts in 42866 Topics- by 34799 Members - Latest Member: Idea Wing Artworx

October 21, 2014, 04:02:52 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Javascript data structures and Serialization
Pages: [1]
Print
Author Topic: Javascript data structures and Serialization  (Read 751 times)
Zarkonnen
Level 0
***


Games! Kittens! Wine!


View Profile WWW Email
« on: April 17, 2013, 09:18:27 AM »

So I'm getting into Javascript-based game development, but one thing I'm really not sure about is how to do the data structures and serialization/savegames.

By default, I'd just use Javascript objects to store game data and serialize them as JSON - but this has one big limitation: all the game data has to be a tree, with no cross-linking or cycles allowed. For example, in a game where there are planets and stars, I can't have a backlink from a planet to its star.

One way to fix this is to introduce IDs on objects and look them up by those IDs. So in our example, the star contains the planet but the planet knows the its star's ID. But that's cumbersome and slow. It's also irritating, because in some places an object will be really there, and in others only its ID will.

And if I go to the hassle of adding IDs to objects, it almost feels like using something like a SQL database would be a better bet: Just have two tables of planets and stars with a foreign key from planet to star.

Finally, I could of course find/write a serialization library that properly deals with reference cycles.

What are your thoughts? How have you solved this in your JS code?
Logged
Chromanoid
Level 10
*****



View Profile
« Reply #1 on: April 17, 2013, 01:17:44 PM »

How would you solve references without some kind of id? With global ids you can at least easily introduce lazy loading, not that bad imho...
Logged
zovirl
Level 1
*


Mark Ivey


View Profile WWW Email
« Reply #2 on: April 20, 2013, 02:36:17 PM »

Finally, I could of course find/write a serialization library that properly deals with reference cycles.

The stars/planets example is pretty simple because you can always regenerate one side from the other. Serialize as a strict tree, only storing the star-to-planet links. Recreate the planet-to-star backlinks when you deserialize.

I suspect you won't want a 1:1 copy of your game objects in the save file anyway. My game objects often have de-normalzed copies of the data for efficiency reasons. For example, I'm saving collision data and sprite data together because my editor is simpler when these two are attached. The game separates them when loading, though, because the collision & rendering are separate.
Logged

Alaskan Runway, a side-scrolling flight sim I'm working on.
Forest, a walking meditation game. (behind the scenes)
www.zovirl.com
Sanojian
Level 1
*



View Profile
« Reply #3 on: April 20, 2013, 10:38:42 PM »

One way to fix this is to introduce IDs on objects and look them up by those IDs. So in our example, the star contains the planet but the planet knows the its star's ID. But that's cumbersome and slow. It's also irritating, because in some places an object will be really there, and in others only its ID will.

Maybe give every object an ID and store all relationships by ID rather than by object.  If it makes it easier group these objects by object type (Stars[starId], Planets[planetId], ...)

Code:
// initialize global object stores
var Stars = {};
var Planets = {};

// create our star
Stars['star1'] = new Star('star1');

// create planets, passing a reference to its star
Planets['planet1'] = new Planet('planet1', 'star1');
Planets['planet2'] = new Planet('planet2', 'star1');

// tell star who its planets are (planets is Array)
Stars['star1'].planets.push('planet1', 'planet2');

// get a planet1's star
Stars[Planets['planet1'].star];

// get all star1's planets
for (var i=0;i<Stars['star1'].planets.length;i++) {
    var planet = Planets[Stars['star1'].planets[i]];
    // do whatever...
}

// get all the stars
for (var key in Stars) {
    var star = Stars[key];
    // do whatever...
}

This should serialize just fine and I don't think it is slow.
Logged
Zarkonnen
Level 0
***


Games! Kittens! Wine!


View Profile WWW Email
« Reply #4 on: April 21, 2013, 12:15:30 AM »

How would you solve references without some kind of id?

The IDs would be generated when writing and discarded again when loading. I'm currently considering cryo or cycle.js.
Logged
Chromanoid
Level 10
*****



View Profile
« Reply #5 on: April 21, 2013, 04:13:01 AM »

So you just didnt like global ids.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic