Jump to content

Simple GUI Question + Try/Catch Question


Recommended Posts

So, with Innsewerants\' permission I have been tweaking the code for his mapping module. I have it behaving almost exactly as I want it to save for two annoying details.

The first is I have added a graphical toggle to turn the module on and off. I want it to default to center screen, and am not sure how to make it do this.

The other issue is I have added a bit of caching with the intention that if the output file is busy, it sill simply skip a write pass & wait for the next one. For some reason, the try/catch block I have implemented this with does not get the memo when something breaks. I\'ve tried using several commands to print debug messages with no dice, and the system log shows it dumping the entire cache even though it\'s supposed to abort. I am either misunderstanding how a try/catch works with a loop or the engine is not telling my script about the error.

The code can be found here. If someone could take a look & give me a few pointers on where I screwed up, I\'d appreciate it.

Also, as I know there\'s at least one stupid thing in that, I should point out I don\'t actually know C#. I\'ve dabbled in Java and C++, and am faking it.

Link to comment
Share on other sites

As for your first question to center the gui.

windowRect = GUI.Window (0, windowRect, WindowFunction, 'Map Module');

You can get the screen width and height by calling Screen.width and Screen.height. Divide it by 2 and you have the center position.

private Rect windowRect = new Rect(Screen.width/2, Screen.height/2, 100, 60);

shoud suffice.

Link to comment
Share on other sites

I am assuming it is this try catch

try {
using (StreamWriter w = File.AppendText(kspDir + 'isa_RAM_Mapper/isa_RAM_' + bodyName + '_Data.csv')) {
while( isa_RAM_Buffer.Count > 0 ) {
w.WriteLine(isa_RAM_Buffer.Dequeue ());
}
w.Close ();
isa_RAM_Pass_Count = 1;
}
} catch (IOException e) {
Console.WriteLine('File Write Error. Pass #' + isa_RAM_Pass_Count + ' (' + e + \')\');
++isa_RAM_Pass_Count;
}

if so, do this to check that it is actually an IOException by changing the IOException to Exception then using the ToString method to find out the actual cause of the exception.

Note, do not leave it as exception, once you know what is can cause it, use the correct exception.

try {
using (StreamWriter w = File.AppendText(kspDir + 'isa_RAM_Mapper/isa_RAM_' + bodyName + '_Data.csv')) {
while( isa_RAM_Buffer.Count > 0 ) {
w.WriteLine(isa_RAM_Buffer.Dequeue ());
}
w.Close ();
isa_RAM_Pass_Count = 1;
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
//Console.WriteLine('File Write Error. Pass #' + isa_RAM_Pass_Count + ' (' + e + \')\');
//++isa_RAM_Pass_Count;
}

Link to comment
Share on other sites

I found the problem. The exception being caught was wrong. Should have been '} catch (UnauthorizedAccessException uae) {'. For S&G\'s I added an additional '} catch (PathTooLongException ptle) {' that shuts it down & clears the buffer.

Link to comment
Share on other sites

  • 2 weeks later...
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...