Jump to content

[SOLVED] UI_Toggle boolean flipping


Recommended Posts

Can someone else confirm that I haven't lost my mind...

In the partmodule declarations...

        [KSPField(guiName = "myToggleButton", guiActiveEditor = true, guiActive = true, isPersistant = true),
        UI_Toggle(controlEnabled = true, disabledText = "Off", enabledText = "On", invertButton = false, scene = UI_Scene.All)]
        public bool myToggle = false;

In the FixedUpdate

            FASLog.Writelog("OnUpdate " + myToggle);

The logged output reads all False until I click the toggle and then... I get this

8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate False
8/14/2018 4:13:58 PM: OnUpdate True

I have searched all of my code and those are the only two places where that variable appears.  I have ripped out the declaration, changed the variable name and smashed a few chairs trying to figure out WHY it's doing this.

Thanks

Edited by Fengist
Link to comment
Share on other sites

1 minute ago, HebaruSan said:

My first guess would be your craft has three parts, or a part has three of your modules.

ya know what....

It's time for me to go to the house.... I should have known that.

I'm getting too old to write code. I'm forgetting more than I"m learning.  Thanks.

 

4 minutes ago, HebaruSan said:

My first guess would be your craft has three parts, or a part has three of your modules.

Well maybe you can  tell me this which is got me off on that stupid wild goose chase.

                BaseField field = Fields[nameof(myUIToggle)];
                field.uiControlEditor.onFieldChanged = onGUIChange;

That works as intended in the Editor.  It calls on GUIChange when the toggle is clicked.  But it doesn't work in flight.

 

Link to comment
Share on other sites

1 hour ago, HebaruSan said:

Which function contains those lines? Maybe onFieldChanged is only being set in the editor.

That's in the partmodule Start and it's before I check to see if it's editor or flight.  I moved those lines around so they're only called in the editor or called in fight with no difference. Supposedly, it's a callback to a function I named onGUIChange.  When the UI_Toggle is clicked it's supposed to call that function.  I'm trying to use the partmodule UI to open a main UI.  In the editor, it performs the callback just fine. I click on the UI_Toggle and it calls the function.  In flight... it does nothing. I've tried debug logging and it never even gets to the function.  I was trying to track down why or come up with a workaround when I went stupid with the booleans.

Edited by Fengist
Link to comment
Share on other sites

9 minutes ago, HebaruSan said:

Admittedly this is a long shot, but try OnStart instead. I'm seeing conflicting explanations of the difference, but on the whole it sounds like OnStart is a bit safer / more consistent.

Thanks, I'll give that a try tomorrow and let you know. Had enough facedesking for one day.

Link to comment
Share on other sites

11 hours ago, HebaruSan said:

Admittedly this is a long shot, but try OnStart instead. I'm seeing conflicting explanations of the difference, but on the whole it sounds like OnStart is a bit safer / more consistent.

Just checked. It was running in OnStart and not Start.

Stumbled on another way to do this.

            Fields[nameof(openUI)].uiControlEditor.onFieldChanged = delegate (BaseField a, System.Object b)
            {
                FASLog.Writelog("OPEN UI");
            };

And again, it works in the editor, but not in flight.

And then... I saw the blatantly obvious answer

            field = Fields[nameof(containsBalloonet)];
            field.uiControlFlight.onFieldChanged = onGUIChange;

It was staring me right in the face but I, and another old modder InfiniteDice simply did not see it.

Edited by Fengist
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...