I’m currently trying to find a decent way to get a generic ‘interaction’ system in place. The idea is to press the ‘F’ key and depending on the object (BP) the player is close to (i.e. within the bounds of a collision volume in the BP) I would like to get a reference to the instance of the BP and do stuff with it.
I’m now a bit puzzled on how to approach this in the ‘right’ way, and which funcionality should go where (object BP, character BP, level BP, game state)?
One way would be to call a Sphere Overlap originating on the player that would give you a hit result of the object you are trying to interact with. The sphere could extend 1 meter from the player or a hundred. If you had multiple items in the sphere you could distance check them, or check if the player is facing one of them, etc.
Alternatively, if you want to get a hit under your mouse cursor a line trace might be more appropriate.
You could also Get All Actors of class, but I generally only use this if I know there is only one such blueprint in existence.
I would put the sphere overlap and the Input(F Key) in the character bp. Put whatever function is being called on the object in the object bp.
I wanted to implement generic interaction behaviour. So if the player stands close to an interactable object, he can press ‘F’ to interact with it. Now, depending on what kind of object he stands at, ‘F’ does something different.
Here’s an example:
Player stands at an equipment crate: ‘F’ opens it and puts the contents in the players’ inventory
Player stands at a phone booth: ‘F’ lets the pawn pick up the phone and do a call
…
My current solution is to track a game state variable ‘curentInteractibleObject’ which I’m populating whenever the pawn enters an interaction trigger volume. Then I do a custom event in the GameState which is triggered when the player presses ‘F’ (which is handled in the char bp). The logic in the game state now takes the object (which is now stored in the mentioned variable) and branches to specific functions based on the class type of the object.