Jump to content

can someone please look over the code for my first ever plugin. I dont want to break the game.


Recommended Posts

_____________________________________________________________________

  1. using UnityEngine;
  2. using System;
  3. using System.Timers;

  4. namespace MachGuage
  5. {
  6. public class MachGuage : Plugin
  7. {
  8. private static Timer aTimer;

  9. aTimer.Tick //this hopefully will do all the calculations for converting m/s to mach
  10. {
  11. MachNumber = (this.Velocity * 0.0029411764705882);
  12. {
  13. Round(MachNumber);
  14. }
  15. }
  16. private Rect _windowposition = new Rect();
  17. private GUIStyle _windowStyle, _labelStyle;
  18. private bool _hasInitStyles = false;


  19. public override void OnStart(StartState state)
  20. {
  21. aTimer = new System.Timers.Timer(20)
  22. aTimer.Enabled = true;
  23. if (state != StartState.Editor),
  24. {
  25. if (!_hasInitStyles) InitStyles();
  26. RenderingManager.AddToPostDrawQueue(0, OnDraw);
  27. }
  28. }

  29. private void OnDraw()
  30. {
  31. if (this.vessel == FlightGlobals.ActiveVessels)
  32. _WindowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "Mach Guage")
  33. }

  34. Private void OnWindow(int windowId)
  35. {
  36. GUILayout.BeginHorizontal(GuiLayout.Width(100f));
  37. GUILayout.label(aTimer.Tick = MachNumber);
  38. if (MachNumber == 0)
  39. {
  40. GUILayout.Label("You are not moving. and now the guage is bored");
  41. }
  42. GUILayout.EndHorizontal();

  43. GUI.DragWindow();
  44. }

  45. prvate void InitStyles()
  46. {
  47. _windowStyle = new GUIStyle(HighLogic.Skin.Window);
  48. _windowStyle.fixedWidth = 100f

  49. _labelStyle = new GUIStyle(HighLogic.Skin.label);
  50. _labelStyle.stretchWidth = true;

  51. _hasInitStyles = true;
  52. }
  53. }
  54. }
  55. the copy from pastebin messed up the brackets and stuff sorry about that
  56. _______________________________________________________________________________________________
    This is supposed to be a mach gauge, and my first time doing anything with C#. If you revise it will you please send me the code via private message. You will be given credit for your help when i eventually release the plugin. by the way, I would like integration with the default toolbar so i dont have to make a part.
    Thanks,
    Ryan:D:D

Link to comment
Share on other sites

All of it seems fine except this:

  1. private static Timer aTimer;
  2. aTimer.Tick //this hopefully will do all the calculations for converting m/s to mach
  3. {
  4. MachNumber = (this.Velocity * 0.0029411764705882);
  5. {
  6. Round(MachNumber);
  7. }
  8. }

You should just use the OnUpdate() method like this:

  1. public override void OnUpdate ()
  2. {
  3. MachNumber = (this.Velocity * 0.0029411764705882);
  4. Round(MachNumber);
  5. }

Also, you should extend PartModule, not Plugin. Plugin isn't even a class.

Also, are you using an IDE like MonoDevelop/Xamarin Studio or Visual Studio, or are you just using Notepad, because you should be using an IDE.

Edited by MrHappyFace
Link to comment
Share on other sites

If you want it without a part, then do something like this:


[KSPAddon(KSPAddon.Startup.Flight, false)] //this spawns your addon seperate from any part. Can run in any game scene including the loading screen, the main menu, the editor, and in flight
public class MachGauge : MonoBehaviour //Read up on the unity documentation for MonoBehaviour [URL="http://docs.unity3d.com/ScriptReference/MonoBehaviour.html"]here[/URL]
{
//replace OnStart(StartState state) with Start()
//replace OnUpdate() with Update()
//use FlightGlobals.ActiveVessel instead of vessel or part
}

And for the toolbar, use the official documentation: http://forum.kerbalspaceprogram.com/threads/86682-Appilcation-Launcher-and-Mods

Link to comment
Share on other sites

Also, parts and vessels already have mach numbers (vessel.mach and part.machNumber) you don't have to (and shouldn't*) calculate it yourself.

*I mean, you could. But if you do, you'll need to calculate the speed of sound, first.

Link to comment
Share on other sites

You are right nathan. I wont be calculating a single damn thing. Thanks. Here the revised label code

Private void OnWindow(int windowId)

{

GUILayout.BeginHorizontal(GuiLayout.Width(100f));

GUILayout.label(vessel.mach);

}

Link to comment
Share on other sites

Also, parts and vessels already have mach numbers (vessel.mach and part.machNumber) you don't have to (and shouldn't*) calculate it yourself.

*I mean, you could. But if you do, you'll need to calculate the speed of sound, first.

By the way you gonna update real solar system any time soon

Link to comment
Share on other sites

That code won't actually work, you need to close any layout groups (ie. you're missing a GUILayout.EndHorizontal()). There's a simpler way to do it aswell:

private void OnWindow(int windowId)
{
GUILayout.label(vessel.mach.ToString(), GUILayout.Width(100f));
}

EDIT

Formatting would probably be useful as well

private void OnWindow(int windowId)
{
GUILayout.label(vessel.mach.ToString("0.000"), GUILayout.Width(100f)); // prints a number rounded to 3dp in a label 100 units wide
}

Edited by Crzyrndm
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...