Jump to content

[Partless Plugin] How to make a Window in Flight Scene?


Recommended Posts

Hey guys,

first off, i´m new to that c# stuff so i want to teach myself a bit by makeing a partless plugin.

So i started looking around YT for some tutorials of plugin making but all videos i found are about part plugins :(

I also searched this forum and found some stuff that helped me a bit ;)

Well i just want to make a window shown up in the flight scene with a title and some labels for the beginning but i can´t get it to work :mad:

Here is the code i have so far:

using UnityEngine;

namespace Omg
{
    public class Omg : MonoBehaviour
    {
        private Rect _windowPosition = new Rect();

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

        private void OnDraw()
        {
            if (this.vessel == FlightGlobals.ActiveVessel)
                _windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "I´m the title :D");
        }

        private void OnWindow(int windowId)
        {
            GUILayout.BeginHorizontal(GUILayout.Width(250f));
            GUILayout.Label("Guess what? I´m the label!");
            GUILayout.EndHorizontal();

            GUI.DragWindow();
        }
    }

}

When i press F6 i got an error about the StartState Methode.

I think it´s coz this  code is aimed to a part plugin.

What do i need to change to make this tiny code working?

Oh btw. any tutorial about partless plugins is welcome :D

 

cheers

Taxi 

 

Link to comment
Share on other sites

Here you go. KSPAddon are how you do part less plugins

 

using UnityEngine;

namespace Omg
{
 
    [KSPAddon(KSPAddon.Startup.EditorAny, false)]
    public class DrawGravity : MonoBehaviour
    {
        private Rect _windowPosition = new Rect();


        public void Start()
        {
            // KSPAddon is set to EditorAny so you it only runs inside the editor
            RenderingManager.AddToPostDrawQueue(0, OnDraw);
        }

        private void OnDraw()
        {
               _windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "I´m the title :D");
        }

        private void OnWindow(int windowId)
        {
            GUILayout.BeginHorizontal(GUILayout.Width(250f));
            GUILayout.Label("Guess what? I´m the label!");
            GUILayout.EndHorizontal();

            GUI.DragWindow();
        }
    }
}

 

Link to comment
Share on other sites

Hi,

@sarbian

Thanks for your help, you saved my day :D

With 2 little changes your code works now!

Just changed:

[KSPAddon(KSPAddon.Startup.EditorAny, false)]
    public class DrawGravity : MonoBehaviour

into this:

[KSPAddon(KSPAddon.Startup.Flight, false)]
    public class Omg : MonoBehaviour

and it works fine for me in flight scene :D

Now just another thing, how to display the TWR and DeltaV (total and stage) in the window? :huh:

 

cheers

Taxi

Link to comment
Share on other sites

  • 6 months later...
On 27/02/2016 at 10:47 AM, sarbian said:

Here you go. KSPAddon are how you do part less plugins

I'm trying to get the example you posted to work, this is the code I have (which is basically the same as what you posted); 
 

Spoiler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace TestMod
{

	[KSPAddon(KSPAddon.Startup.EditorAny, false)]
	public class TestModPartless : MonoBehaviour
	{
		private Rect _windowPosition = new Rect();

		public void Start()
		{
			// KSPAddon is set to EditorAny so you it only runs inside the editor
			RenderingManager.AddToPostDrawQueue(0, OnDraw);
		}

		private void OnDraw()
		{
			_windowPosition = GUILayout.Window(10, _windowPosition, OnWindow, "I´m the title :D");
		}

		private void OnWindow(int windowId)
		{
			GUILayout.BeginHorizontal(GUILayout.Width(250f));
			GUILayout.Label("Guess what? I´m the label!");
			GUILayout.EndHorizontal();

			GUI.DragWindow();
		}
	}
}

 

I have references setup for Assembly-CSharp and UnityEngine, but when I come to try and build I get an error for the line inside Start()
RenderingManager.AddToPostDrawQueue(0, OnDraw);
The error: The type `Callback' is defined in an assembly that is not referenced. Consider adding a reference to assembly `KSPUtil, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' (CS0012) (test)

If I add a reference to KSPUtil then it will build without errors.  However when I go into one of the editors I don't get a window showing up, but I can see in the logs an exception which says;
NullReferenceException: Object reference not set to an instance of an object
    RenderingManager.AddToPostDrawQueue (Int32 queueSpot, .Callback drawFunction)
    TestMod.TestModPartless.Start ()
 

This is my first attempt at writing a KSP mod so I'm probably doing something noobish. Any suggestions? 
 

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