-
Notifications
You must be signed in to change notification settings - Fork 0
Combining the two
We will now try and make an entity walk along an actual path.
Remember how a findPath() returns an ArrayList of WalkableBlocks or turning points from the previous page? Thus, if we keep moving from one block to the next continuously in the ArrayList until the last block, we would've traversed along the path. The move_along_path
node lets you achieve exactly that. It is a special decorator node that iteratively changes the target of its child node to the next one in the path.
Replace the move_to child we had from the previous example with this
{
move_along_path: {
child: {
move_to: {}
}
}
}
We have now successfully set up a moving along mechanism once we have computed the path. However, your cube will not start moving, because it doesn't know what the path is yet. Since we must store states on the entities and not on the behavior tree itself, move_along_path
reads from the path property of the MinionMoveComponent of the Actor. So we need to set this value before calling it.
Remember how we did a pathfinder.findPath() on the previous page. The find_path
behavior node does exactly that. It computes the path from the Actor's current position to the target location stored in the MinionMoveComponent. So add this node before the move_along_path
node.
You should now be able to successfully move along a path.
{
loop: {
child: {
sequence: [
find_path,
{
move_along_path: {
child: {
move_to: {}
}
}
}
]
}
}
}
This should be wrapped inside a sequence control flow node and you should be good to go. Place the Spawner block, and the target block. Construct a wall in between. When you activate Item 1, you should be able to see the entity move around the wall to reach the target block.
Instead of setting the moveComponent's target manually, like we did in the previous page, it is always better to set it as a part of a behavior. This ensures that the target is set before any of the related nodes use it. set_target_to_followed_entity
sets the target position to the location of 'entityToFollow' property in the FollowComponent. This is particularly useful, as NPCs generally move to some entity for various reasons. We will see some more examples later.