How do I interact with objects in my environment by left mouse button clicks?

Hi, excuse my utter lack of knowledge here, inside the FirstPersonCharacter Blueprint, I have set up line tracing to register accurate left mouse button clicks.

I plan to set up various objects in my environment that are interactive to left mouse button clicks. pulling levers, animating objects, pushing buttons, etc. All using blueprints.

How would I go about making these interactive objects? Do I create blueprints from the meshes of these objects? Do I set it up inside of the level blueprint? Also, how do I get the FirstPersonCharacter blueprint to work an ‘talk’ to these blueprints? Any help on the matter would be greatly appreciated, I have attempted on click events, I’ve set up a ‘use’ action inside inputs in project settings, but nothing seems to be working.

Thanks in advance!

Hi!
For this case, I suggest using [Blueprint Interface][1].

The ideia behind using Blueprint interfaces for communication is to try to call a specific function interface, no matter the class inheritance (which can get quite messy if you have lots of interactable objects.

So, with you create a blueprint interface and a function inside it (like the documentation above shows, all you have to do is inside each interactable object, [implement the interface][2] and do whatever specific behavior you need to do (like animate the leaver).

So all you need to do to communicate to blueprints that implement this interface (via FirstPersonCharacter blueprint or wherever) is to call the function for the hit actor (no ugly casts and things like that).

I hope that was a clear and helpful explanation. :slight_smile:

Hi DanZaidan! Thank you so much for your reply, this community is so bad ■■■, I’m really going to pick your brains here, again I’m sorry if I’m picking this up slowly, but I’d really like to learn this, so I’ll be speaking in layman terms.

So I’m certain I understand the logic behind BP Interfaces after reading the documentation. I created an interface called it ‘BPInterface’, and just have a function inside of it that I haven’t tampered with, and called it ‘Interact Function’ (This could be my first hiccup, do I need to alter settings in the details panel here?)

I then have a bar stool mesh in my scene, in which I would like to interact. I dragged the break hit result from the Out Hit of my line trace, and hooked it up to my ‘Interact Function’, just as you had posted in your image.

This is where I get a little lost, I then just had the bar stool mesh print a string ‘you hit the stool’ when clicked, triggered by an on click event. I then dragged a node from that to the ‘Interact Function’ again, and referenced self.

I’m a little out of my depth and I’m sure it’s because I’m missing some fundamentals here. In this instance I’m basically wanting to achieve the following:

Walk up to meshes in the environment and click on them to interact, you can only interact with meshes if you are close enough to them. My thinking is that the line trace is the accuracy of the click, and the multiplied float value determines how close you need to be. My understanding of the interface is that my left mouse click will communicate to various meshes in the environment that interact in a manner of different ways, and ‘interact’ can yield all kinds of results depending on what is clicked, so way more complicated than LMB solely being fire gun, or punch, or whatever.

Thanks again in advance!

No problem!

You are totally right about the logic you described in the last paragraph: The line trace is perfect for this, the float you are multiplying by the direction vector (Actor Forward Vector) will control the length of the line and the BP interface will allow each object to react differently to the same action (being hit by the mouse via line trace).

I see you are debugging the line trace, so I`m assuming it is hitting the desired object correctly.

The reason it is not working correctly yet is the final step (described in the third screen shot) - the others are great. In the Interact Bar Stool Blueprint (and all the objects you want to interact with the Left Click), you should add the interface in the Implemented Interface (Class Settings):

Then (after compiling the blueprint) you will be able to override the event to create a specific behavior for the bar stool by creating an Event Interact Function (In my case, it is called Event Click Interaction).

Note that there is only an output execution pin, so that this event will be called in other places (the line trace) but will execute its own commands.

This way, there is only one place handling the player input (the single InputActionUse event); and several places controlling what Use means for each object. This is really easy to expand or delete later on (for instance, the player has to have a special item in order to be able to interact. By placing a single Branch node before the line trace, you control whether or not the interface message will be called for all interactive actors!).

To recap: The Click Interaction (Message) is used by any actors that wants to communicate with an actor that implements this interface. The Event Click Interaction is used in the actor that implements the interface (like the bar stool) to write its own specific behavior. :slight_smile:

Best of luck! :smiley:

Thanks again for explaining these steps with me DanZaidan I really appreciate it.

I made the changes to the implemented interface in the class settings of the bar stool blueprint. I then had the Event InteractFunction replace the Event ActorOnClicked in the bar stool blueprint. My hits are indeed registering when viewed through debugging so that’s definitely not the issue. I’m thinking it may be an issue outside of editing the blueprints themselves, and I’m missing it. I changed the print string to a rotation of the mesh, just in case it was something to do with the print string, and seeing the mesh rotate visually is more of a confirmation that it’s working, but still no luck.

I’ll just run through the steps that I took to set it up, just in case I’m doing something wrong.

  1. I imported the mesh, placed it in my scene.
  2. I right click on the bar stool mesh in the content browser>asset actions> create blueprint using this…
  3. I create the blueprint in the screenshot below, making sure the implemented interface is correct in its class settings, compile, and save.

  1. I run the game, clicking on multiple instances of the bar stool, seeing the hits but no rotation (or printed strings before I changed the blueprint).

Do I have to attach the blueprint to the meshes in the scene or something? Or will all instances of the bar stool rotate when clicked? The pins of the bar stool’s blueprint don’t fire up at all as I’m playing.

Thanks again for tolerating my barrage of questions.