Spawn Blueprint C++

Hello everyone,

I’m so confused about how to spawn blueprint from C++. I researched it but I couldn’t. I want to make select weapon system. Player will be able to select desired weapon and will play the game. How can I make this? Btw, is Child Actor Component for this problem?

The question doesn’t make much sense as C++ and blueprints are a way of specifying functionality.

What you are asking is how to spawn an actor and it doesn’t matter to the spawner if the spawned actor is implemented in C++, blueprint or a mix of the two.

You probably want to look at Actor Component which is a method of attaching one actor (weapon) to another (the player).

I think you are looking for something like this:

NOTE: You must have a base class from C++ that your blueprint children are based on for this feature.

.h File

.cpp File

For a detailed breakdown:

  • The UPROPERTY meta tags will expose the drop-down menu and accessibility from the Blueprint and will constrain valid classes to only those that derive from YourBaseClassFromC++.
  • TSubclassOf<> is the special data type that gets class values.
  • GetWorld()->SpawnActor() is the function usually used for spawning any actor.
  • The key part: SpawnableClass->GetAuthoritativeClass() will attempt to get the most-derived valid class of the SpawnableClass value passed in (i.e., will reference a Blueprint class if it was used as the value).

4.24 it Work. Good Luck

UObject* SpawnActor = Cast<UObject>(StaticLoadObject(UObject::StaticClass(), NULL, TEXT("/Game/DEXIED/Foliage/Tree/BP_TreeDestroyed_Style_1.BP_TreeDestroyed_Style_1")));

UBlueprint* GeneratedBP = Cast<UBlueprint>(SpawnActor);
if (!SpawnActor)
{
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("CANT FIND OBJECT TO SPAWN")));
return;
}

UClass* SpawnClass = SpawnActor->StaticClass();
if (SpawnClass == NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("CLASS == NULL")));
return;
}

UWorld* World = GetWorld();
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
World->SpawnActor<AActor>(GeneratedBP->GeneratedClass, GetActorLocation(), GetActorRotation(), SpawnParams);

2 Likes

Thanks man!

I worked fine for me, but i still have a problem: my spawning actor has a variable who is exposed to the spawner, but i don’t know how define it, can you help?
needtodothis.PNG
Something like this.