How can I instantiate an Actor before Spawning in C++?

ACodeTSSGameMode::ACodeTSSGameMode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
// set default pawn class to our character class
DefaultPawnClass = ACodeTSSPawn::StaticClass();
AEnemy Enemy = AEnemy(ObjectInitializer);
}

This is what I am trying to do basically.

I created an Enemy class with a Target property of type APawn. I want to instantiate the Enemy class, set the Target to the player’s pawn, and then spawn the Enemy. Here is basically what I want:
//create an enemy instance
AEnemy Enemy = AEnemy(ObjectInitializer);

UWorld* const World = GetWorld();
if (World)
{
        //set the target pawn
        Enemy.SetTarget("Somehow get player pawn here");

        //spawn the instantiated enemy
	World->SpawnActor<AEnemy>(Enemy::StaticClass());
}

Sorry for any poor formatting.

I’m not used to C++ yet, so thats another barrier. I think I might need to look up a quick refresher course.

Try

AEnemy* Enemy;

Instead of

AEnemy Enemy = AEnemy(ObjectInitializer);

I think this should instantiate an Enemy. As you see the same was done with the default pawn class before only that its representing the PawnClass itself.

Then with Enemy->DoSomthing you can do whateveryou want with that object before spawning.

At last do World->SpawnActor(Enemy) could work.

Test that because im not 100% sure and tell if it went well or tell me the error it gives you :wink: