How should I call the the UArrowComponent* ProjectileSpawnPoint in SpawnActor with c++ code?

As the picture show above , I have already perform the SpawnActor with projectile , but I have not idea how to get projectile Spawn Point(already inherit in c++), How should I do?

it would be spawn loation FVector as I see. ??? also if you need the reference of the actor, remember to save the return value of the actor when spawned.

How exactly would you save the return value of the actor that is being spawned? My code looks very similar to spark009’s, but I need a way to save the return value.

You just need a variable of AActor type. So you can declare it in the function that you are already in:


AActor* MyActorRef = World->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);

Problem with doing it this way is that variable will only exist inside that function (Fire) once you leave that function that variable gets destroyed.
Or you can declare an actor in your .h file so it’s available throughout the entire class the code is nearly the same:


MyActorRef = World->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);

And you should verify that it worked:

Thank you so much !

Yeah I realized what to do a bit later. I just made the mistake of having the actor reference on the wrong side of the “equation”.

It’s odd though, I’m not able to access any functions. My only options are “execFUNCTIONNAME()” for each function I try to access. Any ideas of what I should do? I was thinking Delegates/events, but I’m not quite sure.

If you want to access functions you wrote of your projectile class, you just cast the AActor to your projectile actor. Spawn actor can return different class types.

So change your .h AActor* MyActorRef into your custom projectile AProjectile (if that is what its called). You will need to do a forward class declaration in your .h file if it doesnt understand AProjectile.

then in your Fire function code you do this:



//MyActorRef is now MyProjectileRef and is no longer an AActor* but now an AProjectile*
//You will need to include the AProjectile .h file at the top of this .cpp file for functions to be exposed
MyProjectileRef = World->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);

//Check cast & our pointer is valid
if (MyProjectileRef) {
    UE_LOG(LogTemp, Warning, TEXT("Actor successfully spawned casted to AProjectile"));

    //Call your functions
    MyProjectileRef->DoSomethingCool();
}