Spawning an Object C++ Only

I am trying to spawn an arrow in my game on a key press.
The following code in my .h is:

    // Projectile class to spawn
    UPROPERTY(EditAnywhere, Category = Projectile)
    TSubclassOf<class AProjectileArrow> ProjectileClass;

.cpp:

void AVR_PlayerChaperone::SpawnArrow()
{
    //spawn object here 
}

I have a C++ class of my projectile that I want to spawn. I do want to use a blueprint at all. Please help.

For that, we’ve got UWorld::SpawnActor with the following syntax:

AActor* UWorld::SpawnActor
(
    UClass*         Class,
    FName           InName,
    FVector const*  Location,
    FRotator const* Rotation,
    AActor*         Template,
    bool            bNoCollisionFail,
    bool            bRemoteOwned,
    AActor*         Owner,
    APawn*          Instigator,
    bool            bNoFail,
    ULevel*         OverrideLevel,
    bool            bDeferConstruction
)

Many of those arguments aren’t needed, but you may want to use them to control the spawning in a better way. Example for spawning your arrow:

AProjectileArrow* spawnedArrow = (AProjectileArrow*) GetWorld()->SpawnActor(AProjectileArrow::StaticClass(), NAME_None, &yourLocation);

SpawnActor just returns a AActor so we need to cast it to your arrow class using (AProjectileArrow*) in front of the term, then we get the class of it using ::StaticClass(), and yourLocation is basicially a FVector with the location you want to spawn it in. See the argument list and extend it to your needs!
Hope that helped!

Source: Spawning Actors | Unreal Engine Documentation

Hey thanks for the help. That does work however if I have the “AProjectileArrow* spawnedArrow” variable declared in AVR_PlayerChaperone header and the VS debugger says the object is nullptr even though it still spawns the arrow and lets me get variables that I’ve declared in my ProjectileArrow class. Any idea on why that is?

I never really debugged with VS, but it shouldn’t be the case that VS indicated it as a nullptr… Sorry, can’t help you there, but if my answer helped anyway, I’d be glad if you mark it as the solution :stuck_out_tongue: