A) only including #include "Runtime/Foliage/Public/InstancedFoliageActor.h" creates about 40 errors (mostly syntax and Error E:\Unreal\Epic Games\4.13\Engine\Source\Runtime/Foliage/Public/InstancedFoliageActor.h(11) : fatal error C1083: Cannot open include file: 'InstancedFoliageActor.generated.h': No such file or directory
B) I have managed to re-create it in Blueprint, but the performance is very poor (can render about 600 instances, but using Foliage paint tool about 30k)
Can anyone please give some code/advice how to properly spawn foliage in c++? It would be great to have same culling setting as in Foliage paint, since setting “Set Max Draw Distance” does not have the nice fade in and out animation and using “Set Cull Distances” instead of “Set Max Draw Distance” did absolutely nothing.
The problem was that I forgot to mention this Note:
you need add Foliage to the PublicDependencyModuleNames.AddRange (new string [] { “Core”, “CoreUObject”, “Engine”, “InputCore”, “Foliage”} ); in YourProjec.Build.cs File.
So i made it working but I have problems saving and further adjusting the foliage.
Say i have some datatables that i want to use to populate the foliagetool. The foliage is not saved except when i add at least one instance via the foliage tool after running the c++ code; then it is somehow marked dirty i assume and it can be saved without being discarded when loading a different level or reopening the project. But I can never access the foliage again with the foliage tool as it is invisible to it, only via code.
AInstancedFoliageActor* FoliageActor = AInstancedFoliageActor::GetInstancedFoliageActorForCurrentLevel(World, false);
if (FoliageActor != nullptr)
{
FFoliageInfo* FoliageInfo = FoliageActor->FindInfo(FoliageType);
if (FoliageInfo != nullptr)
{
FFoliageInstance NewFoliageInstance = FFoliageInstance();
/*
* From FFoliageInstancePlacementInfo
* Set your desired transform here.
*/
NewFoliageInstance.Location = FVector(0.0f, 0.0f, 500.0f);
NewFoliageInstance.Location = FVector(0.0f, 90.0f, 0.0f);
FoliageInfo->AddInstance(FoliageActor, FoliageType, NewFoliageInstance);
}
}
}
Found a solution. Better use the ‘AddInstance’ function to properly add it to your desired FoliageType. I think you have to trigger some notify function after adding all instances but when you use this code the instances are correctly saved and when you restart the foliagemode you have the updated instance count and you can also edit your placed instances with the foliagetool.