Jump to content

Making a plugin load


Ydoow

Recommended Posts

I understand plugins work by attaching themselves to other parts and working through those parts, more or less.

So how would I get a plugin to influence any part? I want my plugin to load/function regardless of what's put on a craft.

I'm not seeing the big difference between implementation PartModule and Part. The wiki makes quick mention of it, and leads me to believe I should use PartModule, but I don't understand why, or even how.

My assumption is, though, that once I get my plugin attached to a part or vessel, I start overriding code in the OnLoad/OnStart/OnActive methods, correct?

So far I've implemented PartModule and thrown some basic Log output messages into the mentioned methods, but nothing shows up; because I don't know how to get the plugin to utilize any parts.

Link to comment
Share on other sites

MODULE

{

name = ClassName

}

That's the thing in the part.cfg that adds your PartModule derived-component to the part GameObject so all of the Monobehaviour and PartModule methods get called when relevant for that part.

I understand plugins work by attaching themselves to other parts and working through those parts, more or less.

So how would I get a plugin to influence any part? I want my plugin to load/function regardless of what's put on a craft.

I'm not seeing the big difference between implementation PartModule and Part. The wiki makes quick mention of it, and leads me to believe I should use PartModule, but I don't understand why, or even how.

My assumption is, though, that once I get my plugin attached to a part or vessel, I start overriding code in the OnLoad/OnStart/OnActive methods, correct?

So far I've implemented PartModule and thrown some basic Log output messages into the mentioned methods, but nothing shows up; because I don't know how to get the plugin to utilize any parts.

Edit: Similar to Kerbal Alarm Clock. It states you don't need to attach any part to a craft to make the plugin work, but have no idea how to accomplish that.

Edited by Ydoow
Link to comment
Share on other sites

Perhaps this example code helps:

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

namespace ClassLibrary1
{
public class Class1 : ScenarioModule
{
public static bool running;

private int rounds = 0;
private int textShown = 0;

public Class1()
{
if (!Class1.running)
{
Class1.running = true;
Thread worker = new Thread(Worker);
worker.Start();
}
}

private void Worker()
{
while (true)
{
if (HighLogic.LoadedSceneIsFlight)
{
textShown++;
ScreenMessages.PostScreenMessage("Text " + textShown + " (" + rounds + ") from Worker();", 1, ScreenMessageStyle.UPPER_CENTER);
}
rounds++;
Thread.Sleep(2000);
}
}
}
}

When entering the scene that the scenario is set to, it starts a background thread that will continue even if you leave that scene, allowing your plugin to work without any parts, and in any scene.

Link to comment
Share on other sites

I understand plugins work by attaching themselves to other parts and working through those parts, more or less.

Technically Part code objects and PartModule-derived code objects are both attached to the part's top level GameObject, but the Part codeObject does maintain a list of PartModules that it calls methods on.

So how would I get a plugin to influence any part?

You would get a reference to that part and then call methods on it. Typically through the vessel, for example FlightGlobals.ActiveVessel, FlightGlobals.Vessels or this.vessel (the last one only on Part and PartModule derived classes).

I'm not seeing the big difference between implementation PartModule and Part. The wiki makes quick mention of it, and leads me to believe I should use PartModule, but I don't understand why, or even how.

My assumption is, though, that once I get my plugin attached to a part or vessel, I start overriding code in the OnLoad/OnStart/OnActive methods, correct?

I want my plugin to load/function regardless of what's put on a craft.

Then you can't use PartModule because objects using that class only get created when you add a part to a craft. What you do is create your own GameObject and add a MonoBehaviour derived class to that.

The favoured way at the moment to launch such code is to use KSP's testing framework, for example Deadly Reentry uses:


public class DeadlyReentry : KSP.Testing.UnitTest
{
public DeadlyReentry()
{
GameObject ghost = new GameObject("DeadlyReentryGhost", typeof(DeadlyReentryGhost));
GameObject.DontDestroyOnLoad(ghost);
}
}

public class DeadlyReentryGhost : MonoBehaviour
{
//Do stuff, all of the [URL="http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html"]MonoBehaviour[/URL] overridable methods will get called.
}

When entering the scene that the scenario is set to, it starts a background thread that will continue even if you leave that scene, allowing your plugin to work without any parts, and in any scene.

Did you test that? I was under the impression that unity wasn't thread safe and trying to access anything in the game from a secondary thread would cause problems.

Link to comment
Share on other sites

Thanks endless wave! I used your method, and I'm seeing messages sent to the Log, so it is working.

When exactly is the plugin loaded, though? During the loading screen, Main Menu, Saved file being loaded, Flight scene, etc...?

It doesn't really matter I guess, but it would be good to know for future use perhaps.

Link to comment
Share on other sites

When exactly is the plugin loaded, though? During the loading screen, Main Menu, Saved file being loaded, Flight scene, etc...?

The output_log.txt in the KSP_Data folder contains the details of the game's most recent run.

It looks like unit tests are loaded before almost everything else as you'd expect, so before the loading screen.

Edited by EndlessWaves
Link to comment
Share on other sites

Edit: Similar to Kerbal Alarm Clock. It states you don't need to attach any part to a craft to make the plugin work, but have no idea how to accomplish that.

Kerbal Alarm Clock uses a "ghost part" to load its stuff into the game on startup

When entering the scene that the scenario is set to, it starts a background thread that will continue even if you leave that scene, allowing your plugin to work without any parts, and in any scene.

It seems that you need to add the scenario module into the persistent.sfs manually, or it won't be loaded

EDIT: BTW EndlessWaves, is the source code of Deadly Reentry available anywhere? The forum thread is lost.

Edited by Michael Kim
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...