By the end of this journey, you'll be able to look at almost any actor in the world and see its name, and you'll have an introduction to a very useful form of collision detection in Unreal Engine: line tracing.
You'll also be able to delete any actor your character looks at!
For reference, you can download archives of my project files from before this journey began and after its completion.
There's a saying, "It's a bad carpenter who blames their tools." I never really got that. Maybe the idea is that a good carpenter would invest in better tools? What about poor carpenters who are just starting out? Seems a bit harsh.
At any rate, as mod developers, it's definitely true that we can't blame our tools, because we can make our own tools! To that end, we're going to begin working on a blueprint that let's us inspect some aspects of the game in real time.
To get started, we'll make this blueprint tell us the name of whatever actor the player is looking at.
To see what we're looking at, we're going to be tying into the physics system and tracing a line beginning at the camera and ending somewhere in the distance. The line will begin at the camera position and the endpoint will be based on the direction the camera is facing.
AdventurousMod
folder, create a new Actor blueprint called BP_ActorInfo
and open it for editing.Get Player Camera Manager
node.Get Camera Location
and Get Actor Forward Vector
.Get Actor Forward Vector
will return a unit-vector (a vector of length one) which points in the direction the camera is facing.
we'll want to look forward far off in the distance, so we're going to multiply this vector by 10,000, which will make it 100m in length (since the units are in cm).
Get Actor Forward Vector
node, drag off from the Return Value
node and type in *
in the search bar, and
select vector * float
.10000
in the float box.
We want two points in world-space to define a line. We have one of them (the camera location), and we have a directional vector 100m long. If we just add the camera location to this vector, we'll get the other point.
Get Camera Location
node, drag off and type +
in the search bar. Select vector + vector
.X
node into the second box of the addition.
Now we have the two endpoints to define our line and we can tie into Unreal's pysics system with a line trace.
LineTraceByChannel
node.
Get Camera Location
node to the Start
input of the LineTraceByChannel
node.vector + vector
node to the End
input of the LineTraceByChannel
node.Event Tick
node over and drag its execute pin to the LineTraceByChannel
node.
>
There's a lot of ways you can make a graph prettier, like straightening the connections between nodes. Hold shift and select multiple nodes, then right-click on them and select
Arrangement
→ Straighten Connections
. Or just press q
. It makes a difference which node
you've selected last. There's also other options under the Arrangement
menu; I recommend you play around with them to see what you like best.
In the screenshot above, I straightened the connection between Get Camera Location
and LineTraceByChannel
, and aligned the column
of nodes in the middle to the right.
Now we have a way to know what actor the player is looking at, but we need to do some legwork to print info about it on the screen.
The first thing we need is to save a reference to our HUD so we can set the text on it. This should be something you're familiar with by now:
BeginPlay
event, drag out a new Get Actor of Class
node and set the class to BP_AdventurousMod
.Return Value
a Get HUD Ref
node.HUD Ref
.
Now we can use our reference to write to the HUD.
LineTraceByChannel
node, drag off from Out Hit
and create a Break Hit Result
node.Now we can see everything that gets returned to us when a line trace completes. It's a lot! Some of this won't apply to every hit, though.
For instance, Hit Bone Name
will only have a value if a skeletal mesh was hit.
We're most-interested in Actor Hit
.
Actor Hit
, drag off a Get Display Name
node.Get HUD Ref
node. From there, Drag out Get txtOutput2
→ SetText (Text)
.Return Value
from Get Display Name
to the In Text
input of the SetText (Text)
node.
This will automatically create a ToText (string)
conversion node.
LineTraceByChannel
node to the SetText (Text)
node.
BP_AdventurousMod
blueprint.SpawnActor
node and create a new Spawn Actor from Class
node.BP_ActorInfo
blueprint.Spawn Transform
input to set the position and change the collision rules to ignore collisions and always spawn.Run around and take a look at things. This should help you get a better understanding of how things are constructed in the space rig, and might just give you some ideas of what to mod and where to start.
You'll notice that you don't see a description for everything. That's because we're only getting the names of actors, and only when they have collision volumes attached. Still, you can see a lot of stuff!
This next part isn't useful for modding per se, but it will be fun!
We're going to add the ability to delete any actor by looking at it and pressing delete
. All we need is to store a reference to the actor being viewed,
and add an event to destroy it when the delete
key is pressed.
Event BeginPlay
node; we'll want to add another node after the
Get HUD Ref
collapsed code.
Oh no! We don't have any execution pin to drag off of from the Get HUD Ref
node! We can easily fix that, though.
Get HUD Ref
node and look at the Details
pane. If you don't have this pane, you can open it by navigating to
Window
→Details
in the top menu.+
next to Outputs
to add a new output.Then
and set its type to Exec
.execute
and execute outputs Then
to keep any text from showing up next to the pin.
Get HUD Ref
node (double-click on it) and make sure the execution output is connected to the rest of the graph.
Otherwise, we'll be stuck with a subtle bug where nothing that comes after that node will be executed, and the reason why won't be obvious.
Enable Input
node after Get HUD Ref
and drag off the Player Controller
input into a new Get Player Controller
node.
Enable Input
node will be the reason.
Next, we'll store a reference to the actor being viewed.
Break Hit Result
node, Right-click on the Hit Actor
output and promote it to a variable called Visible Actor
.
SetText (Text)
node to delete it, then insert the Set Visible Actor
node into the execution flow.
Delete
keyboard event.Pressed
pin, drag out a new Destroy Actor
node.Visible Actor
reference and set it as the Target
of the Destroy Actor
node.