Download

Landscape SetHeightmapData doesn't work and crash

Hi,

I try to change the landscape heights at runtime but it doesn’t work.
When I execute the function below, LandscapeEditorUtils::SetHeightmapData return true but the landscape doesn’t change. When I execute it a second time, it crashes.
If I execute the function from BeginPlay it crashes immediately.

Is it possible to change a landscape with c++ (at least before the game start) ?

	ULandscapeInfo::RecreateLandscapeInfo(GetWorld(), 1);
	TArray<ALandscape*> landscapes = GetLandscapes();
	if (landscapes.Num() != 1)
		return;

	ALandscape* landscape = landscapes[0];
	FIntRect ComponentsRect = landscape->GetBoundingRect() + landscape->LandscapeSectionOffset;
	int numHeights = (1 + ComponentsRect.Width()) * (1 + ComponentsRect.Height());

	TArray<uint16> Data;
	Data.Init(0, numHeights);
	for (int i = 0; i < Data.Num(); i++) {
		Data[i]= FMath::RandRange(1000, 32768);
	}
	bool res = LandscapeEditorUtils::SetHeightmapData(landscape, Data);
	if (res)
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Green, TEXT("SetHeightmapData success"));
	else
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, TEXT("SetHeightmapData error"));
	ULandscapeInfo::RecreateLandscapeInfo(GetWorld(), 1);