Download

Question about spawning actors using C++

Hi guys!

I have a question about spawning actors using C++.
Here is my code:

Header:

//Spawner Stuff
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Testing”);
TSubclassOf<AActor> RHC_Spawn;

UFUNCTION(BlueprintCallable, Category = “Spawn_Function”)
void SpawnRHConstraint();


CPP:

void ASpawner::SpawnRHConstraint()
{
FActorSpawnParameters SpawnParams;
AActor* SpawnedActorRef = GetWorld()->SpawnActor<AActor>(RHC_Spawn, FVector(0.0f, 0.0f, 0.0f), FRotator(0.0f, 0.0f, 0.0f), SpawnParams); //Location, Rotation

}

Now, this code works but there is one thing I do not like. For it to work, I have to go in to my Blueprint editor and set RHC_Spawn to the class that I want to spawn. Check the picture.

Is there a way to do this assignment in C++. I have searched and searched and searched and have not found an answer on this. Using the editor to do the assignment is not the end of the world but if it is possible I do prefer C++. Is there a way?

Thank you in advance.

You can use this way:



const classPath ActorPath(TEXT("/Game/Blueprints/BP_Actor.BP_Actor_C"));
const UClass* ActorClass = ActorPath.TryLoadClass();


However, it should be noted that this method is not safe for the final build of the game. I myself have not encountered this problem, but to avoid losing the link, it is recommended to use manual pointing to the asset as in your original method.

Yep, this did the trick, and yes, I ran into the link issue you mentioned but still good to know how to do it. Thnx!