Jump to content

Tried to make a GUI text area and now there are no parts in the game


Recommended Posts

I was following the tutorial here and decided I wanted to add a TextArea object and a Button. When I added them KSP could no longer find any of the parts in the game and the vessel I had been testing the PartModule part on failed to load because no parts existed anymore. What could possibly have caused that? I've checked the KSP folder and everything still exists and no directory names have been changed...

Link to comment
Share on other sites

If you want a sea of text, here you go mate :) In the future it may be more practical to start form the perspective of common causes of such errors in order to provide context that would allow me to share code specific to the issue.

using UnityEngine;
using GuiWindowPlugin.Extentions;
using KSP.IO;


namespace GuiWindowPlugin
{
public class GuiWindowPlugin : PartModule
{
private Rect _windowPosition = new Rect();
private GUIStyle _windowStyle, _textAreaStyle, _buttonStyle;
private bool _hasInitStyles = false;
private int _drawCall = 0;
private string _textAreaText = "";


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


public override void OnSave(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<GuiWindowPlugin>();
config.SetValue("Window Position", _windowPosition);
config.SetValue("Draw Call", _drawCall);
config.SetValue("Text Area Text", _textAreaText);
config.save();
}


public override void OnLoad(ConfigNode node)
{
PluginConfiguration config = PluginConfiguration.CreateForType<GuiWindowPlugin>();
config.load();
_windowPosition = config.GetValue<Rect>("Window Position");
_drawCall = config.GetValue<int>("Draw Call");
_textAreaText = config.GetValue<string>("Text Area Text");
}


private void OnDraw()
{
_drawCall++;
if (this.vessel == FlightGlobals.ActiveVessel && this.part.IsPrimary(this.vessel.parts, this.ClassID))
{
_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Draw Call #" + _drawCall.ToString(), _windowStyle);
if (_windowPosition.x == 0f && _windowPosition.y == 0f)
{
_windowPosition = _windowPosition.CenterScreen();
}
}
}
private void OnWindow(int windowId)
{
GUILayout.BeginVertical();
GUILayout.TextArea(_textAreaText, _textAreaStyle);
GUILayout.Button("Run", _buttonStyle);
GUILayout.EndVertical();

GUI.DragWindow();
}


private void InitStyles()
{
_windowStyle = new GUIStyle(HighLogic.Skin.window);
_windowStyle.fixedWidth = 250f;


_textAreaStyle = new GUIStyle(HighLogic.Skin.textArea);
_textAreaStyle.stretchWidth = true;


_buttonStyle = new GUIStyle(HighLogic.Skin.button);
_buttonStyle.fixedWidth = 100f;


_hasInitStyles = true;


}
}
}

using System.Collections.Generic;
using UnityEngine;


namespace GuiWindowPlugin.Extentions
{
public static class PartExtentions
{
public static bool IsPrimary(this Part thisPart, List<Part> partsList, int moduleClassID)
{
foreach (Part part in partsList){
if (part.Modules.Contains(moduleClassID))
{
if (part == thisPart)
{
return true;
}
else
{
break;
}
}
}
return false;
}
}
}

using UnityEngine;


namespace GuiWindowPlugin.Extentions
{
public static class RectExtentions
{
public static Rect CenterScreen(this Rect thisRect)
{
if (Screen.width > 0f && Screen.height > 0f && thisRect.width > 0f && thisRect.height > 0f)
{
thisRect.x = (Screen.width / 2) - (thisRect.width / 2);
thisRect.y = (Screen.height / 2) - (thisRect.height / 2);
}
return thisRect;
}
}
}

Link to comment
Share on other sites

If you want a sea of text, here you go mate :) In the future it may be more practical to start form the perspective of common causes of such errors in order to provide context that would allow me to share code specific to the issue.

Sections of code are generally worthless unless you know what all the inputs/outputs are (and that they are valid). You also haven't mentioned any exceptions being logged that you could follow the stack trace for to narrow it down. My bet would be that it's throwing a lot of errors during the initial loading process, but it's impossible to tell without any information from the logs

PS

An easier way to handle default window positions

_windowPosition = config.GetValue<Rect>("Window Position"); // defaults to 0,0,0,0 if no value is found (current implementation)
_windowPosition = config.GetValue("Window Position", new Rect(10,10,0,0)); // defaults to 10,10,0,0 if no value can be found

EDIT

Your problem is probably going to be OnStart. It could get called during the part compilation process and the != editor check wont catch that (HighLogic.LoadedSceneIsFlight is probably the check you want)

Edited by Crzyrndm
Link to comment
Share on other sites

If you want a sea of text, here you go mate :) In the future it may be more practical to start form the perspective of common causes of such errors in order to provide context that would allow me to share code specific to the issue.

If you don't want help, don't ask for help. You procided no info except "it doesn't work". Based on that, the only guess I could make is that something screws up during the loading process, and your code is a very good indiction to *what* might be screwing up. The two basic things to provide is logs and code.

Crzyrndm has a good point. Other than that, I'm not sure if OnStart runs in the Loading Sequence, but if it does I can guarantee it will fail, beause this.vessel will be null. You're also not nullchecking FlightGlobals.ActiveVessel, which can be null a few frames while the vessel is being loaded in flight. To figure out more, logs will be needed.

Link to comment
Share on other sites

Your problem is probably going to be OnStart. It could get called during the part compilation process and the != editor check wont catch that

I changed it to

if (state == StartState.Flying)

and I have the same issue.

I tried removing all references to the TextArea control on the _textAreaText string and nothing changed.

Is there a way I can see debug output from the part compilation?

- - - Updated - - -

I found this, not sure exactly how to interpret it. From KSP.txt

[EXC 19:29:32.749] XmlException: Document element did not appear. file:///C:/TestEnvironments/KSP/Kerbal Space Program/Plugins/PluginData/GuiWindowPlugin/config.xml Line 1, position 1.	Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
Mono.Xml.EntityResolvingXmlReader.Read ()
Mono.Xml.DTDValidatingReader.ReadContent ()
Mono.Xml.DTDValidatingReader.Read ()
Mono.Xml.Schema.XsdValidatingReader.Read ()
System.Xml.XmlValidatingReader.Read ()
System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader)
System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader)
System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader)
System.Xml.XmlDocument.Load (System.String filename)
KSP.IO.PluginConfiguration.load ()
GuiWindowPlugin.GuiWindowPlugin.OnLoad (.ConfigNode node)
PartModule.Load (.ConfigNode node)
Part.AddModule (.ConfigNode node)
PartLoader.ParsePart (.UrlConfig urlConfig, .ConfigNode node)
PartLoader+.MoveNext ()
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
:MoveNext()
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
PartLoader:StartLoad()
:MoveNext()

Link to comment
Share on other sites

http://forum.kerbalspaceprogram.com/threads/92230-!!!-PLEASE-READ-BEFORE-POSTING-!!!-Stock-Support-Bug-Reporting-Guide

Scroll down to the bit about output logs. Search that file for exception and you'll find what you need

- - - Updated - - -

It's onload that's running

GuiWindowPlugin.GuiWindowPlugin.OnLoad (.ConfigNode node)

Something wrong with the config read

Link to comment
Share on other sites

The config node that's passed to OnLoad isn't invalid (or atleast, it has nothing to do with the error). It errors when you call:

KSP.IO.PluginConfiguration.load ()

which is the second line of OnLoad (config.load(); )

There's nothing out of the ordinary with your method, so that leaves either a corrupt file (delete this file: C:/TestEnvironments/KSP/Kerbal Space Program/Plugins/PluginData/GuiWindowPlugin/config.xml and see if things get resolved), or maybe a permission error.

Link to comment
Share on other sites

Note that one issue with a ConfigNode file is that it can't be empty.

If you have a "no data" ConfigNode.cfg file with nothing in it, KSP does not initialize the node correctly.

ConfigNode myNode = ConfigNode.Load(fileOnDisk.cfg);

will result in a myNode that is null if fileOnDisk.cfg is empty, or a valid config node if fileOnDisk.cfg has data.

Note that KSP will happily save an empty confignode file without issue, it just can't load it. Took me forever to figure that twist out when I ran into it.

Note that this is from my experience with ConfigNode.Save/Load with .cfg files on disk, I assume the same holds true for the XML files you are using.

D.

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