player interaction with object, is it correct to show interface in this way?

Hello, I’m trying to make it so that when the player approaches an object, press E is displayed on the screen. To do this, I am using what is shown in the image, and I am doing it this way to use soft references, as I understand that using ‘cast to’ generates hard references
Is doing it this way correct?

BP call BPI and this call WB

BP is the object that is interacted with.
WB contains the text displayed on the screen.
BPI is what helps them communicate and thus be able to use soft references.

image

Hard references to interfaces are fine, especially for an interface that you will be using all the time like the interaction interface.

What I typically do is put an Interactable component on interactable actors, and an InteractionDetection overlap volume on the character. Then, in InteractionDetection OnBeginOverlap, I get the overlapping component, cast to Interactable interface, and get the text to display, and show the overlay GUI. I also remember the component that overlapped in a variable, that I clear in OnEndOverlap.

When the user presses E, if there’s a current saved interactable object, call that interface again with a “DoInteraction” message. That component will typically just use that message to invoke an event dispatcher, so the interactable component can be added to any actor, and the actor event graph can then hook up what to actually do during interaction.

So, the Interactable interface is:

GetInteractionText()
DoInteraction()

The InteractionComponent has variables for InteractionText and an Event Dispatcher for Perform Interaction that its actor can configure.

The player adds an overlapping Trigger Volume hanging off the root (or sticking out the eyes, for an FPS,) poking out ahead, to detect things to interact with, and wires up On Begin Overlap and On End Overlap, as well as the GUI widget to display the text, and the Input Action to perform the interaction.

None of this needs “soft references” because the size of the involved components is minuscule, and the character and interaction doesn’t need to know about the concrete actor being interacted with at all.

This is so common, I documented it. I should turn it to a tutorial or something …

Interface definition:

Player character definition:

Player controller definition:

The configuration of the collider box:

Without knowing exactly what your interface and code looks like, I think you are correct in the implementation.

The interface should be on the player and have it return the player controller. When a collider on the object overlaps with the player it can perform an interface call on “other actor”. Since the player is the only one with this interface you’ll only continue the function if the other actor is in fact the player.

Then have a function on your player controller that shows “press e” on the screen; reverse with OnOverlapEnd to hide it. Call that function from the returned interface reference on the object.