How to achieve something like Interface variables?

In my game, I have a way for the player to interact with things using an interact interface by checking what the player is looking at every tick (via a trace), and this has worked fine for the most part, but I wanted to implement some functionality into the player’s hud that showed some text and an icon relating to the thing they were interacting with.

I realize now that an interface itself won’t really cut it anymore since interfaces cant store variables, and I want the interactable object to store a text variable and an icon. What is the alternative I should use?

I am using c++ in case it matters.

What I do in this case:

  1. I create an interface Interactable that I implement on things that can interact.
  2. The Interactable interface has three methods: GetDescription() -> String, IsEnabled() -> Bool, and Interact()
  3. I make the player have an array of Interactable
  4. I define an interface Interactor that has two methods: AddInteractable() and RemoveInteractable()
  5. I make interactables have an overlap shape, which overlaps pawns, and when something that implements Interactor overlaps the shape, call AddInteractable() and when it ends overlap, call RemoveInteractable(). Typically this overlap shape is set to only overlap Pawns.
  6. I make the player Pawn implement Interactor to add/remove to/from its slice of Interactables and call a function on the HUD to update list of interactables
  7. In the HUD, when the function is called, I show/hide a widget that’s at the center of the screen, and I add items to the widget based on what’s in the slice
  8. In my widget, I have the concept of “selected” thing, and also the concept of “inactive” thing, and when the player presses the “interact” button, it sends the Interact() signal to the selected thing, if that thing is enabled.

You might be able to simplify to a single instance of Interactable that’s updated with the trace, and telling your HUD whether there’s something or nothing to interact with.

jwattes approach would be mine as well.

Your interface can have methods to deliver the text and the icon. Your actors that implement the Interactable interface can store the actual values for that in various ways. Could be simple uproperties, references to a data table or a UDataAsset. Or you could create a InteractableComponent that holds this data. That really depends on different requirements and your liking.