Jump to content

Want to add OnFieldChange event to KSPField using UI_FloatRange


Recommended Posts

I've got a KSPField that is available in the editor scene. It has some default values and it allow a player to change it via the slider in the right click menu.

[KSPField(isPersistant = true, guiActiveEditor = false, guiName = "Surface Sample Capacity"), UI_FloatRange(minValue = 0, maxValue = 50, stepIncrement = 1)]
public float maxSurfaceSampleCapacityRange = 0;

I want to run a piece of code once a player has changed that figure via the slider. I saw there is a "onFieldChanged" event under the uiControlEditor of the field I'm looking at. How do I get that event to run my code?

Callbacks confuse me at the moment, but I'm not sure it that is even what I should be looking at. Sorry in advance if it's a straightforward solution. :confused:

Edited by wile1411
Link to comment
Share on other sites

For the particular use I'm looking at now:

        private void UIEvent_onTweakableChange(BaseField field, object what)
        {
            needUpdate = true;
        }

        private void SetupUIFieldCallbacks()
        {
            for (int i = 0; i < Fields.Count; i++)
            {
                var uiField = Fields[i].uiControlEditor;
                if (uiField.GetType().FullName == "UI_FloatRange")
                {
                    uiField.onFieldChanged = UIEvent_onTweakableChange;
                }
            }
        }

Nothing difficult!

Link to comment
Share on other sites

I'm not a new coder ( I'm a very lapsed coder ) but I'm new to C#; I think the assignment should probably be

uiField.onFieldChanged += UIEvent_onTweakableChange;

which is ( seems at least ) C# specific. onFieldChanged is a MulticastDelegate, which is a construct which can execute multiple bits of code which you might want to read about ( and then read about somewhere other than MS's document site! ).

Edited by Van Disaster
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...