Jump to content

What am I doing wrong?


Recommended Posts

Ok, so I'm new to Unity development, and I can't figure out what I'm doing wrong. I have the following code:

 [KSPAddon(KSPAddon.Startup.EditorVAB, false)]
public class Class1 : MonoBehaviour
{
bool inQ = false;

public void Awake()
{
print("awake");
}

public void Start()
{
print("Starting the test mod");

}

public void OnGUI()
{
if (!inQ)
{
RenderingManager.AddToPostDrawQueue(0, DrawGui);
inQ = true;
}
}

public void DrawGui()
{
var r = new Rect(300, 50, 200, 200);
GUI.Window(0, r, FillWindow, "Test Window");
}

public void FillWindow(int windowID)
{
GUILayout.BeginVertical();
if (GUI.Button(new Rect(20, 20, 50, 50), "Push"))
{
print("Button pushed");
}
GUILayout.EndVertical();
}
}

It displays a small window with a button exactly as desired. However, in the VAB, when I hover over a part, the tooltip window is blank and I get a message in the log that says "ArgumentException: Getting control 0's position in a group with only 0 controls when doing Repaint Aborting". It appears that somehow my plugin is interfering with the VAB tooltip window. Any ideas? I've tried looking at the code for both Kerbal Alarm Clock, and Engineer Redux and, while I'm not nearly as complex, am trying to follow the same pattern.

Any help is greatly appreciated.

Link to comment
Share on other sites

One thing that I noticed is that you used GUI.Button instead of GUILayout.Button. Also, GUI.Window instead of GUILayout.Window. Any reason?

Other than that, which is not wrong, I don't see anything that is obviously wrong and would be causing your issue. Are you sure it is your mod? Do you have any others installed? I know that an older version of Kerbal Engineer threw a lot of exceptions at various times.

Unfortunately, I have not added a GUI example to https://github.com/taraniselsu/TacExamples yet. In the mean time, feel free to look at https://github.com/taraniselsu/TacPartLister and https://github.com/taraniselsu/TacLib. My TAC Part Lister is a (fairly) simple mod that shows a window in the VAB and SPH.

Link to comment
Share on other sites

Unity calls GUI code in separate passes for layout and rendering. These are implemented as events. The error you're getting means that your (or some) GUI code gets called for repaint before layout. Hence, it can't find the layout data for your first control...

I'm not sure if adding it in OnGUI like that is safe because I don't know when the addition to the queue will become effective. I looked at the source for Kerbal Alarm Clock and it looks like it only adds the GUI to the queue when a flag was set during a game world update. This means that unlike in your code, in KAC it actually happens at a defined time.

You can check Event.current.type to see which pass it's doing, e.g. for repaint Event.current.type == EventType.Repaint will be true. Maybe you can avoid adding things to the queue during a repaint event?

Edit: And like TaranisElsu wrote, you probably want to use GUILayout. instead of GUI., since you do use GUILayout.BeginVertical(), which will only affect layout of GUILayout. elements.

Edited by damny
Link to comment
Share on other sites

Thank you both for your helpful hints. First, I'm sure its my mod as its the only one I have installed on this instance of KSP. I made a separate copy of KSP and deleted all other mods, and about half of the SQUAD parts as well for fast loading. Everything is fine if I take out my mod. The only reason I was using GUI instead of GUILayout was lack of knowledge. Thank you for the pointers. Having read up on them now, I believe I understand the difference much better.

Unfortunately, I tried changing that and it didn't help. I will take a look at TacPartLister and see if I can figure anything out. It's very strange.

Link to comment
Share on other sites

Ok, so I couldn't get my code to work no matter what I tried, but I downloaded and studied TacPartLister and TacLib and I got something to work following that code. Thank you so much, TaranisElsu. I greatly appreciate the help.

On a side note, when I first added your code, it wouldn't load at all. I had to re-target my library to .Net 3.5 and then it loaded fine. For some reason, on .Net 4.5, which is the default for Visual Studio 2012, I was getting assembly load errors. Does KSP have an official .Net target platform that I should make sure I use?

Link to comment
Share on other sites

Does KSP have an official .Net target platform that I should make sure I use?

Unity uses Mono. There is no direct relationship, but if you create a MonoDevelop project in Unity, it sets 3.5 as the target.

Link to comment
Share on other sites

I think this should help.


public void DrawGui()
{
var windowID = 123 // choose one you like (lower ones may interfere with KSP Window IDs).
var r = new Rect(300, 50, 200, 200);
GUI.Window(windowID, r, FillWindow, "Test Window");
}

public void FillWindow(int windowID)
{
if (windowID == 123) // ID from above
{
GUILayout.BeginVertical();
if (GUI.Button(new Rect(20, 20, 50, 50), "Push"))
{
print("Button pushed");
}
GUILayout.EndVertical();
}
}

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