Problem: Spawn blueprint actor in C++

Hi! I’ve been doing this C++ tutorial (https://docs.unrealengine.com/latest/INT/Videos/PLZlv_N0_O1gYup-gvJtMsgJqnEB_dGiM4/qZHMf2WqjSM/index.html). I have a problem to spawn proper actor that is derived from my Pickup class (Pickup class is C++ class) I have bluprint class called BatteryPickup_BP that is derived from my C++ class (APickup). I have setted static mesh component in blueprint and If I drag that bluprint to the level, the right actor is showing up and works fine. But when I try to spawn that actor in my C++ code nothing shows up. I added console log output to spawn function and I checked that it is called. I have one spawn volume that should spawn blueprint actors. I’ve set TSubclassOf<class APickup> class (in spawning volume) in editor to BatteryPickup_BP so it should spawn my blueprint actors. There are actors being spawened to level from my spawning volume but their class is APickup instead of BatteryPickup_BP. What am I doing wrong or is this suppose to be worked like this?

Here is how I define what to spawn in spawn volume actor:


UPROPERTY(EditAnywhere, Category = "Spawning")
	TSubclassOf<class APickup> WhatToSpawn;

Here is my spawn function:


void ASpawnVolume::SpawnPickup()
{
	
	if (WhatToSpawn != NULL)
	{
		
		UWorld* const World = GetWorld();
		if (World)
		{
			
			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = Instigator;

			
			FVector SpawnLocation = GetRandomPointInVolume();

			
			FRotator SpawnRotation;
			SpawnRotation.Yaw = FMath::FRand() * 360.0f;
			SpawnRotation.Pitch = FMath::FRand() * 360.0f;
			SpawnRotation.Roll = FMath::FRand() * 360.0f;

		
			APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn, SpawnLocation, SpawnRotation, SpawnParams); // <------------ Is this the right way to do it?
		
			SpawnDelay = FMath::FRandRange(SpawnDelayRangeLow, SpawnDelayRangeHigh);
			GetWorldTimerManager().SetTimer(SpawnTimer, this, &ASpawnVolume::SpawnPickup, SpawnDelay, false);
		
		}
	}

}

I spawn actor using World object’s SpawnActor function that should spawn my blueprint actor. I’ve set WhatToSpawn in editor earlier but for some reason only APickup is being spawned but not the bluprint class that is derived from APickup.

Any thoughts?

when calling World::SpawnActor it should be like this for blueprint classes, the good new that it works for both C++ and BP classes


APickup* const SpawnedPickup = World->SpawnActor<APickup>(WhatToSpawn->GetDefaultObject()->GetClass(),  SpawnLocation, SpawnRotation, SpawnParams);

just make sure GetDefaultObject() and GetClass() returns not null first.

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);

Thanks, that work for me, too. Only in Editor / Viewport. When i package my project it can’t find the Blueprint, i get “CANT FIND OBJECT TO SPAWN”.
it looks like the Blueprints missing in the package, i tried to add the folder in the package settings, but no :confused:
Do someone have any hint for me?
Working on a c++ / blueprint mix project.

NVM, i found a better solution for this:

  1. Add in .h file:
    UPROPERTY(EditDefaultsOnly, Category = “Pickups”)
    TSubclassOf<class AActor> BombPickupTypeToSpawn;

  2. Add in .cpp
    World->SpawnActor<AActor>(BombPickupTypeToSpawn, Location, GetActorRotation(), SpawnParams);

  3. Setup the Type in the Blueprint -> Class Defaults which spawn the BombPickupTypeToSpawn.