Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The NodeLinkingTo property gets or sets the PositionedNode that the Link connects to. Notice that a link only has one NodeLinkingTo. This means that links are "one way". Therefore, if two PositionedNodes need to link to each other, then each will need a Link to the other. When NodeNetworks are created through the AIEditor, PositionedNodes which link to each other do so with two Links.
The Cost member represents the difficulty in traveling the node. Usually cost is influenced by or even equal to distance, but it doesn't necessarily have to be. Cost can measure a number of things including:
Difficulty in crossing due to terrain
Difficulty in crossing due to risk (enemies, thieves)
Resources required to perform a particular action (if node networks are used abstractly)
The cost property is used by the to find the most efficient path from one node to the next.
The most important aspect of a PositionedNode object, is that it contains links to other PositionedNode objects within the NodeNetwork. They can also represent literal locations within the game world.
Links can be created between any two PositionedNode objects, and can be one-way or two-way. A link has an associated value, which represents the cost of traveling across that link. Once a set of PositionedNode objects have been created, linked, and then added to a NodeNetwork object, the shortest path from one node to another can be discovered.
To create a link from one node to another, call the LinkTo method. This automatically creates a two-way link between the nodes. The LinkToOneWay method creates a one-way link from the node this method is called on, to the node that is passed on the method.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The FlatRedBall.AI.Pathfinding namespace provides implementation of pathfinding. Pathfinding is used in determining the best path from one location to another.
The NodeNetwork class provides the facility to group together PositionedNode items, and discover the best paths between them. Currently, only the A* pathfinding algorithm is implemented.
PositionedNode represents a location within the NodeNetwork.
PositionedNodes are linked to each other by Link objects. Link objects represent the cost from one PositionedNode to another.
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:
Add the following at class scope:
Add the following in Initialize after initializing FlatRedBall:
Add the following in Update:
NodeNetworks are a collection of which are linked to eachother using . NodeNetworks are used for pathfinding.
Files Used: NodeNetworks can be loaded from .nntx files. Download the file and:
For more information on file loading in FlatRedBall, see the wiki entry.
The following code creates a simple NodeNetwork and a . Pressing the 1, 2, 3, or 4 causes the 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:
Links represent a one-way path to a PositionedNode. PositionedNodes store a list of links to other PositionedNodes internally. These are used by NodeNetworks to find the shortest path between two nodes.
Links are one-way to support wider functionality with pathfinding. In other words, if Node A and B link to each other, then A stores a link to B and B stores a link to A. The reason this is important is because the cost to travel from A to B may not necessarily be the same as the cost to travel from B to A. If A is on higher ground, then traveling to B may be considered easier because the trip is downhill.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
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 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.
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 TileNodeNetwork object is a type of NodeNetwork which is designed specifically for tile-based games. Since the TileNodeNetwork object inherits from NodeNetwork, all available methods in NodeNetwork are available in TileNodeNetwork. Therefore, you should check out the NodeNetwork page for additional information and features on the TileNodeNetwork object. Normally a TileNodeNetwork is used when creating a game which uses tile maps. As shown below, TileNodeNetworks can be filled from tiles in a tile map with no code, but they can also be edited and accessed in code for additional game logic.
The TileNodeNetwork offers the following benefits:
Simple addition of tiles on a grid-based system using four or eight-way movement.
Quick index based ( O(1) ) tile finding
Concept of tile occupation to prevent multiple characters moving on the same tile.
The FlatRedBall Editor supports the creation and population of TileNodeNetworks. Typically TileNodeNetworks are filled using tile maps. This section covers common ways to fill a TileNodeNetwork. We recommend using the GameScreen and Level approach, as this makes it easier to maintain games with multiple levels. Using this pattern, a TileNodeNetwork would be defined in the GameScreen:
Select the GameScreen
Click Add Object to GameScreen in the Quick Actions tab
Select TileNodeNetwork as the type
Enter a name for your TileNodeNetwork such as WalkingNodeNetwork
Click OK
Be sure to create the TileNodeNetwork in the GameScreen so it is included in all levels. Once you have created a TileNodeNetwork, it can be filled from certain tiles. It can also be convenient to fill a TileNodeNetwork from the absence of tiles if the tiles represent solid areas such as walls. To create nodes where there are no tiles:
Select the new TileNodeNetwork
Click on the TileNodeNetwork Properties tab
Click the From Layer option under the Creation Options category
Select your map and the layer. Typically this will be Map and GameplayPlay layer.
Verify the All Empty option is selected
Optionally you may want to make the TileNodeNetwork visible so you can verify that it has been filled in:
Select the TileNodeNetwork
Click the Variables tab
Check the Visible checkbox
The game should display the node network wherever no tiles are present.
Some games include specific tiles for pathfinding rather than all empty tiles. The first step is to determine which tile to use as walkable tiles. Whichever tile is used should have a type specified in the tileset in Tiled.
Once this tile Type is set (and the .tsx is saved), this tile can be used to mark walkable areas in the map.
To use these tiles:
Select the TileNodeNetwork
Click the TileNodeNetwork Properties tab
Check the From Type property
Select the Source TMX File (usually Map)
Select the type for the tile you just saved. It should appear in the drop-down if the .tsx file has been saved.
The TileNodeNetwork will now place a node wherever the walkable tiles are present.
The following code creates a TileNodeNetwork that is 10X10 with has a seed (bottom left tile) at 0,0. Add the following using statement:
Add the following to Initialize after initializing FlatRedBall:
The following code can be used to create a TileNodeNetwork using a loaded TMX file:
Note that the example above uses a name (PathTile) for simplicity, but you can also use properties. You simply have to convert the properties to a list of names:
Many games which have tile maps which also require pathfinding often include different terrain types. For example, a map may include regular terrain, water, and mountains. Terrain is important because certain units may be able to travel over certain terrain faster than other terrain. The SetCosts method allows for specifying the cost of travelling over certain terrain types quickly without modifying the cost of travelling across each Link in the NodeNetwork manually.
The SetCosts method requires the following steps:
The costs of each terrain type must be defined. These are defined in a float array
Each point on the tile which is not of the default type must have its terrain type set through the PositionedNode's PropertyField variable.
The TileNodeNetwork's SetCost method must be called with the float array containing the cost of each terrain as the argument.
The following pieces of code show how a TileNodeNetwork can be set up for different terrain types. First the terrain types must be defined. Since they will be reused in multiple places we'll use an enum:
The following code assumes that tileNodeNetworkInstance is a valid TileNodeNetwork instance which has already hadd its nodes added:
AddAndLinkTiledNode adds a new PositionedNode at the argument X, Y index at the argument DirectionType, or using the default DirectionType specified in the constructor if one isn't specified. This method uses indexes, where 0,0 is the bottom left of the TileNodeNetwork. Each index represents one tile, so if your TileNodeNetwork has a tile size of 16, then an X value of 1 would translate to 16 pixels further to the right than an X index of 0.
The following code adds a few nodes to a TileNodeNetwork using X,Y indexes.
The following code can be used to add nodes with the cursor when the PrimaryDown value is true (left mouse button). The following code could be added to CustomActivity of a Screen which has access to a TileNodeNetwork.
OccupyTileWorld marks the tile at the given location (in world coordinates) as occupied. Occupied tiles can have an occupier which can be checked with the GetOccupier function. Note that occupied tiles will still be considered in pathfinding.
The following code shows how to check if a tile is occupied, and if so, to move a character to the given tile. To keep the code shorter, it only considers moving in one direction.
AddAndLinkTiledNodeWorld adds a new node at the given position, and links it to any adjacent nodes following the DirectionalType specified in the argument call, or using the default DirectionalType specified in the TileNodeNetwork's constructor.
The following code can be added to a screen to allow the user to click the mouse and add new nodes. Newly-created nodes will be connected to adjacent nodes using the DirectionType specified in the constructor (four-way).