AI systems using Unreal Engine 4: part 4

Jun 30, 2020 at 10:08 am by nemirc


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.

Then, I create this Service for the Behavior Tree, where I call the idle function. Please note the casting nodes. First, I need to cast to the AI controller class (Stalker_AIController) and then I cast to the stalker class (StalkerBase), and finally I call the "Idle" function in StalkerBase. An alternate solution could be to just cast to Stalker_AIController and then have an "Idle" function in the AI controller, and let that function do what you need. It all depends on the setup and the needs, and you may actually need to change things (for example, deriving subclasses from the AI controller rather than having a one-size-fits-all AI controller for every stalker).
 
Lastly, I add the service to the Behavior Tree. Note my service had a "Finish Execute" node at the end, with the "Success" checkbox set to false. That makes the service to finish the service as a "failure" so the branch moves to the next node, causing the AI to "wait" for one second before executing again. If the "Finish Execute" checkboxes were set to true, the branch would just restart and the Wait service would be ignored.

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).

Next, I will explain the patrolling branches.

Get Unreal Engine: https://www.unrealengine.com/

 





Sign up for our newsletter

This website uses cookies to ensure you get the best experience possible More Info
Got it!