This is first part of two tutorial.
This one contains how create simple interaction with user and actor in game. And in second we create simple dialog mechanic to use.
Here you see this interaction in use. It’s exactly same for box and characters in video.
Creating actor to interact
First we need to interact different objects in game (well we could make just run event from keybind run but it’s hard when there is many actors what run same code).
I decided to use Blueprint Interface, because it’s so simple to use.
So let’s create blueprint interface.
Name it whatever you want, I named it UserActions.
Now open interface and rename function to “Interact” (you can use name you want just remember later what you called it).
Now we’re done with Interface and you can close it.
Now open ThirdPersonCharacter blueprint in blueprint folder.
First we need to add to this blueprint new UserActions interface. On top panel click class settings icon and on right side there is now class settings open.
On interfaces click Add and select interface what you just created.
After that Compile and Save.
Now we can implement new Interact function in BP.
Right click in BP Event Graph and click context sensitive off. Now you can find Interact (Message).
(This is what it looks like. If it doesn’t look same it means you have taken wrong Interact node.)
Now this have 2 inputs and 1 output. First is execution pin where we create soon action button.
And target, this is where we put target who we are commanding with interface. But how we get target? There is many ways to do this but in this example I take easiest way…
Getting overlapping actors to our Other Actor
On Components list drag and drop CapsuleComponent to Event Graph to create CapsuleComponent Getter (it shows error but it works). From it drag node and search “Get Overlapping Actors”, you don’t need to put anything to “Class Filter” so it gets all actors what is Overlapping with capsule.
Now you get Array variable from this node, drag from it and search “Get” node so we can specify what Actor we want. But what actor we want? Well for this I use Array Node called “Last Index” so we get last actor who was put in Array (last actor we are overlapping). Now you can drag Int variable from “Last Index” to “Get” node.
Now we can drag actor to “Interact” node to get target with who we interact. There is one last thing what is good to add is check if Actor implements UserActions interface, so we don’t call this action towards floor and other things. So drag actor from"Get" node and search “Does Implement Interface” and select it.
Actor should go right away to “Test Object” input and we still need to select Interface what to test. Output is boolean what is true if Test Object got Interface called “User Actions” (what we created before, but add later to interaction target). Drag from boolean and create “Branch” node so we run code only when test is success. Now you can drag true to Interact node.
Now we need to fill Execution pin, create new action mapping input in project settings. Edit => Project Settings => Input => Action Mappings => + sign to add new Action Mapping => Name it “ActionButton” and put button as E or whatever you want to use.
Now you can close Project settings window. And after that save project (ctrl + s).
Now go back to Character blueprint and right click in Event Graph. Search for ActionButton and there should be it now. Click it and you get InputAction ActionButton node. Drag execution to Branch node, now you’re ready with this blueprint.
(here is how it should to look like.)
Old way, not so good actually. For testing purposes ok?
Now our character blueprint is finished. You can close it now.
Create “NPC” character with who we will interact
Now lets add some components for it.
- Add scene component and drag it over root component to make it new root(optional)
- Add capsule component(to do collision tests).
- Add skeletal mesh
Align all well with root and make collision capsule “big”.
Collision capsule Begin Overlap and End Overlap isn’t necessary for this but with this you can chance stage of Widget what shows text on top of target when user is close enough. So basically you need just “Event Interact” node to do magic.
First lets add interface for this actor like previously for user character.
Now add node called “Event Interact” this is event what triggers from user to this character when you press action button.
This is not nessessary but helps if you want to example to show Widget over head when you can interact with this actor.
Now we’re seriously finished for this part.