Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

hab136: Have you checked out public ShipConstruct EditorLogic.ship ... that and its add method looks promising.

Thanks! I got it working tonight. There's a little more than just ship.Add()... in particular to have to set the position and the AttachNodes.

This code is rough, but it will attach a large probe core to the bottom of another large probe core (or actually the bottom of whatever the root part is, but the numbers are for large probe cores):


// Get all the parts of the ship
EditorLogic editor = EditorLogic.fetch;
ShipConstruct ship = editor.ship;
List<Part> parts = ship.parts;

Part rootpart = parts [0];

// Make a new Part object
AvailablePart ap = PartLoader.getPartInfoByName ("probeStackLarge");
UnityEngine.Object obj = UnityEngine.Object.Instantiate(ap.partPrefab);
Part newPart = (Part)obj;

newPart.gameObject.SetActive(true);
newPart.gameObject.name = "probeStackLarge";
newPart.partInfo = ap;
newPart.transform.localScale = rootpart.transform.localScale;
newPart.transform.parent = rootpart.transform; // must be BEFORE localposition!
Vector3 v = new Vector3 ();
v.x = 0.0f;
v.y = -0.4f;
v.z = 0.0f;

Quaternion q = new Quaternion ();
q.x = 0.0f;
q.y = 0.0f;
q.z = 0.0f;
q.w = 1.0f;
newPart.transform.localPosition = v;
newPart.transform.localRotation = q;

rootpart.addChild (newPart);
ship.Add (newPart);

AttachNode newAN = newPart.findAttachNodes ("top") [0];
newAN.attachedPart = rootpart;
AttachNode rootAN = rootpart.findAttachNodes ("bottom") [0];
rootAN.attachedPart = newPart;

I'm very happy that I was able to do this much - it demonstrates that I can programmatically add parts!

Now, to work out surface attachment (vs node attachment demonstrated above) and then math to figure out where in 3-D space to place the surface attachments. But that's for tomorrow. :)

Link to comment
Share on other sites

Hello fellow plugin makers!

I am making MP mod and im now stuck with vessels parts setting. Im beginner with KSP mods and C# so my question is probably easy to solve: How do i get parts of the vessel, and how do i set them.

public Vessel craft = FlightGlobals.ActiveVessel;
Debug.Log(craft.parts.ToString());

Just gives me

[Log]: System.Collections.Generic.List`1[Part]

on the KSP debug console. Thanks in advance for help!

Link to comment
Share on other sites

I am making MP mod and im now stuck with vessels parts setting. Im beginner with KSP mods and C# so my question is probably easy to solve: How do i get parts of the vessel, and how do i set them.

Note that there is already a working multiplayer mod:

http://forum.kerbalspaceprogram.com/threads/55835-Kmp-0-22-wip-alpha

http://www.reddit.com/r/kmp

That said, here's how to print out the parts of the ship (lifted straight from my own mod):


private void printPart(string header, Part p){
if (p==null){
print (header + ": null!");
} else {
print (header +": "+p.name.ToString () + ": " + p.uid.ToString () + "/" + p.symmetryMode.ToString ()+"/"+p.children.Count.ToString());
}
foreach (Part child in p.children) {
print ("child: "+child.name.ToString () + ": " + child.uid.ToString () + "/" + child.symmetryMode.ToString ()+"/"+child.children.Count.ToString());
}
}

private void printPartList(string title, string header, List<Part> parts){
print ("=== "+title+" ===");
foreach (Part p in parts) {
printPart(header,p);
}
}

EditorLogic editor = EditorLogic.fetch;
printPartList("All parts of ship", "Part", editor.ship.parts);

The editor.ship.parts is a List of "Part" objects. See http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx if you need help with Lists. You can access the list like an array (parts[0], parts[1]) or step through it with "foreach".

To add a part, see my sample at http://forum.kerbalspaceprogram.com/threads/7544-The-official-unoffical-help-a-fellow-plugin-developer-thread?p=764072&viewfull=1#post764072

Link to comment
Share on other sites

Note that there is already a working multiplayer mod:

http://forum.kerbalspaceprogram.com/threads/55835-Kmp-0-22-wip-alpha

http://www.reddit.com/r/kmp

That said, here's how to print out the parts of the ship (lifted straight from my own mod):


private void printPart(string header, Part p){
if (p==null){
print (header + ": null!");
} else {
print (header +": "+p.name.ToString () + ": " + p.uid.ToString () + "/" + p.symmetryMode.ToString ()+"/"+p.children.Count.ToString());
}
foreach (Part child in p.children) {
print ("child: "+child.name.ToString () + ": " + child.uid.ToString () + "/" + child.symmetryMode.ToString ()+"/"+child.children.Count.ToString());
}
}

private void printPartList(string title, string header, List<Part> parts){
print ("=== "+title+" ===");
foreach (Part p in parts) {
printPart(header,p);
}
}

EditorLogic editor = EditorLogic.fetch;
printPartList("All parts of ship", "Part", editor.ship.parts);

The editor.ship.parts is a List of "Part" objects. See http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx if you need help with Lists. You can access the list like an array (parts[0], parts[1]) or step through it with "foreach".

To add a part, see my sample at http://forum.kerbalspaceprogram.com/threads/7544-The-official-unoffical-help-a-fellow-plugin-developer-thread?p=764072&viewfull=1#post764072

I know that there is already a MP mod, but it is very unstable and i want to finish my project.

Your code doesnt help me, i need parts of the vessel while its flying, not in the editor + your editor.ship.parts gives me an error: The name 'editor' does not exist in the current context

Link to comment
Share on other sites

Im beginner with KSP mods and C# so my question is probably easy to solve: How do i get parts of the vessel, and how do i set them.

That code is perfectly correct. craft.parts is the list of parts.

You're getting that output because C#'s default List implementation of toString() gives you details of it's class not contents, if you'd wanted to print out the names of all parts you'd use something like a foreach look on the list like in hab's example.

Link to comment
Share on other sites

Would any of you guys happen to know if there's any way to hook into Part events like OnPartExplode() through a part module?

I've been digging through docs and these forums for a couple of hours to try and find something like it, but to no avail.

I realize I can create my own child class to Part and override the function that way, but that seems rather counter to the spirit of the API so I'd like to avoid it if possible.

For context, I'm trying to correct the explosive staging exploit through having such explosions set off chain reactions amongst boosters.

Link to comment
Share on other sites

Would any of you guys happen to know if there's any way to hook into Part events like OnPartExplode() through a part module?

GameEvents.onPartDie (since onPartExplode doesn't seem to provide the exploding part or position of the explosion) and GameEvents.onSplashDamage might be of interest as well.

Link to comment
Share on other sites

GameEvents.onPartDie (since onPartExplode doesn't seem to provide the exploding part or position of the explosion) and GameEvents.onSplashDamage might be of interest as well.

Hehe...yup, I figured out that bit earlier. The important part was being clued into the existence of GameEvents, which I wasn't previously aware of.

The current documentation for the API is so scattered about that finding what you need when you don't know what it's called in the first place, is the rather rough bit :)

Link to comment
Share on other sites

  • 2 weeks later...

Hello I'd like to know the basics of making plugins for KSP. I did the tutorial on the wiki using Xamarin Studio, but I don't know where to go from there. I just want to know the basic built-in functions that I can use and how I can use them to influence things in the game.

Link to comment
Share on other sites

Hello I'd like to know the basics of making plugins for KSP. I did the tutorial on the wiki using Xamarin Studio, but I don't know where to go from there. I just want to know the basic built-in functions that I can use and how I can use them to influence things in the game.

Taranis have release some incredible code example to help people start. Have a look there:

http://forum.kerbalspaceprogram.com/threads/56053-Example-plugin-projects-to-help-you-get-started

Link to comment
Share on other sites

I started making a plugin that can disable a fuel line with an action group.

To achieve that, I'd like to look at all children of my "disabler part". However, every time I try to activate the disabling funcion I get the following error in the debug log:

FileNotFoundException: Could not load file or assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

The function body looks like this:




[KSPEvent(guiName = "Deactivate Fuel Flow", guiActive = true)]
public void deactivateFuelFlow()
{
print("Fuel Controller: deactivate flow.");
foreach (Part c in this.part.children){
if (c is FuelLine) {
print("FuelLine Child found!");
((FuelLine)c).BreakLine();
}
}

}

It doesn't even print the "Fuel Controller: deactivate flow.". However it prints the debug messages that I put into the onStart method of the class.

Commenting out the line " ((FuelLine)c).BreakLine();" changes nothing at all.

I'm sure that I am missing something easy and essential. Can someone help?

Edited by dtobi
Link to comment
Share on other sites

I started making a plugin that can disable a fuel line with an action group.

To achieve that, I'd like to look at all children of my "disabler part". However, every time I try to activate the disabling funcion I get the following error in the debug log:

FileNotFoundException: Could not load file or assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

Wild guess here - do you have your references to Assembly-CSharp.dll and UnityEngine.dll set correctly when you build the DLL?

http://wiki.kerbalspaceprogram.com/wiki/Setting_up_MonoDevelop

http://wiki.kerbalspaceprogram.com/wiki/Setting_up_Visual_Studio

Do you have "using System.Collections;" in your code?

Link to comment
Share on other sites

I know that there is already a MP mod, but it is very unstable and i want to finish my project.

Your code doesnt help me, i need parts of the vessel while its flying, not in the editor + your editor.ship.parts gives me an error: The name 'editor' does not exist in the current context

You already had how to get the parts of the ship during flight; in fact you posted it here:


public Vessel craft = FlightGlobals.ActiveVessel;
Debug.Log(craft.parts.ToString());

I was just giving your some functions to print out Part objects, and lists of Part objects, in a nice way.

So instead of:


printPartList("All parts of ship", "Part", editor.ship.parts);

just do:


printPartList("All parts of ship", "Part", craft.parts);

Link to comment
Share on other sites

FileNotFoundException: Could not load file or assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

Make sure you target the .net framework 3.5. Unity does not offer 4.0+. You should be able to specify it in your project settings (on Visual Studio: Right click on project => Properties => Application => Target Framework => ".NET Framework 3.5")

Link to comment
Share on other sites

Make sure you target the .net framework 3.5. Unity does not offer 4.0+. You should be able to specify it in your project settings (on Visual Studio: Right click on project => Properties => Application => Target Framework => ".NET Framework 3.5")

I think it was the framework version. I tried to use the .net framework 3.5 with Visual Studio Express 2012 but I cannot set the target version at all. Googling didn't help. Installing .net 3.5 didn't work. That was quite frustrating. I'm now doing it with Mono and Xamarian Studio. It works but its code browser is not as good. Does anybody have some pointers how to set .net framework 3.5 in Visual Studio Express?

Thanks!

Another thing: I am looking for a tutorial or code example how to do a part that can be activated by a stage (like a parachute or engine, just not a parachute and engine ;-) ).

Can somebody help me? Searching Google and the forum died not yield any results.

Thanks, too!

Edited by UbioZur
merge posts
Link to comment
Share on other sites

Another thing: I am looking for a tutorial or code example how to do a part that can be activated by a stage (like a parachute or engine, just not a parachute and engine ;-) ).

Can somebody help me? Searching Google and the forum died not yield any results.

Thanks, too!

Check the code of the radial stack separator. It should give you a good hint.

Also try to edit you post instead of dual posting please (I have merged them).

Link to comment
Share on other sites

I'm writing a plugin that runs in the editor (VAB/SPH), has anyone figured out some kind of event/callback for parts being added/removed from the ship?

Right now, I'm updating my list of parts every frame in Update() but there's got to be a better way. :(

Link to comment
Share on other sites

GameEvents.onPartAttach should be good enough. Make sure to unbind yourself again, once your MonoBehavior is destroyed and/or check whether you are still in the editor, since it can fire in flight as well. Also not sure if it works with the fist part.

Link to comment
Share on other sites

Hi, Am trying to iterate through a vessel and work out which parts generate/consume electricity (and other resources eventually), but I've gotten stuck with a few things.

1. On an engine I think the ModuleAlternator is the object that generates the charge, but i cant find the property that tells me what the current rate per second is

2. On a solarpanel I have been looking at the ModuleDeployableSolarPanel object and can see the max rate, etc, but am a little unsure how to get the current value as well - thought it might be the powerCurve FloatCurve, but I cant seem to get a value that matches the one that is shown in the resourcepanel

Anyone got any tips/ideas on working with the resources?

Link to comment
Share on other sites

I'm writing a plugin that runs in the editor (VAB/SPH), has anyone figured out some kind of event/callback for parts being added/removed from the ship?
GameEvents.onPartAttach should be good enough.

GameEvents.onPartAttach should be good enough

That's amazing! Thanks for letting me know about the "GameEvents" class. It took me a minute to figure out how to subscribe to the events, but here it is:

GameEvents.onPartAttach.Add(OnPartAttach);

void OnPartAttach(GameEvents.HostTargetAction<Part, Part> data)
{
Debug.Log("OnPartAttach");
}

And no, it doesn't fire for the first part, but does for every additional part, and of course GameEvents.onPartRemove for the opposite.

Edited by Jyrroe
Link to comment
Share on other sites

@ Trigger Au

You can't. The games resource system works via Part.RequestResource(...), that directly updates the existing resources and do not offer any way to tap into it (though i think this is good from an performance pov). Lets imagine a modded partmodule does sth like this:

part.RequestResource( ... , UnityRandom(1, 20) );

The value actual appears to never be saved anywhere except in the final resource data. So the only way to collect this data would be to sum up all resources after every part module is executed. Not sure if sth like that would be possible, but its performance would be terrific. So yes, its basically impossible to support more than just a few modules you can build a custom detector for. ModuleAlternator should be doable, though, since they store the current rate in the parts resource "amount" property, so it can be displayed in the games resource UI. DeployableSolarPanel does expose some flowRate properties, that might what you need. If not you might have to re-run some of the modules code. Thankfully it appears some of the vars for that are accessible (have you checked what this class exposes?), especially the RaycastHit. Btw, wasn't there a very old version of that code for this module available, or do i mix that up with sth else?

Edited by Faark
Link to comment
Share on other sites

@ Trigger Au

...

Thanks for the ideas Faark. There are a few mods that look at fuel usage (eg Mechjeb, Engineer), and yeah they do that by running through all the parts and building a simulator of the vessel. More than I want to attempt :)

I did look at a bunch of the PartModule classes and their properties before posting here, was when I realised I was starting to sink not swim I thought I'd ask for help. Thanks again, I'll see whats possible without grinding KSP to a halt.

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