Landscape Heightmap in C++

I’m really new to programming in C++ in UE4, and all I want to do is bring in the landscape heightmap and read it programmatically to then place collision boxes on it at equal distances (in like a grid). Everything else I need to do at the moment can be done via the editor using blueprints etc.

So I’ve spent the last two days looking through the code, blowing my mind up with its complexity and lack of real documentation for landscapes. So far I have the following code to bring in the heightmap - just some slightly altered lines from the methods SplitHeightMap() in LandscapeEdMode.cpp and ExportHeightmap() in LandscapeEdit.cpp:

#include "LandscapeCollision.h"
#include "LandscapeEdit.h"
#include "LandscapeInfo.h"
#include "Landscape.h"
#include "EngineUtils.h"

void ALandscapeCollision::importHeightMap()
	int32 MinX = MAX_int32;
	int32 MinY = MAX_int32;
	int32 MaxX = -MAX_int32;
	int32 MaxY = -MAX_int32;
	UWorld* World = GetWorld();
	LANDSCAPE_API ULandscapeInfo* landscapeInfo;

	for (TActorIterator<ALandscape> ALandscapeItr(World); ALandscapeItr; ++ALandscapeItr)
		landscapeInfo = ALandscapeItr->GetLandscapeInfo();

		FLandscapeEditDataInterface landscape(landscapeInfo);

		TArray<uint16> HeightData;
		HeightData.AddZeroed((MaxX - MinX + 1) * (MaxY - MinY + 1));
		landscape.GetHeightDataFast(MinX, MinY, MaxX, MaxY, HeightData.GetData(), 0);

		this->heightMap = HeightData;


I’m really aware my code is far from perfect at this point.

Anyway, so far I think my main issue is correctly referencing to API_LANDSCAPE in certain places. My Build.cs file has:

		PublicDependencyModuleNames.AddRange(new string[] {
            "InputCore" });

		PrivateDependencyModuleNames.AddRange(new string[] {
            "Landscape" });

And the errors I’m coming into are along the lines of:

CompilerResultsLog: Error: LandscapeCollision.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) class ULandscapeInfo * landscapeInfo" (__imp_?landscapeInfo@@3PEAVULandscapeInfo@@EA)

and depending on if I place LANDSCAPE_API on various lines:

CompilerResultsLog: Error C2491: 'landscape': definition of dllimport data not allowed
CompilerResultsLog: Error C2205: 'landscape': cannot initialize extern variables with block scope

Can anyone give me any insight into what I can do to get this code to work? Thanks so much.

Hello MowLiao, did you figure this out? I am also stuck at the same problem

Sadly not. I abandoned this attempt shortly after seeing no replies because I had a tight deadline, and this was for a side project. Good luck with your implementation!

thank you!!!