Controllers

In this example, we will give PlayerController the ability to take and apply damage. This is done once the AI is finished, and you'll be able to fight the AI. Also, as we will use AI Perception in this course, we should register the Stimuli source for our controller. Next, we will set up the AIController. We will give it the ability to sense stimuli. Then, we will create an Actor component, which will contain the function needed to update Behavior Tree.

Let's move on and begin to create these base components, as follows:

  1. In the AI folder, right-click and select Blueprint. Then, from the options, select Player Controller. Then, we want to name this OutController.
  2. Open Event Graph and find Event BeginPlay.
  3. Right-click nearby and search for Get Controlled Pawn. Then, pull from Return Value and search for Assign OnTakeAnyDamage.
  4. This will create an OnTakeAnyDamage event and output Damage.
  5. We need to create a variable to hold our Health variable. Click on Add variable and make a Float variable named Health. Set the default to 5.
  6. Looking at OnTakeAnyDamage, pull out Damage and subtract it from Health. Then, set the results in our Health variable. Next, we will check whether Health is less than or equal to 0.0.
  7. Create a Branch node from the results. Pull from True and right-click to search for Print String. Populate this with Player Died, right-click, and then search for DestroyActor.
  8. Right-click and search for Get Controlled Pawn; then, plug this into the Target pin on DestroyActor from the previous step. Now, we can take damage from our AI counterpart:
    Controllers
  9. Right-click and search for the E Input event. Pull Pressed and release to search for Sequence. Pull from Then 0 and release to search for DoOnce. Pull from Then 1, then release to search for Delay. Now, set Duration to 0.75 and plug Completed into the Reset pin in DoOnce.
  10. Looking at DoOnce, pull from Completed, and release to search for LaserfromController. Then, pull from Hit Actor and release to search for Apply Damage. Set Duration to 1.0 and Damage Causer to Self.
  11. Now, we want to add this controller as StimuliSource. In the Components section, click on Add Component and find AIPerceptionStimuliSource.
  12. Click on Register as Source for Senses and then click on +. Let's select AISense_Sight from the available options.

Now, compile this. We are done with our PlayerController setup. Now we need to create an AIController setup so that our AI can fire back at us:

Controllers

Let's set up our AIController as follows:

  1. Right-click and click on Blueprint. In all classes, search for AI Controller. Select it and then click on Select to create it. Name it EnemyController and open it in EventGraph.
  2. Let's create a new Float variable named Health. Then, set the default value to 5.0.
  3. The beginning of this is the same on both characters except for one minor change. So, we can copy the code from OurController or start from Step 2 in the previous section. We will copy the section shown in the following screenshot into EnemyController from OurController:
    Controllers
  4. At the end of the Bind Event to OnTakeAnyDamage event, pull to release and search for Run Behavior Tree. Set the BTAsset value to EnemyAI, which is the asset we created earlier in the chapter. Now, the code in the tree will run on this AIController.
  5. Now that the AI can receive damage, we need it to sense the player. We will do this with the AI Perception component.
  6. On the Components section, click on Add Component and search for AIPerception. After adding this, under AI Perception on the right-hand side, select Senses Config from the drop-down menu and hit +. Then, set it to AI Sight config and select Sense from the drop-down menu.
  7. Drop down Detection by Affiliation and check for Detect Neutrals.
  8. Go under Events and click on OnPerceptionUpdated to get the currently sensed actors.
  9. From OnPerceptionUpdated (AIPerception), pull and release to search for Sequence. We want then_0 to be drawn and then released to search for DoOnce. Then, we want then_1 to be set to the Duration pin of the Delay node. Set Duration 1. Now, connect Completed to Reset.
  10. Focus on DoOnce. We will pull from Completed and then release to search for Stop Movement. Right-click and search for Blackboard. Pull from Blackboard and release to search for Set Value as Object. Right-click, search for Make Literal Name, and plug this into Key Name. Get the first element from UpdatedActors and plug this into Object Value:
    Controllers

Now, the AI will immediately report the actors that get sensed to Behavior Tree, which then can be used immediately in our AI's next decision.

..................Content has been hidden....................

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