Overview
Smart Objects are objects placed in a Level that AI Agents and players can interact with. These objects contain all the information needed for those interactions.
At a high level, Smart Objects represent a set of activities in the Level that can be used through a reservation system. This system keeps track of all the Smart Objects in the Level and allows an AI Agent to "reserve" a Smart Object so no other Agent can use it until the Smart Object becomes available again.
Goals
In this Quick Start guide, you will learn how to create and use Smart Objects with an AI Agent.
Objectives
Create the AI Gameplay Behavior and Behavior Definition that the AI Agent can use when it reaches a Smart Object.
Create a Smart Object that holds the Behavior Definition to play an animation montage.
Create simple AI behavior with a Behavior Tree and Behavior Tree Tasks.
Create the AI Agent that can search for and use Smart Objects in the level.
1 - Required Setup
Create a new Blueprint project based on the Third Person template.
From the main menu, go to Settings > Plugins to open the Plugins window.
Go to the Gameplay section and enable the Smart Objects, AI Behaviors, and Gameplay Behavior Smart Objects plugins. Restart the editor if prompted.
Section Results
In this section, you created a new project and enabled the Smart Objects and AI Behaviors plugins. You are now ready to create the AI Gameplay Behavior for your AI Agent.
2 - Create the AI Gameplay Behavior
In this section, you will create the Gameplay Behavior and Gameplay Config Blueprints that define what your Agent will do when it reaches a Smart Object Slot.
In the Content Browser, right-click and select Blueprint Class from the Create Basic Asset section.
In the Pick Parent Class window, expand the All Classes section, then search for and select Gameplay Behavior. Click Select and name the new Asset BP_SO_Behavior_PlayMontage.
Create a new Blueprint class, then search for and select Gameplay Behavior Config. Click Select and name the new Asset BP_SO_BehaviorConfig.
In the Content Browser, double-click BP_SO_BehaviorConfig to open it. Go to the Details panel and click the Behavior Class dropdown. Select BP_SO_Behavior_PlayMontage. Compile and save the Blueprint.
In the Content Browser, double-click BP_SO_Behavior_PlayMontage to open it. Click the Override dropdown next to Functions and select OnTriggeredCharacter.
Right-click the Avatar pin of the Event OnTriggeredCharacter node and select Promote to Variable.
Drag the Avatar variable to the Event Graph and select Get Avatar. Drag from the Avatar node, then search for and select Get Component by Class.
Click the Component Class dropdown, then search for and select Skeletal Mesh Component.
Right-click in the Event Graph, then search for and select Play Montage.
Connect the Return Value pin of the Get Component by Class node to the In Skeletal Mesh Component pin of the Play Montage node.
Connect the Set Avatar node to the Play Montage node.
Click the Montage to Play dropdown on the Play Montage node. Select an animation montage from the list.
If you don't have animations available, you can get free animation asset packs from the Unreal Engine Marketplace, such as the Animation Starter pack. You can also convert any Animation Sequence to an Animation Montage by right-clicking it and selecting Create > Create AnimMontage.
Drag the Avatar variable to the Event Graph and select Get Avatar. Drag from the Avatar node, then search for and select End Behavior.
Connect the On Completed and On Interrupted pins from the Play Montage node to the End Behavior node.
Compile and save the Blueprint.
This example uses the Play Montage node, as opposed to the Play Anim Montage node to use the On Completed and On Interrupted pins to end the behavior. This ensures the Smart Object remains occupied until the animation finishes playing.
Section Results
In this section, you created the Gameplay Behavior and Gameplay Config Blueprints that the Agent will use to play an animation montage once it reaches a Smart Object Slot. You can now create the Behavior Definition that will be used by the Smart Object.
3 - Create the Smart Object Definition Data Asset
In this section, you will create the SmartObject Definition Data Asset that will define the behavior for each Slot of the Smart Object.
In the Content Browser, right-click and select Miscellaneous > Data Asset.
In the Pick Class for Data Asset Instance window, search for and select Smart Object Definition.
Click Select to create the Asset and name it SO_Definition_PlayMontage.
In the Content Browser, double-click SO_Definition_PlayMontage to open it. Scroll down to the Smart Object section and click the Add (+) button next to Slots to add a new Slot. This is the Slot that will be used by the AI Agent when performing the behavior.
Click the Add (+) button next to Default Behavior Definitions and select Gameplay Behavior Smart Object Behavior Definition for Index 0. Click the Gameplay Behavior Config dropdown and select BP_SO_BehaviorConfig.
Save and close the Blueprint.
Section Results
In this section, you created the SmartObject Definition Data Asset that defines each Slot of the Smart Object and its default behavior definition.
4 - Create a Smart Object
In this section, you will create a Smart Object that can be found and used by an Agent in the level.
In the Content Browser, right-click and select Blueprint Class from the Create Basic Asset section.
In the Pick Parent Class window, click the Actor class button to create the asset. Name the new Asset BP_SO_RunBT.
In the Content Browser, double-click BP_SO_RunBT to open it. Go to the Components window and click the + Add button. Search for and select Smart Object.
With the SmartObject component selected, go to the Details panel and scroll down to the Smart Object section. Click the Definition Asset dropdown and select SO_Definition_PlayMontage.
Compile and save the Blueprint.
Section Results
In this section, you created a Smart Object and added the Behavior Definition that defines the default behavior of its Slot.
5 - Create the Behavior Tree for the AI Agent
In this section, you will create the necessary behavior for the AI Agent to search for and use Smart Objects in the Level. You will use a simple Behavior Tree and two custom Behavior Tree Tasks to accomplish this.
Create the Behavior Tree and Blackboard
In the Content Browser, right-click and select AI > Blackboard. Name the Blackboard BB_SO_Agent.
Double-click BB_SO_Agent to open it. Click the New Key dropdown and select SO Claim Handle. Name the key ClaimHandle. Save and close the Blackboard.
In the Content Browser, right-click and select AI > Behavior Tree. Name the Behavior Tree BT_SO_Agent.
Create the Behavior Tree Tasks
Finding Smart Objects
In the Content Browser, right-click and select Blueprint Class from the Create Basic Asset section.
In the All Classes section, search for and select BT Task Blueprint Base, then click Select. Name the Blueprint BTT_FindSmartObject.
In the Content Browser, right-click BTT_FindSmartObject and select Duplicate. Name the new Blueprint BTT_UseSmartObject.
In the Content Browser, double-click BTT_FindSmartObject to open it. In the Event Graph, right-click, then search for and select Event Receive Execute AI.
Drag from the Owner Controller pin of the Event Receive Execute AI node, then search for and select Get Blackboard.
Right-click the Return Value pin of the Get Blackboard node and select Promote to Variable. Name the variable Blackboard.
Connect the Event Receive Execute AI node to the Set Blackboard node.
Drag from the Controlled Pawn pin of the Event Receive Execute AI node, then search for and select Get Actor Location.
Drag from the Return Value of the Get Actor Location node, then search for and select Subtract.
Drag from the Return Value of the Get Actor Location node, then search for and select Add.
Set the X, Y, and Z values of both nodes to 2000. This creates a search box of 4000 x 4000 units, or 40 x 40 meters around the Agent.
In the Event Graph, right-click then search for and select Make Box.
Connect the Subtract node to the Min pin of the Make Box node.
Connect the Add node to the Max pin of the Make Box node.
In the Event Graph, right-click then search for and select Get Smart Object Subsystem.
Drag from the Smart Object Subsystem node, then search for and select Find Smart Objects.
Drag from the Request pin of the Find Smart Objects node and select Make SmartObjectRequest.
Connect the Return Value pin of the Make Box node to the Query Box pin of the Make SmartObjectRequest node.
Drag from the Filter pin of the Make SmartObjectRequest node and select Make SmartObjectRequestFilter.
Drag from the Behavior Definition Classes pin and search for then select Make Array.
Click the dropdown of the Make Array node and select GameplayBehaviorSmartObjectBehaviorDefinition.
Connect the Set Blackboard node to the Find Smart Objects node.
This is what the Blueprint looks like so far.
Right-click the Out Results pin of the Find Smart Objects node and select Promote to Variable. Connect the Out Results node to the Blackboard node.
Drag from the pin of the Out Results node, then search for and select Is Valid Index.
Drag from the Is Valid Index node, then search for and select Branch. Connect the Out Results node to the Branch node.
Drag from the False pin of the Branch node, then search for and select Finish Execute. The Out Results will be invalid if no nearby Smart Objects match the search criteria.
Create a Smart Object Subsystem node in the Event Graph. Drag from the node, then search for and select Claim.
Connect the True pin of the Branch node to the Claim node.
Drag the Out Results variable to the Event Graph and select Get Out Results. Drag from the node, then search for and select Random Array Item.
Drag from the Random node and connect it to the Request Results pin of the Claim node.
Right-click the Return Value pin of the Claim node and select Promote to Variable. Name the variable ClaimHandle.
Drag from the Claim Handle node pin, then search for and select Is Valid Smart Object Claim Handle.
Drag from the Return Value of the Is Valid Smart Object Claim Handle node, then search for and select Branch.
Drag from the False pin of the Branch node, then search for and select Finish Execute.
Drag the Blackboard variable to the Event Graph and select Get Blackboard.
Drag from the Blackboard node, then search for and select Set Value as SOClaim Handle.
Connect the True pin of the Branch node to the Set Value as SOClaim Handle node.
Right-click the Key Name pin of the Set Value as SOClaim Handle node and select Promote to Variable.
With the Key Name variable selected, go to the Details panel and enable the Instance Editable checkbox. Set the Default Value to ClaimHandle.
Drag the ClaimHandle variable and connect it to the Value pin of the Set Value as SOClaim Handle node.
Drag from the Set Value as SOClaim Handle node, then search for and select Finish Execute. Enable the Success checkbox on the node.
Compile and save the Blueprint.
Use the Smart Object
In the Content Browser, double-click BTT_UseSmartObject to open it. In the Event Graph, right-click and search for then select Event Receive Execute AI.
Drag from the Owner Controller pin of the Event Receive Execute AI node, then search for and select Get Blackboard.
Drag from the Return Value pin of the Get Blackboard node, then search for and select Get Value as SOClaim Handle.
Right-click the Key Name pin of the Get Value as SOClaim Handle node and select Promote to Variable.
With the Key Name variable selected, go to the Details panel and enable the Instance Editable checkbox. Set the Default Value to ClaimHandle.
Drag from the Return Value pin of the Get Value as SOClaim Handle node, then search for and select Use Claimed Gameplay Behavior Smart Object.
Connect the Get Value as SOClaim Handle node to the Use Claimed Smart Object node.
Drag from the Owner Controller pin of the Event Receive Execute AI node and connect it to the Controller pin of the Use Claimed Smart Object node.
Drag from the On Succeeded pin of the Use Claimed Smart Object node, then search for and select Finish Execute. Enable the Success checkbox on the node.
Compile and save the Blueprint.
Create the Behavior Tree
In the Content Browser, double-click BT_SO_Agent to open it. Drag from the Root node and select Selector.
Drag from the Selector node and select Sequence.
Drag from the Selector node and select Wait. This node will have the Agent wait for 5 seconds before searching for a new Smart Object, if the initial search was unsuccessful.
Make sure the Sequence node is to the left of the Wait node. This ensures the Sequence will be executed first in the Behavior Tree.
Drag from the Sequence node and select BTT_FindSmartObjects.
Drag from the Sequence node and select BTT_UseSmartObjects.
Drag from the Sequence node and select Wait. With the node selected, set the Wait Time to 2.0 and Random Deviation to 0.5. This node will have the Agent wait between 1.5 and 2.5 seconds before searching for a new Smart Object.
Save and close the Behavior Tree.
Section Results
In this section, you created the Behavior Tree and Behavior Tree Tasks that allow the Agent to find and use Smart Objects in the Level.
6 - Create the AI Agent
In this section, you will create the AI Agent that will search for Smart Objects in the level.
In the Content Browser, double-click the BP_ThirdPersonCharacter Blueprint to open it.
Select all the nodes in the Event Graph and delete them. Right-click in the Event Graph, then search for and select Event Possessed.
Drag from the New Controller pin of the Event Possessed node, then search for and select Cast to AI Controller. Connect the Event Possessed node to the Cast to AIController node.
Drag from the As AIController pin of the Cast to AIController node, then search for and select Run Behavior Tree. Click the BTAsset dropdown and select BT_SO_Agent.
Compile and save the Blueprint.
Section Results
In this section, you created the AI Agent Blueprint that will search for Smart Objects in the Level. You also modified the animation Blueprint to make sure the animation montage can play correctly.
7 - Test the Smart Objects
You will now test the Agent to make sure it can find and use the Smart Objects in the Level.
From the Main Toolbar, click Add Content (+) > Volumes > NavMeshBoundsVolume to add a new Navigation Mesh actor to your Level. Scale the Mesh to cover your Level so the Agent can navigate to its destination.
Drag several BP_SO_RunBT Blueprints to your level.
Drag the ThirdPersonCharacter Blueprint to your Level.
Press Simulate to see the Agent find and use Smart Objects in the Level.
Section Results
In this section, you confirmed that the Agent can find and use Smart Objects in the Level.