Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:behaviors:pathfinding [2021/04/07 19:34]
entropy [Examples]
gdevelop5:behaviors:pathfinding [2021/11/22 23:06] (current)
Line 1: Line 1:
 #Pathfinding #Pathfinding
  
-**[[#Examples|I would rather see the Pathfinding behavior examples! Please take me there now.]]+**[[#Examples|See Pathfinding behavior examples]]
 ** **
  
 Pathfinding behavior allows us to move objects to a selected destination as well as to flag items as obstacles. Objects that are flagged as obstacles will be avoided by the moving objects. Pathfinding behavior allows us to move objects to a selected destination as well as to flag items as obstacles. Objects that are flagged as obstacles will be avoided by the moving objects.
  
-## Add behavior to object +## Step 1) Add "Pathfinding" behavior to objects that will travel  
-To add behavior to an object, as usual, create the object. Then open the object's properties by right-clicking on the Objects list located on the right of the Scene Interface. Select "Edit object" from the popup list. +To add the Pathfinding behavior to an object, as usual, create the object. Then open the object's properties by right-clicking on the Objects list located on the right of the Scene Interface. Select "Edit object" from the popup list. 
  
 {{:gdevelop5:editplayerobject.jpg?200|}} {{:gdevelop5:editplayerobject.jpg?200|}}
Line 17: Line 17:
 Next, we have two options to choose from: Next, we have two options to choose from:
  
-## Option 1:  Pathfinding behavior 
 {{ :gdevelop5:behaviors:pathfinding-behavior-inlist.png?nolink |}} {{ :gdevelop5:behaviors:pathfinding-behavior-inlist.png?nolink |}}
  
-"Pathfinding behavior" allows us to move the object to a destination while avoiding all objects flagged as an obstacle+"Pathfinding behavior" allows us to move the object to a destination while avoiding all objects that have the "Obstacle for Pathfinding".
  
-After we add the behaviour to the object, we can customize a number of options.+After we add the behavior to the object, we can customize a number of options.
 {{ :gdevelop5:behaviors:pathafindin-behavior-options.png?nolink |}} {{ :gdevelop5:behaviors:pathafindin-behavior-options.png?nolink |}}
  
Line 34: Line 33:
   * **virtual cell height and width:** our path is generated using a virtual grid. We can change the size of the cells here, the smaller the cell size, the smoother the movement is going to be. //A smaller cell size implies more calculations so try to still keep the size as large as possible.//   * **virtual cell height and width:** our path is generated using a virtual grid. We can change the size of the cells here, the smaller the cell size, the smoother the movement is going to be. //A smaller cell size implies more calculations so try to still keep the size as large as possible.//
  
-## Option 2: Pathfinding Obstacle behavior+## Step 2: Add "Obstacle for Pathfinding" behavior to objects that should be avoided
 {{ :gdevelop5:behaviors:pathfinding-obstacle-inlist.png?nolink |}} {{ :gdevelop5:behaviors:pathfinding-obstacle-inlist.png?nolink |}}
  
Line 43: Line 42:
   * **impassable:** if we enable this, the obstacle is going to be impassable. The moving object is going to avoid it at all cost.   * **impassable:** if we enable this, the obstacle is going to be impassable. The moving object is going to avoid it at all cost.
   * **cost:** if the obstacle is not impassable, we can set a cost for the obstacle. The moving object, when looking for the best possible path to a destination, is going to prefer to avoid the obstacle with high-cost values. For example, a river might have some cost. The moving object, if possible,  will avoid it. However, if there is no other way around the river, or if the river has a lower cost compared to another area, the moving object will go through it (instead of climbing a mountain with a higher cost for example). And if we put crocodiles into the river and increase its cost, the moving object might decide to climb the mountains instead.   * **cost:** if the obstacle is not impassable, we can set a cost for the obstacle. The moving object, when looking for the best possible path to a destination, is going to prefer to avoid the obstacle with high-cost values. For example, a river might have some cost. The moving object, if possible,  will avoid it. However, if there is no other way around the river, or if the river has a lower cost compared to another area, the moving object will go through it (instead of climbing a mountain with a higher cost for example). And if we put crocodiles into the river and increase its cost, the moving object might decide to climb the mountains instead.
 +
 +## Step 3: Create an action to move the Pathfinding object
 +
 +To initiate a pathfinding move, add the action "Move to a position" and specify the location you want the object to move to.  It will move using the characteristics defined in the behavior properties.  If you want to change how the object moves during the game, these properties can be changed using actions.
 +
 +{{ :gdevelop5:behaviors:pathfindingmoveactionwithcondition.jpg?direct |}}
 +
 +<note important>The "Move to a position" action only needs to be run once.  If you run this action with no conditions, it will try to recalculate the path every frame and your object will NOT move.  One way to make sure this action only runs once is to add a condition that makes sure the object pathfinder speed is 0. </note>
  
 ## Examples  ## Examples