How to retain parameter values in custom editor when switching objects?

I use this code to add an additional parameter to the terrain object properties (as an example):

using FlaxEngine;

#if FLAX_EDITOR
using FlaxEditor.CustomEditors;
using FlaxEditor.CustomEditors.Editors;
using FlaxEditor.CustomEditors.Elements;


[CustomEditor(typeof(Terrain))]
public class Test_Editor : GenericEditor
{
    public override DisplayStyle Style => DisplayStyle.Inline;

    public override void Initialize(LayoutElementsContainer layout)
    {
        base.Initialize(layout);
        FloatValueElement testVar = layout.FloatValue("testVar", "lost value");
        testVar.Value = 2;
    }
}
#endif

Create a terrain and you will see the additional property at the end of the normal terrain properties.

If I change the value of testVar in the property window and then click another game object, when I come back to the terrain the testVar value reverts to its default value. How do I keep it at the updated value?

There are several ways to do it in Custom Editors:

  1. You can use CustomValueContainer with getter/setter functions that provide some static variable to be global during Editor session.

  2. You can use Editor.Instance.ProjectCache to store/read custom data to be cached by Editor for a given project (eg. via TryGetCustomData or SetCustomData - see https://github.com/FlaxEngine/FlaxEngine/blob/master/Source/Editor/CustomEditors/Dedicated/ClothEditor.cs)

  3. You can get those values from a script or actor on a level to be presented in a custom way via ValueContainer where you can specify the script member info (eg. script field reference) and outer script instance to get that value. This way might be hard for starters because it touches the core of Custom Editors data management and I would recommend reading engine sources for this solution.

1 Like

Thanks for those ideas, I’ll have a play around and see what I can get working. If anyone has some simple example code that would be helpful.