Press and hold interactions with aim, nearby targets, and server requests for Unreal Engine.
Press and hold interactions with aim, nearby targets, and server requests for Unreal Engine.
NsInteraction lets an actor, usually the player, use other actors. A sensor gives the targets. The interaction component asks each target for its option, keeps the best one, and runs it on a press or at the end of a hold. A ready-made interactable actor covers the simple case with no code.
NsInteraction has no UI and no input binding. Your game shows the prompt and calls the component from its input. NsInteraction depends on no other plugin.
INsInteractable in C++ or Blueprint. Return an option, then run it.ANsInteractableActor, set its option in the Details panel, and handle On Interact.ScoreOption to change which option wins.Unreal Engine 5.7+
NsInteraction folder into the Plugins folder of your project."NsInteraction" to the dependencies of your game module in Build.cs.NsInteractableActor. Add a static mesh to it.On Interact event, and place the actor in the level.Begin Interact when the input starts and End Interact when it ends.On Option Changed to show the prompt.In C++, implement the interface on any actor:
// MyLever.h
#include "GameFramework/Actor.h"
#include "NsInteractable.h"
#include "MyLever.generated.h"
UCLASS()
class AMyLever : public AActor, public INsInteractable
{
GENERATED_BODY()
public:
virtual bool GetInteractionOption_Implementation(AActor* Interactor, UPrimitiveComponent* Part, FNsInteractionOption& OutOption) override
{
OutOption.Prompt = NSLOCTEXT("MyGame", "PullLever", "Pull the lever");
return true;
}
virtual bool ExecuteInteraction_Implementation(AActor* Interactor, const FNsInteractionOption& Option) override
{
bPulled = !bPulled;
return true;
}
bool bPulled = false;
};
| Sensor | Setup |
|---|---|
| Aim | Add a UNsInteractionViewSensor. It uses the first camera of the owner, or the component from SetViewSource |
| Nearby | Add a sphere or box collision. Pass its overlap events to HandleSensorBeginOverlap and HandleSensorEndOverlap |
| Game code | Call AddTarget, RemoveTarget, or SetTargets |
INsInteractable – The interface of a usable actor: GetInteractionOption, ExecuteInteraction, OnInteractorEnter, and OnInteractorExit.ANsInteractableActor – An actor with one option from the Details panel. It fires On Interact and OnInteracted.FNsInteractionOption – The target, the action name, the texts, bEnabled, press or hold, the authority, and the priority.UNsInteractionComponent::BeginInteract / EndInteract – Start and end the input.UNsInteractionComponent::OnOptionChanged – Fires when the shown prompt changes.UNsInteractionComponent::OnServerRequest / ExecuteOnServer – Fires for a server option on a client, and runs a request on the server.UNsInteractionComponent::ScoreOption – Override it to change which option wins.UNsInteractionViewSensor::IsPartInView – Check a client aim on the server.