Behavior Trees - Godot Game Dev (BETTER AUDIO)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome to another video today i'm going to talk about behavior trees [Music] behavior trees are used to create complex ai by using a small independent tasks every behavior tree has a root node that's the entry point of your tree there are leaf nodes that's where our custom logic leaves here you implement your actions and conditions the composites are the brains of your tree composite nodes are also known as contour flow nodes because this is what they do the two most common implementations are sequences and selectors a sequence expects all its children succeed so it executes them in order and in case one fails it stops the selector node does the opposite it requires only one child to succeed it also executes its children in order and stops assume one succeeds the last type of note to talk about are decorators they are not part of the mathematical definition but they are a common pattern in software development it helps to make the tree less complex and the nodes more reusable here are some examples of decorators the inverter also known as not inverts the output of its child the succeeder are always succeed returns success regardless of the child output and the limiter works slightly different from the other ones instead of changing the output it actually controls how many times this child can be called now that you have a high level idea let me show you a practical example i created this project to show a behavior tree in action one thing that i didn't mention is that every node in a behavior tree is going to return one of those states success failure or running the name may change slightly depending on the implementation but the meaning is all the same nodes also have a comma method here it's called tick but you can find it as update or process in other implementations this is the method called by the parent node it all receives an actor that's the element where this behavior is applied to and a blackboard that's where you restore and share data this is basically the interface of every node in a behavior tree so in my example i have two nodes that i call home they are part of a group called home what i want in this example is my npc to follow my mouse currently it's not doing anything i want my character to follow my mouse and if the cursor is too far it should go for one of the homes the closest one so i'm gonna show you how a behavior tree for this npc would look like so let's start creating the behavior tree i'll first add the root node and a selector as the first node now i'm gonna add a sequencer for the first branch i'll create an action node that moves the player to the mouse position in this example i'll create simple implementations and then as new scenarios come i will refactor them to become more reusable after creating my node i need to come to the inspector here to the script field and choose the option to extend script this will create a new script extending my action node now i need to implement the tick function here i get the mouse position and i make the actor move in its direction [Music] if i run the example you can see that my card is already following the mouse one thing i need to change here is that when you have an action that takes more than one tick to complete you need to return running as a state i will do that by checking if the final distance is less than one pixel to the target if it is i consider the task as completed otherwise it's too runny now that my actor is following the mouse i want to implement the condition to stop if the mouse is too far for this i'll create a condition node it's virtually the same as an action node but the icon is different in my tree my condition will check the actual position is less than let's say 300 pixels from my mouse position when i run the scene you can see now that my npc only follows mouse when it's close enough for the sake of reusability you shouldn't have values hard coded in your node so i'll create a parameter called detection radius so when reusing this script i can't find a different value for that goto will expose this parameter as a field in the editor what's quite handy let me set 600 pixels here now you can see that my npc is following the mouse from farther so let me now implement a second branch which goes to the closest home for that i'll add another sequence again i will start for simpler implementation and then refactor it i'll create a new action that would go to home position first i get all the nodes in the group home and then i select the closest one if you can't find any home it will fail otherwise it will move the actor in that direction [Music] let's see it working as you can see it tries to follow the mouse as before but when it's too far it goes straight to the closest box so there are a few things to improve in this logic first of all this node is doing too much so we are going to break it in smaller tasks i'll move the find closes home to another action and save it to the blackboard [Music] the go to home position now just picks the position from the blackboard and moves the actor in it direction you can see it still works now you may have noticed that go to home position and go to mouse position are quite similar so let's make them more generic so we can reuse the same script in both places one good practice is to never hardcode blackboard keys inside your tasks this makes them less reusable so let's change it here [Music] so i'll move the key name to the field in the aerator and i'll also rename some variables here now that the script's generic enough i'm going to rename it from go to home position to go to target position now i'll also move the blackboard key from the find called home and another improvement would be exposing the group name as a parameter so now this action is not about finding the closest home anymore but finding the closest node in a group one thing you may have noticed is that you went with the name of the nodes and the name of the scripts being different and that's totally fine because i want my scripts to be as generic as possible while my nodes should be descriptive enough so i know what my tree is doing one thing i need to change to be able to use my new go to target position script in my go to mouse position node is to move the logic that finds the mouse position to another action this logic is actually used in two different places so i went killing two boards with one stone removing some duplication here it will get the mouse position and save to the blackboard now in the script that checks the mouse positions is nearby i can get it from the blackboard and as this script is generic enough i'm going to rename it to is target nearby now my go to mouse position script can be replaced by the generic one i just need to define what's the blackboard key for my position [Music] as you can see it still works [Music] now let me give you an example of the power of reusability in behavior trees let me add another action to my behavior tree let's say if the npc reaches home it changes its color this will be a third branch in my main selector however the way the tree is working right now this node will never be called the reason for that is that the branch that moves to home position never fails so it will always stop in the second branch even if the item already reached home to fix that i need to add another condition to the second range that checks if the items are at home luckily i can renew the script as target nearby i just need to set the detection radius to 1 which means both items are technically in the same position however this script succeeds when the item is nearby and what i want here is the opposite of that i want this branch to fail if the item is nearby for that i can use inverter decorator [Music] now my npc is following the mouse and it tries to go home with the mouse too far and when it reaches home it changes its color to red and that's it besides implementing the new action to change the color i didn't have to implement a single line of code extra to make this validation work and that's a cool thing about behavior trees once you have the basic nodes implemented you can reuse them as you wish here's how our behavior tree looks like in the first branch there is a sequence that first loads the mouse position checks if the mouse is nearby and then go to home position in case this one fails the selector tries the second branch which find the closest home and in case the item is not home already it goes to the home position if this branch also fails then it tries the third range that's just an action that changed color we could reorganize this tree in a different way the first branch would remain the same the second branch would then check the items at home and change its color if it's not and in case this one fails then it goes to the third branch which find a closed home and go to home position either way both trees would do the same thing this is what i have for this video i hope you enjoyed see you next time [Music] you
Info
Channel: This Is Vini
Views: 3,588
Rating: undefined out of 5
Keywords:
Id: YHUQY2Kea9U
Channel Id: undefined
Length: 10min 25sec (625 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.