I’m relatively new to C++ and to Unreal and am trying to get my head around the best design for some key functionality in my project - any advice would be much appreciated!
Context is that I am trying to design my ‘interactables’ functionality.
At it’s base this is functionality for objects in the game world which the player, and potentially NPCs, can interact with. Effectively it boils down to:
When the player hovers the cursor over an interactable object it should highlight, and when they press the ‘interact’ key whilst hovering over the object it should check the player is in range and then trigger it’s relevant functionality.
To tackle this, initially I implemented an interface in C++ with such functions as OnMouseOver, MouseOverEnd, OnInteract. Then, I included this interface on the relevant top level classes such as DoorActorBase, ButtonActorBase, etc. so it’s available to any derived classes.
The problem I had with this is that I was then having to implement the common functionality, such as the highlighting of the object, in each top level class.
I have a feeling this isn’t the best way to do this as it means if I want to change say the default highlight colour, width, etc. (or even more fundamentally the code behind the highlight) I then need to go into each top level class to make the change(s) to the actual functionality.
Therefore after some research I’ve started to attempt to create a C++ Actor Component instead. My understanding is that then the relevant logic such as for highlighting the object can be stored in the ActorComponent, and the actual object ‘functionality’ can be stored in the Actor class. So the component takes care of the highlight and any checks and can then trigger the ‘functionality’ on the owner Actor.
I’m running into some confusion though on how to best approach this, so am hoping for some guidance from much more experienced developers! (Basically anyone )
Firstly, is my thinking on the ‘best’ approach correct, or misguided? If it is on the right path then what’s the best way to structure this? Would it involve a mix of an interface which checks the Actor has the InteractableComponent on it, and then calls the relevant function on the InteractableComponent? If so, I’m really confused on how to actually include code in UE C++ interfaces, rather than just function declarations?! Or is some other way a better approach?
P.s. If it’s helpful, as an end goal ideally what I believe I want is for default interaction functionality (as set in a single source of truth) to be available to any relevant Actors I choose (as an example of ‘default functionality’: OnMouseOver should trigger a highlight with the default settings, MouseOverEnd should remove the highlight, OnInteract would check player is in range and then call TriggerFunctionality). However ideally each of these can be overridden as required on a per class basis too.