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

Hello community,

I am working on my 2D game project and everything is just fine, except for one thing.

In most of the games, you can attack, so I decided to create my own attack function in c++, but unfortunately, I can’t get the reference to my enemies, that I placed in the level.

/* I created AEnemy class and made a blueprint, and placed some enemies in UE4level editor */

I know that there is a utility called “ConstructorHelpers::FObjectFinder” - it didn’t work in this situation.

I also tried “TActorIterator<.AEnemy>” to iterate through all the enemy classes - it also didn’t work
Every time I got just nullptr

This is my simple function, just in case if someone will have other questions

Everything that I want to know is how to access those AEnemy actors in the level via c++ code, that I can work with them.

Thanks for response

1 Like

If there is only one enemy in the entire level you could just do GetAllActorOfClass() and save the enemy reference in your Enemy pointer and then apply damage.

But if there are multi spawns of the same pawn i would rather use ApplyDamage. Like from the weapon/Projectile that is colliding use an overlap to determine the enemy and apply the damage

Thank you, the AEnemy is just a template. In the level, there are several enemies.
I have no weapon or projectile, I just wanted to detect a collision, if the player and enemy collided and simply subtract healths.

Then your player must have an Collision like Collision Capsule in third person template.
On the player OnComponentBeginOverlap check if the overlapping thing is EnemyAI. You get the enemy reference in OtherActor. Check the docs for it. Let that then subtract health accordingly

Yes, that works OK in blueprint but not in a code.

My player has a capsule collision component, so I did the OnComponentBeginOverlap.AddDynamic() in c++ and create additional function BeginOverlap() where I Cast OtherActor to my enemy, but it is nullptr after all

Anyways thank for the response

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.