This is the type of cases that would benefit from Blueprint Interfaces or Event Dispatchers. The blueprints don’t need to know about each other’s existance or cast to anything.
-
Create a Blueprint Interface and name it something like
BPI_Interact
. -
Open the created interface and on the top right side create a function. Name it something like
Interact
. You don’t need to do anything else. Save and close theBPI_Interact
. -
Open your character blueprint and on the Class Settings, go to the Details panel and scroll down to the Interfaces section. Under the Implemented Interfaces click the
Add
button and selectBPI_Interact
. -
Compile and save. Now open your
BP_Trigger
and do the same, and in yourBP_Light
also. -
Open your
BP_Trigger
and create an array of the type Actor. Make it Instance Editable and Expose on Spawn. This will allow you to select variousBP_Light
actors from the world Outliner or the Viewport directly, so one Trigger will be able to activate as many lights as you want, or any Actor, and as long as they implement the interface you can have multiple actors doing something at the same time. -
Now, on your
ActorBeginOverlap
event, from the Other Actor pin drag and getDoes Class Implement Interface
node and promote the boolean to a variable. -
On your
ActorEndOverlap
, just set that variable to false. -
Given that all of them implement the interface, in your
BP_Trigger
under your Functions section, should appear an Interfaces section. You will see the function you just created on your Interface. Right click on it and select Implement Event. It will create a new event and you will notice a small arrowed blueprint icon on the top right of the event node. This means it’s an Interface event.
-
Now drag your Actor type array and into a for each loop call the function
Interact
on theBPI Interact
section and connect your looped Actor to theTarget
pin on theInteract
function. Use a Branch with the boolean you promoted earlier to know if it’s Overlapping, therefore, it can interact or not.
Make sure you select the one under BPI Interact. If you select the one under Call Function, will just call the one implemented inside the current blueprint.
Your trigger is ready now. -
Open your
BP_Light
and implement theInteract
Interface Event. Now it’s up to you what the actions will be. You could use a boolean to keep track of the status of the light, or a FlipFlop, or whatever method you decide. -
Open your character blueprint.
-
In your
OnActorBeginOverlap
get the Other Actor and promote it to a variable. -
in your
OnActorEndOverlap
just set that variable to None/Empty. -
From your
Key F Event
, add anIsValid?
node and connect the Actor variable you created before, and from theTrue
pin just call the Interface Event underBPI Interact
and connect the Actor variable to theTarget
pin on your Interact function. If there’s a valid Actor that implements the BPI, it will send the message, and if there’s no Actor stored in the variable, it will do nothing if you press the action button, or you could use the action button to do something else if there’s no valid actor for your interaction, i.e. press it to reload a weapon or heal the character if it’s not inside the trigger area for the interactive Actor/s. TheIsValid?
node helps you avoid undesired behaviours and crashes if there’s a Null Pointer (None/Empty).
That’s all you need. You don’t need to Cast, or hard reference anything.