State machines

Traditionally, we would default to the AI Behavior Tree, which is available to anyone using UE4. However, in our scenario, we will break the job of Behavior Tree into components directly written in blueprint. So, the next thing we need to create is a way to maintain a state. Then, we can essentially create a state machine by updating the variable, allowing our AI to transition into different states by controlling the execution flow.

We will establish this using an event that will update our state when the conditions we specify are met. To start this off, let's continue to the next step!

  1. First, let's create a new Int variable in our AIController called State. This will maintain our current state within our state machine.
  2. We now need a new event, and we will call it NextRoute. So, upon right-clicking on the event graph and going under Add Event, we will notice Add New Event. Let's name this new event NextRoute.
  3. Now, after our assigned event ReceiveMoveCompleted, we should call NextRoute to initiate or enter the state machine:
    State machines
  4. Next, focusing on ReceiveMoveCompleted, let's switch on Result. From the switch node, let's pull Success. This means that only when the move is completed by our AI will we continue on to the next step in the execution flow.
  5. From Success, let's create a new Branch node and set up a new condition. This condition will check whether our State Int variable currently equals 0. The value 0 represents our default state.
  6. If our State variable's value is 0, let's create a Retriggable Delay node and set it at 0.2 seconds. Next, we want to check whether our Current Point variable is valid; if so, we want to grab the variable we created earlier to define the next route in the linked list.
  7. From there, we want to set this new variable into our Current Point variable. This will allow us to indefinitely navigate between waypoints.
  8. Next, we want to create two new variables that will be responsible for delaying the amount of time till we continue to the next route.
  9. So, let's create a new Float variable called RoutePauseDelay, which will define how long we will wait. Next, we want to create deviations so that the wait time isn't always the same. So, now create a new Float variable called RoutePauseDevia.
  10. Let's pump RoutePauseDeviation into Random Float in Range and add this to RoutePauseDelay. Then, this will be pumped into a Delay node.
  11. From the Delay node, we want to call the NextRoute event, which we created earlier.
  12. Focusing on the newly created NextRoute event, let's check whether our State variable is equal to 0 and create a Branch node to check the results when executed:
    State machines
  13. Next, we should get our AIController from our Actor, and check whether our Current Point variable is valid.
  14. Next, if our Current Point variable is valid, then we should pull from Return Value of the getAIController and Move to Actor nodes.
  15. From there, pull the Current Point variable we checked and put this into the goal of the node. Now, when the AIController takes possession of the actor it belongs to, it will call this event. Once the move is completed, it will update the route and continue to the next route.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset