Multiplayer Interaction System in Blueprint

Introduction

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!

Last Updated: 3 November 2018 (Created: June 14 2014)

Project source is available on GitHub! (Last Updated: Unreal Engine 4.20)

ue4_tutorial_usableactor02

The Concept

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.

BP_UsableActor

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)

Three Functions

bp_usableactor

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…

Project Settings

BP_Character

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: GetUsableActor

Function output: Add a new Output with name “UsableActor” of type “BP_UsableActor”.

getusableactor01

In case you are stuck creating a “Cast-to” node, watch the GIF below:

ue4_castto_procedure_07

EventGraph

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.

character_use_02

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.

character_handlefocus_02

Closing Words

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!

Multiplayer Demo

Lever Demo

Continue to part two where we implement a basic inventory system!

69 Responses

  1. Hi Tom
    I have an issue,
    I was following the tutorial but now my sphere of example doesn´t have the line around it, in the custom render depth the target is primitive component and i don´t know how to change it as you show in this page. Also when i´m going to collide with the sphere, i cross it and i can´t touch it. Can you help me?

    • There is a couple of things to check.

      Make sure your object has a static mesh, and that you set the custom depth to true on this mesh.

      To check if your outline is working, check the Set Render Custom Depth in the properties of your static mesh compontent of your object by default (so it should show an outline as soon as you play, not just when you overlap)

      Make sure your static mesh has collision assigned, or the trace won’t work.

  2. It wont work for me. The client cant destroy the actor but only the server can do. I even downloaded the example and even there it wont work for me.

  3. Hi Tom look, first thank u for the example, but in your sample th blueprint has to used the get usable actor call to next error “Error Blueprint Runtime Error: Accessed None trying to read property CallFunc_GetUsableActor_UsableActor from function: ‘ExecuteUbergraph_MyCharacter’ from node: On Used in graph: EventGraph in object: MyCharacter”, I dont´t have idea was happened and dont know how to solve this problem i using engine 4.11

      • Hi guys,

        Have a look at the updated graph below. This should avoid the issues you are seeing. I’ve added an IsValid checks on both beginfocus and endfocus in the Character blueprint.

        Updated graph to avoid your errors

        (Right-click open in new tab to view full-size)

  4. I’m having trouble with this blueprint, when I put in the blueprint word for word, I wasn’t able to get it to function. I traced everything to the smallest detail, and the blueprint is still not functioning. I played it in the editor, and found an error message that traced it to the onused node, connected to the useableactor. The engine I’m currently using is 4.10

    • Haven’t used that in a while (this tutorial was originally created about 2 years ago) Looks like it’s just missing a IsValid check before calling BeginFocus. Seeing any unexpected behavior or just the warnings?

  5. Hi Guys, I have a strange issue. When an Item is focused, the custom depth is set and all players see it, even thought its supposed to only show to the owning client. Any idea how this is happening?

    I have the function is replicated in anyway, so very confused how this is happening. The object is replicated, but that I assume isn’t the issue?

    • It means that you are calling the function that sets the custom depth on all clients and not just the owning client. For example the Tick() is called for everyone, you might want to check if the pawn is locally controlled before calling begin/end focus functions.

  6. Hi Tom,

    My End Focus is not kicking off when looking away from the Usable Actor it does execute if I look at another usable actor. I have debugged it all and I can basically put it down too.

    When you cast from the function and look at usable actor it stores it in the variable when you look away you’re not picking up an object from the cast to so it never executes EndFocus, it can’t compare 2 objects when its not picking up an extra object if you look away so it never sets it off.

    Any ideas? I may just modify the sequence events to execute the EndFocus straight after look away from it.

    • Ignore my first post It’s because I didn’t execute the Cast failed to the output of the Function. I am getting a ton of Accessed None fails for Begin Focus now tho.

  7. So im having an issue as to where the Usable Actor Variable is not showing on the GetUsableActor node. Any reason why?

    • Did you add an Output named “UsableActor” to the GetUsableActor in the details panel when selecting the function? The blueprint may need to recompile and the node may need to be re-create to refresh.

    • That should be totally fine if that is desired for your game. I don’t really see why you want to unfocus the item when you’re still moving over however.

  8. Hey, thanks. That did help. Got another question though, and you seem cool enough to answer it…

    Lets say I wanted the pickups function to only allow you to take one of that specific actor. So if I had a 2 candles on the table and I wanted the player to only be able to grab one, what would be the best way to begin doing that? I tried checking the inventory array with a contains node, but it treats all children of “inventoryActor” as being the same object, so if I try to take, say, a candle and a book it treats them all the same and disallows it…

    Hope that was a clear enough question. Thanks again.

    • Hi Bonehead,

      You would need a more specific comparison to check if that item is allowed. Eg. child of Candle instead of InventoryActor. You could take a more abstract approach and add a maximum allowed carried variable (integer) and when picking up your item check if there any items with the same class (GetClass is a node you can pull from every item to make a comparison)

      I hope that helps you get started!

      – Tom

  9. I’ve got a stupid question. I have it functioning. But let’s say I have my parents ‘on used’ function set to destroy actor (like in the tutorial), but I don’t want the children to do this. For instance, if you have a switch on the wall to open a door, and an apple on a table that are both children of “usable actor” how would you make them function differently? I have a few cumbersome ideas, but I’m curious it’s intended to work.

    • Hi bonehead!

      Functions can be overwritten in Blueprint, usually you need to call the parent function if you don’t want to lose the original behavior (By right-clicking the function input node in the overwritten blueprint and looking for “Add Parent Call”)

      I did not intend the Destroy node to exist, it’s mostly a demo’ing call for the tutorial only. But if you wish to extend/overwrite behavior you can for each of the derived blueprints you create.

      I hope that helps!

      – Tom

  10. Hey Tom, I’m running into a problem where I can’t find the node where you Call the End Focus Function. Would you know how I would be able to do so?

  11. I’ve got one more question for you. Please explain me this – when I create basic inventory form your tutorial, what am I supposed to do with function “get usable actor” from first tutorial “usable actor”? when I plug my action button to adding item from the world (inputActionUse -> get usable actor – onused) nothing work, no outline, no item add… I was creating this tutorial for five times and sitll dont work… some part is unclearly for me. Please help Tom!

  12. Hello!

    You’ve done really amazing job with these tutorial series. I was searching this kind of tutorial for long time, but get to the point . There is a way to create a optional blueprint for singleplayer? what I need to change? I woudl be very grateful!

  13. Hello Tom,

    I’m trying to follow this setup to get a simple inventory system going. I want a character to be able to grab certain objects in a level. Carry them around, and then when ready place them back in the world. I’m going to figure out the appearance/placement stuff later down the road but for now I’d just like to grab the object store it and then place it again.

    I’ve followed the tutorial and taken a look at your source files to try and get this to work, but I can’t seem to figure it out. I’m in 4.4, the previous comments helped me with some of the trouble I had.

    I currently have everything setup, the only problem I’m having now is creating the – LastUsableActor – BP_UsableActor (default: none) variable. Whenever I make mine, it doesn’t look like yours. I tried basing my BP_UseableActor off of a Static mesh, like yours but still It doesn’t work. I don’t really need the Begin/End Focus stuff, nor any outline stuff. I also don’t need to replicate as the game is local multiplayer only.

    I’m also getting a lot of mismatched type errors for the references to Self for some reason. I’ve used the context sensitive typing trick and tried to make my nodes match yours and if I can’t I copy pasted them.

    I don’t quite understand how yours works or why my variables aren’t quite like yours. Any help or explanation would be appreciated.

    Here are some images of the issues. When I press E I’m not even getting a trace to appear (Turned on the debug so I could try and see it.) and it looks like I’m not even entering getUseableActor function.

    Thanks

    • Hi John,

      Don’t see any images to look at. Could you provide the links so I can check out your errors?

      I tried the source in 4.4 and it works without change.

      There really isn’t a lot going on in the BP_UsableActor class, the primary thing that could be wrong here is the base class of StaticMeshActor (especially since you mentioned it looked different, assuming a different icon)

      First thing that comes to mind when the trace isn’t triggered it could be because either Input is not set to “E”, a different character class is used in the current map, or “E” is already swallowed by another event and never triggeres for your character.

      Hope any of that helps, otherwise perhaps those images can give me some more detail on what’s going on.

      • The project “UsableActorBPTut.uproject” work on UE 4.9.0, but produce following errors after stopping:
        Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
        Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
        Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
        Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
        Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
        __________________________________________________________________________

        Note: these errors raise only if crosshair is at least on top of one “BP_UsableActor_X” actor

        • I’m getting the same error in ue4.9: Error Accessed None ‘LastUsableActor’ from node Begin Focus in graph ‘EventGraph’ in blueprint MyCharacter
          Did you figure it out?

  14. Hey, great tutorial!

    I’m having some trouble, though. It seems that Use doesn’t work on my Client. I set it up slightly differently, putting all this in my Controller instead of the Character. Everything else works as intended, but E does nothing on the Client. Any ideas what I might be doing wrong?

    • Hi Rook,

      I would place this in the Character since Controller does not replicate like Character does. So your ray-casting may be completely off when trying this on client-to-server. In the GetUsableActor enable the drawing of the debug line in the Line Trace node to see where it’s tracing on the server and the client (note it’s executed on server-side, so probably never shows up on your client and is only visible in the server’s viewport)

    • Hi Aaron,

      I think this happened because the class I used at the time used Actor instead of StaticMeshActor, my bad. You can still access the Static Mesh through the component – this is what I actually use now in my UsableActor. Self -> StaticMeshComponent -> Get Static Mesh

      I hope that helps!

  15. Hey, not sure if you’re still posting on here but worth a shot ! 🙂

    I could do with some help, The Highlighting system is going incredibly wrong for me, I have followed your guide to the dot, which is awesome by the way, thank you for sharing, anyway..

    The Issue I’m having I can’t even explain, so I am going to try my best, When looking at an item the highlight “flashes” I’ve noticed that it does not seem to have consistency as when I look at an item, it may not flash straight away, it may be delayed or it may even flash numerous times whilst moving my mouse around the object, I have gone back and forth reading every word out loud so I don’t miss anything and still to no avail.

    • Hi Munky,

      That’s really bizarre. I’ve added the source download at the top of the page that could help you figure this out by comparing. Try to place Breakpoints on your “Set Custom Depth” nodes in begin + end focus events to see when they get triggered, maybe something else is triggering them as well. Or perhaps your post process volume gets overridden by something else (have only 1 and make sure it’s set to unbounded = true)

      • Hi Tom,

        Thank you very much for the tutorial, it is really useful.

        I am having this same problem where the highlighted item flashes but does not keep the highlight material. The Use button works for me, even not seen the item highlighted if i look at him and press E it gets destroyed and if I am looking away it does nothing, but it only shows the highlighted material during less than 0.1 seconds (I think the reason is that it shows the material when focus begin, but it does not keep it showing until focus end as supposed.

        I downloaded your example version and everything is exactly the same as in my project. Do you know what can be causing this? I am working in the version 4.19, I know this project was done in an older version but everything should be the same.

        Thank you very much in advance.

  16. Thanks for posting your source! It helped me find the issue I was having (I accidentally cast to the Character class and not my own MyCharacter class).

    Just wanted to say that I was having an issue with this script using my custom character set up. I eventually realized that the trace was not going to the accurate location because the “Base Eye Height” did not reflect my camera’s height. This wasn’t an issue on large objects, but on smaller props it became apparent when the outline would appear only when I aimed directly over and not on the object.

    Here’s a link to how I set mine up so that it was based on the camera in my character’s component rather than the arbitrary eye height.

    http://i.imgur.com/8fS46sQ.png?1

    • Hey thanks that’s a better approach for accurate traces for sure! I’ve added this to my to-do for a future update of the tutorial.

  17. First of all, thanks for the download link.
    I got a question. I want to create different Items which should react in a different way. As an example item 1 one should get destroyed in OnUsed and item 2 should spawn a smoke or something like that + get destroyed. How can i make a difference if the OnUsed function is in the parent class and not in the child BP?

  18. Hello, how do you get the “self” link to OnUse, BeginFocus and EndFocus in the Character Event Graph? What i mean is the function just shows Target and not Charakter like in your picture.
    And how do i get the Static Mesh Variable? When i tried it last time it was already there but this time there is no Variable in the Function do i have to create it by myself?

    I dont know where the failure is. The Trace Line recognizes the box and the “E” press works also (checked with print string) but somehow it doest destroy the object i dont realy know whats wrong. Could you upload your Blueprint? Would be so nice of you.

    • Hi Majek,

      To get the “Character” output node for OnUse etc. you need to add this as a custom pin in the properties of the function. To do this you must select the function and check the Details window for “Inputs” add the character input to all three functions.

      The lack of Static Mesh may depend on your base class/blueprint. Perhaps you didn’t derive it from StaticMeshActor ?

      I will make a downloadable project available in the near future, need to rebuild it since the original got a bit clobbered with additional experiments.

      • No problem! Glad you found it useful.

        I’m collecting all feedback and will make an update soon. (including a project download link)

  19. Would you consider posting your Blueprint source as well? I’m following along but running into a couple of snags, and it would be nice to be able to test a working version locally to see where I am going wrong.

    • This is a good idea. I have multiple tutorials in a single project so I will need to rebuild them for your own sanity when digging through it. I will try to get around to this though as this sounds very helpful.

  20. Hi, I’m having a small issue, for some strange reason the ‘End Focus’ event is never called when I look away from the interactive item, and the ‘highlight’ only stops when I look at another interactive item.

    Is there something I could have missed? I’ve been over this like 10 times already.

    • Hi noms,

      Try adding “Print String” to the two False-outputs of the two Branch-nodes in the “End Focus” part of the blueprint. This may give you some insight into what’s going on.

      The order of the sequence node is important as well. End Focus before Start Focus.

      I hope that may help, I don’t see what else could be wrong without seeing your Blueprint. If you’re still having issues after, you can send me your blueprint .uasset or the project in case it’s you don’t mind sharing it in private.

  21. Your response was perfect. I thought all the nodes go in the Character BP Event Graph. After reading what you wrote I realized they all need to be moved into the GetUsableActor function. Thank you!

  22. I too am having some a bit of trouble. On the GetUsableActor part I created the GetUsableActor and created the output, when I drago off of single line trace for objects I’m able to call GetUsableActor but it looks different then the one in your image and it has a usable actor output as well. Also I can’t seem to get the returnNode to appear from the output of cast to BP_UsableActor. Am I missing something?

    • I’ll try to give a few pointers as I’m not quite sure what is going wrong for you.

      Make sure you step into the GetUsableActor function (double-click on the “GetUsableActor” on the left panel) to create all the nodes of the image. The line trace node should be created inside the function.

      I will create another GIF to clarify the steps required for this function. Let me know if the above helps, or if your issue is somewhere else.

  23. THANK YOU! Was banging my head into the keyboard hoping I could get those to show. Your advice worked perfectly. Thanks again!

  24. One quick question regarding this system… is there a trick to getting the “End Focus”, “Begin Focus” and “On Used” functions to appear on the player blueprint? For the life of me I cannot get these nodes in play. I am sure there is just some trick to it, but since you didn’t go into detail about it I’m really not sure how to troubleshoot that. Any tricks?

    • Drag a line from the usable actor (the one that is cast to the correct type) and with context sensitive enabled you should have the events.

      I will explain this in a gif in a future update, it requires more than basic understanding of BP editor to konw how to do this! Thanks for the feedback and hopefully this helps you on your way.

Leave a comment on this post!