Jump to content

Two questions I can't pin down about hidden parts and GUI updating.


Recommended Posts

I've been learning KSP plugins mostly by trial and error, but two things I can't figure out are...

1. [Edit: I was trying to figure out how to hide parts, when I really just needed to delete the part folder. Realized after I posted.]

2. How do I update a GUI window's label during flight? I keep getting errors saying I'm not on the GUI thread but I have no idea where it should be updating from, right now it's using the OnUpdate() method.

Thanks in advance.

Edited by TwistedMexi
Link to comment
Share on other sites

GUI stuff needs to be done in OnGUI().

I thought so, but it doesn't seem to update for me.

Do I call OnGui() when I want it to update, or does it get called already? I've tried it both ways, but the label in the window stays on "This is a label." I'm used to just doing something like labelname.Text = stringvariable, so I'm unsure of how to specify this label specifically. That may be part of my problem.

This is what I have for OnGUI(), OnDraw(), and OnWindow()

Velocity is of course a string.

        public void OnGui()
{
GUI.Label(_windowPosition, velocity);
}

private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is my Plugin Title", _windowStyle);

if (_windowPosition.x == 0f && _windowPosition.y == 0f)
_windowPosition = _windowPosition.CenterScreen();
}
}

private void OnWindow(int windowId)
{
GUILayout.BeginHorizontal(GUILayout.Width(250f));
GUILayout.Label("This is a label.", _labelStyle);
GUILayout.EndHorizontal();

GUI.DragWindow();
}

Link to comment
Share on other sites

snip

I had the same problem at the beginning when you come from Winforms or WPF. With Unity, all gui controls are one shot so you don't update them, but just create another one and stop to draw the old one. Here i would do that (not verified) :

        public void OnGui()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is my Plugin Title", _windowStyle);

if (_windowPosition.x == 0f && _windowPosition.y == 0f)
_windowPosition = _windowPosition.CenterScreen();
}
}

private void OnWindow(int windowId)
{
GUILayout.BeginHorizontal(GUILayout.Width(250f));
GUILayout.Label("This is a label.", _labelStyle);
// Your labe
GUI.Label(_windowPosition, velocity);
GUILayout.EndHorizontal();

GUI.DragWindow();
}

Link to comment
Share on other sites

snip

Got it! Thanks for the help. Your code seemed off to me, but I was able to figure out what you meant. This is working code, for anyone else's reference.

It seems really ineffective to redraw every time like that, but I guess I just need to get used to Unity.

        public void OnGui()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is my Plugin Title", _windowStyle);
}
}

private void OnDraw()
{
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "This is my Plugin Title", _windowStyle);

if (_windowPosition.x == 0f && _windowPosition.y == 0f)
_windowPosition = _windowPosition.CenterScreen();
}
}

private void OnWindow(int windowId)
{
GUILayout.BeginHorizontal(GUILayout.Width(250f));
GUILayout.Label(velocity, _labelStyle);
GUILayout.EndHorizontal();

GUI.DragWindow();
}

Link to comment
Share on other sites

It seems really ineffective to redraw every time like that, but I guess I just need to get used to Unity.

Oh yes ! That was exacty my thought. As a rule of thumb, i just call gui stuffs in OnGUI or WindowFunc callback methods, i don't know if that's how it is designed to work but it works so.

Good luck.

Link to comment
Share on other sites

This is working code, for anyone else's reference.

Why do you do the same thing in OnGUI() and OnDraw()? You don't need OnDraw(). Like I've said in my first reply, GUI things go into OnGUI().

As a rule of thumb, i just call gui stuffs in OnGUI or WindowFunc callback methods, i don't know if that's how it is designed to work but it works so.

Looks good.

Link to comment
Share on other sites

Why do you do the same thing in OnGUI() and OnDraw()? You don't need OnDraw(). Like I've said in my first reply, GUI things go into OnGUI().

Looks good.

I followed a video tutorial for KSP to get familiar with the basic setup. It said the initial GUI gets called at OnDraw() and I use that for centering the window initially. I also have this:

        public override void OnStart(StartState state)
{
if (state != StartState.Editor)
{
if (!_hasInitStyles) InitStyles();
RenderingManager.AddToPostDrawQueue(0, OnDraw);
}
}

Do I not need OnDraw, and if that's the case, should

RenderingManager.AddToPostDrawQueue(0, OnDraw);

be

RenderingManager.AddToPostDrawQueue(0, OnGui);

?

Link to comment
Share on other sites

From my understanding of unity, On[...] methods for class that inherit from MonoBehaviour are called automatically. RenderingManager is a KSP class which is basically a list of callback to draw your stuff.

Take a look at the veru first example of Taraniselsu on his github. That's all you need to know of entry point for your code.

IMO If you can use RenderingManager, don't even bother with the rest (except awake, use it as a constructor, and OnDestroy, use that as an IDisposable). Register your callbock in rendering manager and call your ui function from that callback.

Link to comment
Share on other sites

Use either OnGUI or RenderingManager, not both. I recommend sticking with RenderingManager.

Your code here: http://forum.kerbalspaceprogram.com/threads/61639-Two-questions-I-can-t-pin-down-about-hidden-parts-and-GUI-updating?p=838979&viewfull=1#post838979 looks good if you delete the OnGUI method.

You will probably also need to move "if (!_hasInitStyles) InitStyles();" to your OnDraw method. Do you have any errors related to that? If not, then I am mistaken and it is fine there.

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