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.

2 Likes

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.

Sorry, I finally got back to this and used static class variables to hold the default values
E.g.

private static int sSeed = 7;

Set the editor value within :
public override void Initialize(LayoutElementsContainer layout)

E.g.
IntegerValueElement seed = layout.IntegerValue(“Seed”, “Starting seed.”);
seed.Value = sSeed;

Then when ever I did something that used that value I updated the static variable to the new value. I’m guessing there is an onChanged event for the IntegerValueElement etc. boxes that I could use to automatically update the static, which would be a little more robust, but I didn’t see it. It would also be nice to be able to save/retrieve the values from an ini file, but I did not know how to do that either.

For me, communication between custom editor and script doing the read of serializable fields/properties of script sems the easy way.

 public override void Initialize(LayoutElementsContainer layout)
 {
    var script = (MyObject) Values[0];
   ...

I not know exactly what Values being List means, but the first index works for me.