Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

This may have been asked before, as I only skimmed through the thread before posting, so apologies if this is a repeat of a previous question.

How would I go about adding my own propellant? And then how would I go about making a custom engine that runs off that propellant and electricity?

Link to comment
Share on other sites

How would I go about adding my own propellant? And then how would I go about making a custom engine that runs off that propellant and electricity?

Resources are defined by .cfg files (of any name) in the Resources folder, see the ResourcesGeneric.cfg holding the default resources for an example.

You don't need a custom engine to use a custom propellant, you can simply change the name of the resource ModuleEngines uses.

Link to comment
Share on other sites

Resources are defined by .cfg files (of any name) in the Resources folder, see the ResourcesGeneric.cfg holding the default resources for an example.

You don't need a custom engine to use a custom propellant, you can simply change the name of the resource ModuleEngines uses.

Okay. So making a fuel tank for the custom propellant would just require me to change the name of the resource that it stores, correct?

Link to comment
Share on other sites

Is there a way to check what kind of terrain a vessel is on? I want to be able to detect if the vessel is in an ocean or on ice. Is there a simple way to do this, or will I have to give it the coordinates of each ocean and ice cap?

Link to comment
Share on other sites

If you did it right, you should have a function with the [KSPEvent] attribute... put your stuff in that function :P

Okay so I'm a complete noob here. Do I put the button gui stuff in the [KSPEvent] or do I put the stuff the button does into the [KSPEvent]?

Link to comment
Share on other sites

The [KSPEvent] line (known as an attribute) causes the game to create a button on the right click menu that, when clicked, executes the code inside the method that the [KSPEvent] attribute is applied to (placed on the line above).

Link to comment
Share on other sites

There doesn't appear to be any plugin code in there at all so this probably isn't the best place for it. With config files you're generally only setting the game-wide options, so if it works the first time you test a feature there generally isn't any hidden surprises.

Also, I would stick to using the default ModuleGenerator unless you actually need the features in ResGen (which you don't appear to be using from the config files). It's better tested, is much more likely to be compatible with future versions and doesn't require download of a third party plugin (which some people don't like doing).

Link to comment
Share on other sites

Okay, thanks for the tips. Is there any way to check if the vessel is splashed down and activate the ModuleGenerator if it is using just config files? I want to make a pump that can collect water from oceans, and if there's a way to make it activate when the vessel splashes down, it would save me a lot of time.

Link to comment
Share on other sites

Hi. I'm trying to rotate attached parts through a simple PartModule, and the best I managed to produce was the whole ship exploding when I applied MoveRotation to the part's Rigidbody. I haven't tried anything concerning AttachNode yet since it's getting late, so I'm interested if anyone knows how I could do this.

I'd hate it if the only way was to make animated parts:S

Link to comment
Share on other sites

Hi, I'm trying to develop a plugin that will show specific bits of information based on the celestial body that the ship is landed on (so long as the ship has the module that the plugin is linked to). I'm really stuck as to how I can get set the plugin to recognize the celestial body and display the relevant information. (I may add a button to refresh the data). Can anyone help? Thanks.

Link to comment
Share on other sites

Hi. I'm trying to rotate attached parts through a simple PartModule, and the best I managed to produce was the whole ship exploding when I applied MoveRotation to the part's Rigidbody. I haven't tried anything concerning AttachNode yet since it's getting late, so I'm interested if anyone knows how I could do this.

I'd hate it if the only way was to make animated parts:S

Part RigidBodies are joined by physics Joints so trying to rotate them through the physics system will put a physics strain on them.

It depends on what you're trying to achieve, if the part has nothing connected to it then it may be sufficient to just rotate the model transform (which includes the colliders). Otherwise you probably want to do something like Damned Robotics does and replace the fixed joints between parts with ones that can be rotated.

Hi, I'm trying to develop a plugin that will show specific bits of information based on the celestial body that the ship is landed on (so long as the ship has the module that the plugin is linked to). I'm really stuck as to how I can get set the plugin to recognize the celestial body and display the relevant information. (I may add a button to refresh the data). Can anyone help? Thanks.

If you're extending PartModule then the current CelestialBody whose sphere of influence the vessel is in should be this.vessel.mainBody

Link to comment
Share on other sites

If you're extending PartModule then the current CelestialBody whose sphere of influence the vessel is in should be this.vessel.mainBody

But is there a code I can use maybe so that when the button in the window is clicked, it detects what the celestial body is? I can make the button but I can't get it to actually do anything. (I've basically got a window with a useless button at the moment.) I'd also like the plugin to display different information, based on the detected celestial body. (eg. Button is clicked ---> Celestial body is found to be Duna ---> Displays information relevant to Duna.) It would be great if it could all be set up for clicking that one button.

Link to comment
Share on other sites

(eg. Button is clicked ---> Celestial body is found to be Duna ---> Displays information relevant to Duna.)

Uh, can you not check what it's called? I'm fairly sure there's a CelestialBody.name property or something that'll allow you to easily identify which one it is

switch(this.vessel.mainBody.name)
{
case "Duna":
//Do Duna Stuff
break;
case "Eve:
//Do Eve Stuff
break;
}

Link to comment
Share on other sites

Thanks EndlessWaves, that's really helpful but now I've got an issue that I really don't get. I've got this code:

private void OnWindow(int RUILifeTechWindow)

{

GUILayout.BeginVertical();

if (GUILayout.Button("Get Data", _buttonStyle))

{

switch (this.vessel.mainBody.name) {

case "Kerbin" :

GUILayout.Label("Current Body: Kerbin", _labelStyle);

GUILayout.Label("Atmosphere Present: Yes", _labelStyle);

GUILayout.Label("Oxygen Present: Yes", _labelStyle);

break;

}

}

GUILayout.EndVertical();

GUI.DragWindow();

}

So, the button identifies the body as Kerbin. However, once it does that, it doesn't display the labels? I've tested it out with other things like "Application.OpenURL" and it works fine, but I can't get it to create the labels. I'm probably doing something really stupid because I'm new to C# and the whole KSP plugin thing. Any ideas what the problem is? Thanks.

Link to comment
Share on other sites

I'm having a lot of trouble trying to use a class to store information for a PartModule. I have had success when using a provided class like ModuleResource, but when using my own class I get a null pointer exception. Bellow is a simplified example of what I'm doing.


namespace MyNameSpace
{
public class TestClass
{
public string name;
public int value;
}


public class MyPartModule : PartModule
{
public TestClass testObject;
public ModuleResource resourceObject;

public override void OnLoad(ConfigNode node)
{
testObject = new TestClass();
resourceObject = new ModuleResource();

testObject.name = "Object 1";
testObject.value = 31;

resourceObject.name = "ElectricCharge";
resourceObject.rate = 0.5;
}

public override void OnSave(ConfigNode node)
{
Debug.Log("OnSave: resourceObject name: " + resourceObject.name + " | rate: " + resourceObject.rate);

Debug.Log("OnSave: testObject name: " + testObject.name + " | value: " + testObject.value);
}

}
}

Using Debug.Log to print out the status as it goes, OnLoad is called during game load and runs fine. I can print back the values stored in both objects. When selecting the part the module is attached to in the VAB, OnSave is then called and throws a null pointer exception for testObject. ResourceObject however works fine.

I'm stumped as to why ResourceModule works but my class does not. Can anyone offer any insight into this?

Link to comment
Share on other sites

Any ideas what the problem is? Thanks.

I haven't done much GUI stuff, but it's largely a system from the unity engine rather than a KSP specific one so you may be able to broaden your search into using buttons to create GUI elements in unity.

I'm stumped as to why ResourceModule works but my class does not. Can anyone offer any insight into this?

Code objects get destroyed between scenes. It is a bit of a mystery about whether there's supposed to be an official way of saving objects but the workaround I normally use is to reference it in a static variable - often a dictionary keyed to part name for compatibility for different parts on the same module.

Link to comment
Share on other sites

Keep in mind that I'm just an utter newbie at plugin development.

I've got a part, the Orion Engine. In Unity 3D, it is called "USAF10MeterOrionEngine2". It is composed of two sub-parts: "obj_propulsionuint" and "obj_pusherplate".

pushProb01.jpg

In the plugin, an animation is played. Each time a propulsion charge is detonated below the pusher plate, obj_pusherplate is moved upward. Then over the period of a second, it gradually moves back down to the rest position. That is working perfectly.

The trouble comes if you try to attach other parts to the pusher plate.

pushProb02.jpgpushProb03.jpgpushProb04.jpg

In the first picture, in the VAB, I've attached landing struts and ladders to the pusher plate. In the second picture, you see the rocket in flight with the pusher plate in the rest position. In the third picture you see the problem. The pusher plate moves but the struts stay in the same position.

I animate the pusher plate movement by brute force, manually changing the pusher plate's transform:


public string engineModelName = "USAF10MeterOrionEngine2";
public string subModelPlateName = "obj_pusherplate";
private Transform entireModel;
private Transform pusherPlate;
public float plateOffset = 4.88f;
public float plateSpeed = 4.88f;

protected override void onFlightStart()
{
...
// get pusher plate section of propulsion system model
this.entireModel = base.transform.FindChild("model");
this.pusherPlate = this.entireModel.FindChild(this.engineModelName);
this.pusherPlate = this.pusherPlate.FindChild(this.subModelPlateName);
...
}

protected void launchBomb()
{
...
this.pusherPlate.localPosition = new Vector3(0f, this.plateOffset, 0f);
...
}

protected override void onActiveFixedUpdate()
{
float newPlateY = this.pusherPlate.localPosition.y;
// if pusher plate is currently displaced upward by a bomb detonation...
if (this.pusherPlate.localPosition.y > 0.01f)
{
// multiply by Time.get_deltaTime() to convert plateSpeed into displacement per second
float plateDelta = this.plateSpeed * Time.deltaTime;
if (plateDelta < newPlateY) {
newPlateY -= plateDelta; // slowly move plate back down to rest position
} else {
newPlateY = 0f;
}
} else {
newPlateY = 0f; // plate at rest position
}

this.pusherPlate.localPosition = new Vector3(0f, newPlateY, 0f); // move plate to position
...
}

Why manually? Unfortunately the pusher plate also has an emission animation for heat build up. When I used another animation for the pusher plate movement, it disabled the emission animation.

So my question is: How can I make the parts attached to the plate move with the plate?

The obvious solution is to somehow identify which parts are attached to the plate and brute force move them as well. Trouble is, I do not know how to find out which parts are attached to obj_pusherplate, I suspect all the parts are attached to USAF10MeterOrionEngine2.

Any advice?

Link to comment
Share on other sites

The obvious solution is to somehow identify which parts are attached to the plate and brute force move them as well. Trouble is, I do not know how to find out which parts are attached to obj_pusherplate, I suspect all the parts are attached to USAF10MeterOrionEngine2.

Almost, joints between parts are physics joints and they're between RigidBodies, not colliders. The other parts are actually attached to a GameObject with the part name (the unique ID specified in the name field of the config), that is the parent of your 'USAFOrionEngine' object as this is the only GameObject in the part with a RigidBody component attached (as you can only have one per GameObject hierarchy).

As to getting it to work, probably the most straightforward solution I can think of is to make the pusher plate a separate part and swap the standard FixedJoint connection with the propulsion unit into a sliding joint (HingeJoint, ConfigurableJoint). This is similar to what R4m0n did with DamnedRobotics so there's some example code there. This could potentially also have the advantage that you wouldn't need to animate the bounce manually but could have it fully physics controlled, dependant on the strength of the force imparted and able to double up as a landing shock absorber.

Alternatively, just set up the config file to disallow surface attach. Given it's being irradiated by nuclear bombs who's going to want to get close enough to stick anything to the surface anyway?

Link to comment
Share on other sites

As to getting it to work, probably the most straightforward solution I can think of is to make the pusher plate a separate part and swap the standard FixedJoint connection with the propulsion unit into a sliding joint (HingeJoint, ConfigurableJoint). This is similar to what R4m0n did with DamnedRobotics so there's some example code there. This could potentially also have the advantage that you wouldn't need to animate the bounce manually but could have it fully physics controlled, dependant on the strength of the force imparted and able to double up as a landing shock absorber

Thanks! I'll look into that

Alternatively, just set up the config file to disallow surface attach. Given it's being irradiated by nuclear bombs who's going to want to get close enough to stick anything to the surface anyway?

Well, the problem is that when it is sitting on just its pusher plate, the entire rocket sways back and forth for some odd reason. When I attach launch clamps or landing shock absorbers, the sway stops.

I was also experimenting with some stock solution to the problem of the poor Kerbals making their way down 20 meters of Orion engine to the ground. A crane would be ideal, but that is not stock.

Link to comment
Share on other sites

Using Debug.Log to print out the status as it goes, OnLoad is called during game load and runs fine. I can print back the values stored in both objects. When selecting the part the module is attached to in the VAB, OnSave is then called and throws a null pointer exception for testObject. ResourceObject however works fine.

I'm stumped as to why ResourceModule works but my class does not. Can anyone offer any insight into this?

I think the part is cloned, not the original that had the "onLoad" method called. The initial workings is probably remnant information (and the IConfigNode stuff that ModuleResource implements).

What you should probably be doing is using a MonoBehaviour (I mean, you can use statics... but if this is VAB / SPH bound; and not part specific... it seems rather brute force), part calls the behaviour via the behaviour's own static interface and the behaviour handles all the storing / managing of data. (KSP's new interface also has "[KSPAddon(KSPAddon.Startup.EditorAny, false)]" which will unload the behaviour when it is not needed (i.e. outside the VAB / SPH).

Link to comment
Share on other sites

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