Jump to content

UI_FloatRange.minValue and maxValue acting like they're static?


Recommended Posts

As far as I can tell, it seems to be impossible to make two different instances of the same PartModule class end up having two different min/max ranges for the same UI slider on the right-click context menu.

It's acting for all the world as if all instances of the PartModule must share the same min/max values and whichever instance happens to have altered them most recently, those values get used from then on by all the other instances too.  (in other words, it's behaving exactly like static member fields on a class would).

Here's an example of how I'm trying to dynamically change the min/max range values at runtime per-instance:  It does this by reading the values from the part.cfg file (so instances of this PartModule for different parts can have different settings for the range allowed on the slider), and applying the value to the slider, like so:

 

            BaseField field;

            DebugMsg("LaserDist is trying to config GUI panel fields from settings:");
            DebugMsg(String.Format("Part name = {0}, MaxBendX = {1}, MaxBendY = {2}", part.name, MaxBendX, MaxBendY));
            
            field = Fields["BendX"];
            ((UI_FloatRange)field.uiControlEditor).minValue = -MaxBendX;
            ((UI_FloatRange)field.uiControlEditor).maxValue = MaxBendX;
            if( MaxBendX == 0f )
            {   field.guiActive = false;
                field.guiActiveEditor = false;
            }
            else
            {   field.guiActive = true;
                field.guiActiveEditor = true;
            }
            

I verified that it is indeed seeing a different value for MaxBendX for two different parts, and is indeed running the code above two different times once for each part, and that it is indeed storing two different values for the *other* properties of the field "BendX" just fine - i.e. the guiActive and guiActiveEditor boolean flags *are* indeed behaving differently for the two different instances.  If I set one of them to have a MaxBendX of 0 and the other to have a MaxBendX of 15, then it *does* hide the one with a value of 0 just like the above code shows it trying to do.

But when I try to make them both have a nonzero value for MaxBendX, but a different nonzero value from each other, I just can't seem to force them to have different settings for max and min.  If I set one partA's MaxBendX to 15 in the part.cfg file, and set partB's MaxBendX to 20 in its part.cfg file, then either *both* end up having ranges from -15 to +15 on their sliders, or *both* end up having ranges from -20 to +20 on their sliders.  Which one I end up with seems to depend on which part KSP loads last.  I proved this by changing the names of the parts to make them get sorted differently, and thus get KSP to load them in the opposite order.  If partA loads last, then both partA and partB have a range of -15 to +15.  If partB loads last, then both partA and PartB have a range of -20 to +20.

I can't get partA to go from -15 to +15 and make partB go from -20 to +20.

Is there a way for me to break out of this static-like behavior of the minValue and MaxValue so I can make them differ or am I just stuck?

 

Link to comment
Share on other sites

Hmmm I'm not seeing this behaviour in a simple test case. This works as expected:

class FloatSliderActingLikeMaxValueStatic : PartModule
{
    [KSPField(guiName = "Test Field", isPersistant = true, guiActive = true, guiActiveEditor = true), UI_FloatRange(stepIncrement = 0.5f, maxValue = 100f, minValue = 0f)]
    public float someField = 100f;

    public override void OnStart(StartState state)
    {
        base.OnStart(state);
        if (part.partInfo.name == "mk1pod")
        {
            Log.Warning("Setting values for pod");

            ((UI_FloatRange) Fields["someField"].uiControlEditor).minValue = 20f;
            ((UI_FloatRange)Fields["someField"].uiControlEditor).maxValue = 220f;
        }
        else
        {
            Log.Warning("Setting values for can");

            ((UI_FloatRange)Fields["someField"].uiControlEditor).minValue = 10f;
            ((UI_FloatRange)Fields["someField"].uiControlEditor).maxValue = 110f;
        }
    }
}


[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
class FloatSliderTestInstaller : MonoBehaviour
{
    private void Start()
    {
        DontDestroyOnLoad(this);

        var pod = PartLoader.getPartInfoByName("mk1pod").partPrefab;
        var can = PartLoader.getPartInfoByName("landerCabinSmall").partPrefab;
            
        pod.gameObject.AddComponent<FloatSliderActingLikeMaxValueStatic>();
        can.gameObject.AddComponent<FloatSliderActingLikeMaxValueStatic>();
    }
}

Is there any more code in there? The only gotcha I can think of is that having the UI_Control affect sym counterparts could possibly overwrite minValue and maxValue (didn't check) but you mentioned two distinct parts so I wouldn't think that was your problem

Link to comment
Share on other sites

  • 3 years later...

4 Years later, and another one bites the dust. :)

I'm facing this exact problem - but I had found that what's happening is the prefab data being shoved back regularly on the ui_control.

By changing the value using Reflection, I managed to get the new Min and Max values working for some time - but as soon as you change scenario, the values are reset again and, curiously, they become imune to the new changes from the code (i.e., trying to restore the data using OnStart, OnCopy or even Update is useless).

Curiously, this appears to do not affect the Resources controls - i.e., I can mangle the Min and Max values for a Resource control.

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...