A* Pathfinding Project

Issues deserialising bespoke graph type


#1

I have written my own graph generator based on the navmesh generator. This is to handle pre and post bake functionality on pretty complex maps.
It works great, but I cannot work out the save/load mechanism.
In an attempt to understand this, I have implemented the polar graph generator as outlined in your tutorials and get the same issue.
Tracing through the code it appears to generate and save the correct number of nodes, but on loading I get an error in the deserialisation.

Caught exception while deserializing data.
System.Exception: 22 nodes were serialized, but only data for 1 nodes was found. The data looks corrupt.

Any advice?

Thanks in advance

Dave


#2

Hi

Could you post the code that you are using currently? (or at least the save/load code)


#3

In the polar test I have literally just added your tutorial polar graph generator and editor scripts to the project.
I do a scan then save the graph with node data included.
I then reload the test scene to clear out the graphs and load the graph data.
I get this error at that point.
Not sure if this is the exact same issue as with my custom graph, but if I can understand the issue with the polar graph then I think that will point me in the right direction.


#4

Hi

The tutorial does not cover serializing graphs. However I think you should be able to use the same code that the PointGraph uses.

protected override void SerializeExtraInfo (GraphSerializationContext ctx) {
	// Serialize node data

	if (nodes == null) ctx.writer.Write(-1);

	// Length prefixed array of nodes
	ctx.writer.Write(nodes.Length);
	for (int i = 0; i < nodes.Length; i++) {
		// -1 indicates a null field
		if (nodes[i] == null) ctx.writer.Write(-1);
		else {
			ctx.writer.Write(0);
			nodes[i].SerializeNode(ctx);
		}
	}
}

protected override void DeserializeExtraInfo (GraphSerializationContext ctx) {
	int count = ctx.reader.ReadInt32();

	if (count == -1) {
		nodes = null;
		return;
	}

	nodes = new PointNode[count];

	for (int i = 0; i < nodes.Length; i++) {
		if (ctx.reader.ReadInt32() == -1) continue;
		nodes[i] = new PointNode(active);
		nodes[i].DeserializeNode(ctx);
	}
}

#5

That’s great thanks Aron. I will give that a go and see how I get on.
Thanks for the VERY prompt responses!


#6

For anyone who should end up here…The serialize/deserialize code for the polar graph demo is as follows…

protected override void SerializeExtraInfo (GraphSerializationContext ctx) {
    // Serialize node data

    if (nodes == null) ctx.writer.Write(-1);

    // Length prefixed array of nodes
    ctx.writer.Write(nodes.Length);
    for (int i = 0; i < nodes.Length; i++) {
        // -1 indicates a null field
        if (nodes[i] == null) ctx.writer.Write(-1);
        else 
        {
            nodes[i].SerializeNode(ctx);
        }
    }
}

protected override void DeserializeExtraInfo(GraphSerializationContext ctx)
{
    int count = ctx.reader.ReadInt32();

    if (count == -1)
    {
        nodes = null;
        return;
    }
    
    nodes = new PointNode[count];

    for (int i = 0; i < nodes.Length; i++) {
        nodes[i] = new PointNode(active);
        nodes[i].DeserializeNode(ctx);
    }
}