How to reference an object from level to c++ code

The code you posted in quastion is actully better, just having function that triggers the attack on enemy you pass is good start. You could have different function elsewhere that triggers the attack, potentially even in blueprint.

The 2nd code have major mistakes, first of all you don’t need to bind overlap event, you can check for overlapping actors with single function call

2nd attempt to do delay, most of the enigne gameplay code runs on single thread and this includes your code and events don’t work magically in parallel, they need to be triggered somewhere in the engine as on every frame actor is checked if it has overlaps (if overlap events are enabled in actor). Looping code for specific amount of time will just lock the thread and engine code won’t be executed at all as you locked the thread that executes it, you kind of taking it as a prisinor. Engine won’t even be able to trigger events that you waiting for as you lock thread that should trigger them.

In other words code is like a rail, you loop the train in tracks it never gonna reach the train stations it need to go to. Note that this also include blueprints, but VM have anti thread lock up system, it detects infinite, super long loops and forcefully break it when detect it to prevent enigne lock up.

If you want to have delay in C++ use timers

Or count time yourself in tick event (count up the delta time)

Now on topic at hand, instead of asking how to get reference, you should ask yourself how this actor will be choosen, what is the condition, in what event will determent it. Try to determent that in C++… and reference should be there, so for example, you want to make attack things that currently overlapping something, so you check what is overlapping. You want attack enemy that is clicked, the use on click event on enemy and tell player character to attack it. Using GetAllActorsOfClass and actor iterators should be your last resort and people who using it commonly are once you didn’t grasp utilities that UE4 APIs provide, UE4 provide many ways to detect actors and many events that can detect actor actions and most of them return actor reference of causer.