NodeNetworks are a collection of PositionedNodes which are linked to eachother using Links. NodeNetworks are used for pathfinding.
Files Used: Sample.nntx NodeNetworks can be loaded from .nntx files. Download the file and:
Drag the file into your Solution. If you decide to use the Content Pipeline, remember to not include the extension when loading the file. This sample will use from-file loading. To load from-file:
Select the .nntx file once it's in the Solution Explorer.
Press F4 or right click and select Properties
Select "None" for the Build Action.
Select "Copy if newer" for the "Copy to Output Directory".
Add the following using statement:
Add the following code to Initialize after initializing FlatRedBall:
The following code creates a simple NodeNetwork and a Sprite. Pressing the 1, 2, 3, or 4 keys causes the Sprite to move toward a given node on the NodeNetwork. This example shows simple node creation, using the node for pathfinding, and how to make the node visible. Add the following using statement:
Add the following at class scope:
Add the following in Initialize after initializing FlatRedBall:
Add the following in Update:
For more information on file loading in FlatRedBall, see the FlatRedBall File Types wiki entry.
The GetPath method returns a List of PositionedNodes which can be used to get from one point to another. The GetPath method first PositionedNode will be the closest node to the startPosition argument and the last PositionedNode will be the closest node to the endPosition.
For an example on how to use GetPath, see the main NodeNetwork page.
The UpdateShapes method will refresh the visible layout of the NodeNetwork. This method is internally called when a NodeNetwork is first made visible. If you are making any changes to a NodeNetwork, such as by calling Shift, adding/removing PositionedNodes, or by changing the position of a PositionedNode, then you will need to call UpdateShapes.
The Visible property controls whether the NodeNetwork has a visible representation. The visible representation for NodeNetworks can be useful in tools and during the development of a game.
There are a few considerations when working with NodeNetworks in Glue:
NodeNetworks are not visible by default. You will need to set their Visible property to true to show them.
When a NodeNetwork is set to Visible, it creates a variety of shapes and adds them to the ShapeManager. This means that if a NodeNetwork is visible when a Screen is destroyed, you will get an error that the Screen did not clean up after itself. To fix this, set the Visible property for the NodeNetwork to false in the Screen's CustomDestroy.
The following code shows how to make a NodeNetwork visible in code, and how to properly clean up the NodeNetwork when the containing Screen is destroyed.