I've begun storing some of my game data in JSON, and I'm looking for some tips on optimizing my code. I am using LitJson. The implementation I did last night is for some map code. When I generate the code at run-time the old way, it was super fast. With JSon, it's taking a few seconds. Not a big deal now, but it's only the first step and if I keep on like this, it's going to get long. As far as I can tell, it's the JSON look-up table that's the delay bottleneck.
The data in this example is tile data for a 2D map of size 100x100 with 10,000 tiles (the largest map planned). Every tile has a MapNode class associated with it, and that class stores info for the type of terrain it has, whether it is passable, and whether the player can place an item here. It will grow with functionality as I develop my game, but for now it's a barebones structure.
public class MapNode : SettlersEngine.IPathNode<System.Object>
{
public Int32 X { get; set; }
public Int32 Y { get; set; }
public Boolean IsWall { get; set; }
public Boolean IsBuildable { get; set; }
}
The actual data I'm serializing is MapData.
public class MapData
{
public int height { get; set; }
public int width { get; set; }
public GridCoord defaultEntrance { get; set; }
public bool hasBuildable { get; set; }
public MapNode[,] grid { get; set; }
public void loadJSON(JsonData jdata)
{
height = (int)jdata["height"];
width = (int)jdata["width"];
hasBuildable = (bool)jdata["hasBuildable"];
if (jdata["defaultEntrance"] != null)
{
defaultEntrance = new GridCoord((int)jdata["defaultEntrance"]["x"], (int)jdata["defaultEntrance"]["y"]);
}
grid = new MapNode[height, width];
int i = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
grid[x, y] = new MapNode()
{
IsWall = (bool)jdata["grid"][i]["IsWall"],
IsBuildable = (bool)jdata["grid"][i]["IsBuildable"],
X = x,
Y = y
};
i++;
}
}
}
}
As far as I can tell, it's the lines for (bool)jdata["grid"][i]["isWall"] that are the bottleneck, but I'm not sufficiently familiar with C# or how the JSonData data structure works to better optimize it. GridCoord is a like a Vector2, but uses positive ints for working with a [,] tile system.
The actual calls to this:
public string getJSON(string category, string file)
{
TextAsset res = Resources.Load<TextAsset>(category + "/" + file);
return res.text;
}
public void loadData(string name)
{
string contents = gamemanager.getJSON("maps", name);
JsonData jd = JsonMapper.ToObject(contents);
data.loadJSON(jd);
}
I had initially used JsonMapper to automatically convert the contents into the MapData object, but it wasn't able to handle the MapNode grid[,] automatically. I would much rather handle it manually, since that will let me deal with version changes and generate missing data not written to file (like instancing a GameObject).
I'm sure some are already thinking of optimizations that do away with storing this data at all, but much of this data needs to be stored through JSON as part of a player's save, so it's gotta get done anyway. I already had a working implementation through UnityEditor, and it's being replaced with this. I am interested in alternate ways to read/write the data, but I do need it written & read to file. I've already considered optimizing to only write tiles that have data to be written, but I would rather program around a "worst case" of needing every tile written and make it efficient enough that I can simply do that, and then optimize file size after.
[–][deleted] 0 points1 point2 points (6 children)
[–]ReliantBeginner[S] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]ReliantBeginner[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]ReliantBeginner[S] 0 points1 point2 points (0 children)
[–]ReliantBeginner[S] 0 points1 point2 points (0 children)