Welcome back to this series about artificial intelligence. This time I will show you the idle/patrol branch of my behavior tree, which I will call "default" branch from now on.
For the default branch, I wanted my AI to do one thing or the other depending on the status of the AI. First, I want a branch where the AI is just "minding its own business" before any stimulus happens. Second, I want a branch where the AI is "patrolling" after a stimulus happens. You can also opt for the AI to go back to the "minding its own business" branch after patrolling for a while.
The default branch's root decorator and its 3 branches look like this. The first branch is the "minding its own business" branch, and the other two branches are "patrolling" branches depending on the stimulus (more about that later).
For the first branch, I decided the best thing is to leave the function "open" so I can create custom functions depending on the stalker. For example, maybe I want the idle state for stalker A to be "stalker just stands there doing nothing" but then I want stalker B's idle function to be "stalker is sitting on a couch, stands up to look for a book, picks the book and then goes back to the couch" and stalker C's idle function is "stalker is in front of the stove, goes to the table to pick ingredients then goes back to the stove, and so on." The good thing about Unreal Engine 4's class-subclass system is that I can derive subclasses from my stalker base class and then just work on the changes as I see fit.
To do what I said, I just did this. First, I created an "Idle" custom event in my main stalker class (not the AI controller, but the actual class). To check the event was being called, I attached a Print node.
Alternatively, if the branch was a "Sequence" rather than a "Selector" the checkbox should be set to true, since Sequence nodes execute the following node after the previous one has succeeded.
Running the game, you can see the branch is being called (since there's no registered stimulus) and the function is being called (as shown by the on-screen message in the game window).