I was wondering how to properly structurize a set of nodes. In my game, sectors are loaded in the background while the players moves around. From what I know, it's best to keep the chunks as large as possible. While the HD is reading, the other threads can continue with the game. If that is true, it would be best to allocate the whole thing in one step, then read the data into the record-array.
This time I need to find a way to store a large set of (pathfinding) nodes. Something like
<Delphi code>
TRelation = record
targetNode : pointer; // pointer to another TNode
data : integer; // Some additional data stuff
...
end;
TNode = record
relations : array of TRelation; // dynamic array
relationCount : integer; // count of related nodes
end;
nodes : array of TNode; // The array I'd like to write/read from a file
So, there is an array of nodes with a known side. But the relation count could differ for each node. Also loading and storing pointers doesn't work I guess, so I'll have to replace the "targetNode : pointer" with "targetNodeIndex : integer", which is an index in the "nodes" array.
I could read node by node, and allocate the "relations" array for each node
stream.read( nodeCount, 4 ); // read the count of nodes
setLength( nodes, nodeCount ); // Allocate node array
// Now start reading node by node
for i:=0 to nodeCount-1 do begin
stream.Read( nodes.relationCount, 4 ); // Read relation count into node
setLength( nodes.relations, nodes.relationCount );
// Read relations
stream.read( nodes.relations, sizeOf(TRelation) * nodes.relationCount );
end;
Works, but something tells me it can be done better. And when continously loading/unloading sectors, won't this fragmentate the memory?
Greetings,
Rick