Placing Procedural Foliage Spawner Programmatically using C++

I have tried to spawn a custom made Procedural Foliage Spawner using C++ using SpawnActor. There I have found SpawnActor cannot be used since ProceduralFoliageSpawner is a UObject.

Then I was able to spawn a AProceduralFOliageVolume, which is an AVolume (which is an AActor) using SpawnActor in Unreal Viewport using the following code.And it worked.

    AProceduralFoliageVolume* Foliage = World->SpawnActor<AProceduralFoliageVolume>(SpawnLocation, Rotator, SpawnParams);

But when I spawn the Procedural Foliage Volume, it doesn’t have the bounding box and does not show one even if I select from Brush Shape. I have also selected the Foliage spawner for the volume and set scaling to the volume.

When I try to call a class to an already created spawner to be included in the Foliage Volume it doesn’t call the class corectly.

  auto FoliageActor = LoadClass<UProceduralFoliageSpawner>(nullptr, TEXT("/Game/Assets/Foliage/FoliageSpawner.FoliageSpawner_C"));

Can I know a way I can spawn a foliage spawner programmatically using C++?

Thanks in Advance

Hello! SpawnActor can be used only with Actors (Naming convention - their names begin with A). UObjects (Naming convention - their names begin with U) can be created with this

auto spawner = NewObject<UProceduralFoliageSpawner>(outer, name);

In fact you can create it just before usage and then destroy (nullify all pointers) if spawning activity is just one moment thing. But also you can keep it in some UProp if want it to be valid for long time

Thank you very much. But when I try to spawn in in the world programmatically I do not get the bound-box (Even selecting Box brush after spawning). Therefore I tried to find the bounds and printing them and they both are 0.

 float a = Bounds.Min.X;
float b = Bounds.Max.X;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("size: %f "),a),false);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("size: %f "), b), false);

Hence I’m looking for a way to spawn the Volume with bounds similar to when we drag and drop from place actors.

Hello! Actor bounds in fact are calculated by components bounds and are collisions dependent. So they are not connected with Brush size…

Hi @Kehel18 , Thank you. I looked into component bounds and couldn’t find a setter but there was just a getter. Then I tried to access Level Bounds class and found this comment in …/Editor/LevelBounds.h

/**
 *
 * Defines level bounds
 * Updates bounding box automatically based on actors transformation changes or holds fixed user defined bounding box
 * Uses only actors where AActor::IsLevelBoundsRelevant() == true
 */

Further finding AProceduralFoliageVolume doesn’t have it defined.

AVolume, which is the super class for AProceduralFoliageVolume has the levelbounds set to false. Yet the procedural volume in Unreal editor does have bounds.
I was wondering how I can set the level bounds programmatically as it’s visible in the Procedural Foliage Volume similar to that we use in editor mode.

Was successfully done using following code

	VectorRegister	Rotation{ 0,0,0 };
	VectorRegister	Translation{ 254020.0,259420.0,5737.462891 };
	VectorRegister Scale3D{ 3000,3000,400 };
	EObjectFlags InObjectFlags = RF_Transactional;
	FName InName = NAME_None;
	UWorld* World = GEditor->GetEditorWorldContext().World();
	ULevel* Level = World->GetCurrentLevel();
	FTransform Transform{ Rotation,Translation,Scale3D };

	UActorFactory* ActorFactory = GEditor->FindActorFactoryForActorClass(AProceduralFoliageVolume::StaticClass());
	auto Foliage = ActorFactory->CreateActor(AProceduralFoliageVolume::StaticClass(), Level, Transform, InObjectFlags, InName);