I am currently working on a project and I need to be able to spawn in bushes which are foliage types in a similar way to the way I spawn in actors. Basically I would like someone to either point me to the file where the foliage painting tool actually spawns foliage type objects into the world or better still provide me with a better idea of how I can achieve what I stated above.
TActorIterator<AInstancedFoliageActor> foliageIterator(GetWorld());
AInstancedFoliageActor* foliageActor = *foliageIterator;
//if you already have foliage in your level, you just need to get the right component, one is created for each type
TArray<UInstancedStaticMeshComponent*> components;
foliageActor->GetComponents<UInstancedStaticMeshComponent>(components);
UInstancedStaticMeshComponent* meshComponent = components[0];
//Now you just need to add instances to component, this example crate 400 instances
FTransform transform = FTransform();
for (int32 x = 1; x < 20; x++)
{
for (int32 y = 1; y < 20; y++)
{
transform.SetLocation(FVector(1000.f * x, 1000.f * y, 0.f));
meshComponent->AddInstance(transform);
}
}
Also, If you need to create the instanced static mesh component too, this is my code that i use
Note2: one also needs to add Foliage to the PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Foliage"});