By popular demand I created this Blueprint tutorial variation based on my earlier UsableActor tutorial in C++. In case your project has a C++ back-end already I would recommend following that tutorial instead. In this tutorial we will implement a framework to interact with objects in the world. The tutorial includes event hooks to use an item and handle object highlighting and supports multiplayer.
Basic knowledge of Unreal 4 and Blueprints is expected, I will focus on getting the feature to work and less on teaching you how to handle Unreal 4’s Interface. If you have any questions, feel free to ask them in the comment section!
Project source is available on GitHub!
The character performs a ray trace each tick that find the currently looked at actor that derives from our “UsableActor”. The first and last frame we look at the actor we trigger events that can be implemented to for example highlight the object or show a HUD message. Finally, the “E”-key triggers the OnUsed event that should trigger some kind of response in Blueprint, like adding it to the players inventory.
First create a new Blueprint that will act as our item to be destroyed, derive it from StaticMeshActor. Make sure you assign a mesh to this blueprint drag it into your level and make sure it’s visible so we can start using it. By deriving from StaticMeshActor any child blueprint can easily overwrite the mesh property once you start implementing multiple item types.
Before you continue, make sure your mesh has a collision primitive. This is required in case you do not check “Complex trace” in the character’s line trace node (Part of BP_Character’s GetUsableActor later in this tutorial) You can create a collision primitive in the Static Mesh Editor under “Collision” in the top menu.
The following simple EventGraph will destroy the Actor from your level as soon as you press “E” while aiming at the object. Nothing fancy, for more interesting behavior I recommend you check out part two of this series where we use this system to create a basic inventory & pickup system.
Note: The Begin & End focus functions will not have any effect unless you have an outline effect (The effect is available in part two of the series, you may skip this for now)
To enable multiplayer…
In BP_UsableActor go to Defaults-tab -> Replication category -> enable “Replicates” property. You are now one step closer to having multiplayer support, the remainder is implemented in the Character blueprint.
Setup your character input
It is important to bind “Use” to a key, this can be done through the Project Settings in Unreal. To open this window go to Edit > Project Settings…
The character handles the line trace to select the actor in view. The Tick event triggers Start/End focus events to be implemented by the UsableActor in Blueprint and finally the Use-event (mapped in the Input window) will trigger the pickup/activate/destroy or whatever you decide to implement in your blueprint.
Variables to add
- MaxUseDistance – float (default: 800)
- LastUsableActor – BP_UsableActor (default: none)
- IsNewFocus – bool (default: false)
Function output: Add a new Output with name “UsableActor” of type “BP_UsableActor”.
In case you are stuck creating a “Cast-to” node, watch the GIF below:
To create the “ServerOnUsed” node you must right-click and select “Create Custom Event…” we need this for function replication as input is executed on the client side, and our use logic must be send to the server. Make sure the event matches the properties in the red box.
In the graph below we handle start & end focus on a item in the world. Add this to the primary EventGraph of your character. Tick-event runs on the server so we need to replicate it to the owning client so we can highlight our object.
Note: I’m replicating this to the client because we are only doing local visual updates that must run on client-side, if you are handling gameplay logic in the tick you should consider changing the blueprint to run on the server instead (or run parts on server, others on client). For now leave it as is below.
If you completed the tutorial you have the basic framework to build your own blueprint interactions (eg. door, lever, pickup item, etc.) For a practical use go to part two of the tutorial series where I use this system to create a basic inventory & pickup system. Part two was originally created based on the C++ UsableActor, but is fully compatible with this tutorial.
Project source is available on GitHub!
Continue to part two where we implement a basic inventory system!