I can't figure out why my event is being ran twice

I’m building a popup widget system for my game using the Thirdperson Template and am running into a weird problem.
My setup goes as follows:
BP_Thirdperson uses a linetrace to detect whether I’m clicking on an interactable actor and displays a popup widget.
The code for the widget is in a custom “Interact” event on a “Interaction” Component.
I created a parent BP class for all interactable actors and added the Component to it.
My scene only contains 1 Child Actor with the Interaction Component.

For some reason, even though there is only 1 actor in the level with the component, the Interact event on the Component is being ran twice.

Here are some screenshots to hopefully give you an idea of what’s going on.
This is the part where Thirdperson BP does the line trace


Here it Casts to the Component and if it is valid, tries to run its Interact event:

And here is the Interact event on the Component, for debugging purposes I replaced it with just a print string:

As you see, there is only one actor in the level, BPChild_item, which contains the Component that has the Interact Event. The print string that says “click” is immediately before the Event gets called, and there is only 1 “click” printed, but then 2 “Hellos” gets printed, which seems to suggest the event is called twice. How is this happening?

You have the component connected to the interact node two times, so it will run it twice
image

By the way using variables from the for loop outside of the loop body is not good practice and can lead to weird issues that will be hard to track
Essentially it will use the variables from the last loop, but if that’s your intention, it’s more readable to just get the last valid element in the array and act on it instead - Use Get(Array->LastvaldiIndex)
But in both cases you probably have to check if the array actually had elements

Also, since you’re casting to the object type anyway, assuming the blueprint class has the interactable component, it’s faster to simply get it from the cast instead of finding it. Also if the blueprint has it, it’s safe to assume it will be valid if the cast succeeds

Hi, thank you for the swift response, and for the very helpful info.
It works just as you describe, and I learned a few things.
Cheers!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.