I have 3 different classes (NPCs, Pickup items, interactable objects), and they don’t share class parentage, but player interacts with them the same way: mouse hovering outlines the interactable actor (turns the outline mesh component visible), shows up their name (turns the title in the interaction widget component visible), clicking on them makes the player move close to them, getting in range opens the interaction options in the interaction widget component:
the code for this interaction within the 3 different actors is >90% identical, and I wanted to reuse it so I don’t have to change it 3 times (or more if I add more interactable classes later).
However, an actor component didn’t work, as interactions are handled mostly via interface, and they control visibility of different components (outline mesh and interaction widget) among some other things. So since the actor being interacted with receive commands from the player controller via interface functions, I’d have to pass every interface function call from the actor to custom events in the component, and that doesn’t save as much work as I’d like. Also, components can’t have meshes or widgets so the actors would still need to have this on their own, and the component would have to communicate with the parent’s components somehow.
this is am examples of the current interactions via interfaces, they are present in all 3 classes:
its possible with an Actor Component just hard to say if its worth it without knowing exactly what you may plan in the future.
you can use GetComponentByClass instead of Interface
the component can communicate with the parent via Event Dispatchers or you can initialize the component in the parent and send through what it needs (widget, outline)
or again depending what you plan in the future you can put most/all of the code in a function library
An actor component would be ideal I believe, but currently I’m interacting with stuff under mouse cursor, so I send interface messages to whatever actor / component I click if it implements the interface.
How do I get the actual actor of the component hit to send interface messages?
So, there are only three classes that don’t share parentage I need to interact with: Fixed interactable objects (such as doors, wardrobe, etc), Dynamic pickup objects (such as weapons, medkit, etc), and NPCs.
Interaction for those three is the same: outline and show collapsed interaction widget on mouse over, move player to on mouse click, show expanded widget when player overlaps the range check mesh, and route widget functions back to the class actor when clicking an option on the interaction widget (a maximum of 4 options).
I suppose it is worth it? it’s a bunch of code repeated over the 3 different classes. my outline method is based on Tom Looman’s material outline, a cube that encompasses the actor with an outline material instead of full screen post process, so the interaction component can have the outline cube and the interaction widget in it, and I just need to pass a reference to the main mesh from the class actor to the interaction component, along with its name and interaction options