Problem saving terrain heightmap data after change in editor

Using some c# to modify a terrain heightmap in the editor results in the heightmap changes being lost when closing the editor and re-opening.

Create new project
Add the following script to Source/ Game/

using System;
using FlaxEngine;
using FlaxEditor.CustomEditors.Elements;

using FlaxEditor.CustomEditors.Editors;
using FlaxEditor.CustomEditors;

namespace TerrainSystem
    public class Reproducer : GenericEditor
        public override DisplayStyle Style => DisplayStyle.Inline;

        public override void Initialize(LayoutElementsContainer layout)
            Terrain terrain = (Terrain)this.Values[0];

            layout.Label("Not Saved:", TextAlignment.Near);

            ButtonElement button = layout.Button("Generate Height Map", Color.Gray);
            button.Button.Clicked += () => GenerateMap(terrain);


        private void GenerateMap(Terrain terrain)
            Debug.Log("Updating heightmap");
            int heightMapSize = terrain.ChunkSize * FlaxEngine.Terrain.PatchEdgeChunksCount + 1;
            int mapSize = 509;
            int heightMapLength = heightMapSize * heightMapSize;
            float[] heightmap = new float[heightMapLength];
            Random rnd = new Random();

            for (int y = 0; y < mapSize; y++)
                for (int x = 0; x < mapSize; x++)
                    heightmap[y * mapSize + x] = ((float)rnd.NextDouble()) * 500;

            Int2 patchCoord = new Int2(0, 0);
            terrain.SetupPatchHeightMap(ref patchCoord, heightmap, null, true);


Create a new terrain, scroll to the bottom of properties and click “Generate Height Map”

You will see a random noise heightmap. Save All.

Upon re-loading the scene, the height map data is lost. There also seem to be some robustness issues with the terrain material data being lost on re-load. But the loss of height map data is apparent regardless of the terrain material being set to Default Terrain Material or left blank.

After a while of doing this data can be seen accumulating in Content/ SceneData/ Scene/ Terrain/ as previously generated heightmap data.

In case anyone else has this problem…

I erroneously pasted the line straight in from the dynamic terrain example:
terrain.SetupPatchHeightMap(ref patchCoord, heightmap, null, true);

The true at the end is for forceUseVirtualStorage=true which you should not use if you want to save the terrain data from the editor.

Just using:
terrain.SetupPatchHeightMap(ref patchCoord, heightmap)

resolves the problem. Thanks to mafiesto for finding my error (Terrain.SetupPatchHeightMap changes in editor are not saved to the scene. · Issue #1532 · FlaxEngine/FlaxEngine · GitHub)

1 Like