Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Is there a "hello world" style tutorial example of a mod out there somewhere that walks a new person through the steps?

Best thing to do is comb through other's code. That's one of the best reasons for having the open code policy on this site (of course, be careful about copy-pasta and sharing, respect licenses). If you want to check out a quick and dirty mod my source can be found at the link below. You can probably puzzle out how to display "Hello World" in a window from that code.

Then there's this: http://docs.unity3d.com/Documentation/ScriptReference/ , which is quite helpful. Specifically check out the MonoBehaviour because all plugins (not parts, not sure how to do those...) extend that class. There's an IRC chat channel #kspmodders, or something, I don't chat. There's also a forum that goes along with it: http://www.kspmodders.com/forum/ but there aren't many people there just yet. Oh, and there's this: https://github.com/Anatid/XML-Documentation-for-the-KSP-API , which has personally been an excellent reference but might be out of date, not sure.

Link to comment
Share on other sites

Best thing to do is comb through other's code. That's one of the best reasons for having the open code policy on this site (of course, be careful about copy-pasta and sharing, respect licenses). If you want to check out a quick and dirty mod my source can be found at the link below. You can probably puzzle out how to display "Hello World" in a window from that code.

Then there's this: http://docs.unity3d.com/Documentation/ScriptReference/ , which is quite helpful. Specifically check out the MonoBehaviour because all plugins (not parts, not sure how to do those...) extend that class. There's an IRC chat channel #kspmodders, or something, I don't chat. There's also a forum that goes along with it: http://www.kspmodders.com/forum/ but there aren't many people there just yet. Oh, and there's this: https://github.com/Anatid/XML-Documentation-for-the-KSP-API , which has personally been an excellent reference but might be out of date, not sure.

All those links are for what to do after it's installed. I don't even know what to install.

Ah finally: After a rather long time googling and finding other links that all assumed I already had everything set up I finally found what I was looking for here:

http://wiki.kerbalspaceprogram.com/wiki/Creating_your_first_module

Edited by Steven Mading
Link to comment
Share on other sites

I tried to post a dedicated thread, but didn't get the response I needed, so here's to trying it on this thread.

What exactly do you do with a class that extended part. I know a class that extends part module is a part module, but I need to use something in part, and I just don't really know how.

Thanks for any help,

Matrixmage

Link to comment
Share on other sites

Hi guys, could some one help me out.

I cant get it to work. Dont have the access to the script atm...

But this is what I'm trying to do (roughly):



import "all the right stuffs to get it to work";

class test extends partmodule (or something){
public contructor(){
//do some constructor stuff

//This show up in the debug/alt F2 so the main set-up works
print("welcome");

public CelestialBody celestial = new CelestialBody();
public string name;

//try one
print(celestial.name/title());

//try two
name = celestial.getTitle/Name();
print(name);
}
}

As seen above I'm trying to get the properties from a planet.

But it seems im not doing it right. The prints show that the values are either '0' or 'unnamed'.

My goal is to get the values from a planet and/or change these.

More specific the values from either the current planet I'm on or just Kerbin.

Don't have much experiance but I enjoy doing this, so a gentle shuff or pointer into the right direction would be enough.

Ill try to supply the code when available to me again.

Thanks in advance and kind regards,

Okreeks

Link to comment
Share on other sites

Hi all,

As an exercise to learn C# (among other things, eg, modeling & texturing) I've been working on making a working stargate in the evenings while watching stargate ^_^ .

While I've got it mostly figured out and coded I've run into a problem; I have no idea how to get the vector/position of a part.


part.transform.localPosition;
part.rigidbody.position;
part.transform.position;
part.localRoot.transform.position;
part.GetReferenceTransform().position

The above all seem to provide the position of the vessel, not the part. So my puddle jumpers and gate travelers all jump to the centre of the gate ship, not the gate, which causes....problems...

So what am I missing? How do I find the position of the part? Thanks in advance.

@Okreeks: I might be able to help. Looking at your example code you seem to be creating a new Celestial body but not properly intialising it and giving it a name, so your print statements are correct when they return 0 or Unnamed.

Try printing the following, it'll get the name of the celestial body whose SOI your vessle is currently in:


vessel.mainBody.name

Hope that works and you can work it out from there.

Edited by Crabsticks
Link to comment
Share on other sites

[..]

You want to call a function in Part or change a variable there? All part-modules have a reference to their part. Just use (partModule or this).part.what_ever_you_want_to_do();

[..]

Your script creates a new planet object and prints name & title of this newly created object. You did not change those properties, so ofc they are still the default values of a newly created object. You don't want to create a new object but instead get a reference to an existing planet. Check out the anatid doc linked few posts above, especially the part about FlightGlobals.

[..]

Are you sure you ruled out part.transform.position? From my understanding this should be the local offset from the parent game object. So you should be able to somehow calculate the actual pos with it. Unity should also offer helpers for that, but I'm definitively no unity expert^^

Edited by Faark
Link to comment
Share on other sites

Thank you guys. Looking back its quite obvious. xD

I'm trying to make a part that gives me a window with information on the planets. And chnge the gravity and atmosphere. Any hints towards that?

EDIT: I have to acces and change: CelestialBody: "public double gMagnitudeAtCenter;"

Some one can tell me how?

Again, thnx!

Edited by Okreeks
Link to comment
Share on other sites

Thanks Faark, I goofed; the vessel I was using to test on had the desired part as the root part which explains why the part and vessel position were the same.

@Okreeks: I've no experience with Celestial bodies but have you tried the following?


CelestialBody planet = vessel.mainBody;
planet.gMagnitudeAtCenter = x;
planet.CBUpdate ();

I have no idea if it'll work ^_^

Link to comment
Share on other sites

Afternoon all,

I'm getting a bit confused about polling for connected resources, specifically intake air. I've written a module that should be displaying the available intake air as a prelude to using this value elsewhere. In OnUpdate I have:



var resources = new List<PartResource>();
this.part.GetConnectedResources(IntakeAirID, resources);
availableAir=resources[0].amount;
Air = string.Format("{0:0.000}",availableAir);

No if I put one air intake anywhere on a rocket and a part to display the 'Air' string in a window then the value in the window tracks the IntakeAir value in the top right. However, if I have more than one air intake then it only gets the value for the first intake placed. This means that a rocket with four radial intakes will have 1.6 IntakeAir on the launchpad according to the resources readout but only 0.4 is being found by GetConnectedResources.

So how do I get the true value for the IntakeAir that is present onthe craft?

Link to comment
Share on other sites

Afternoon all,


var resources = new List<PartResource>();
this.part.GetConnectedResources(IntakeAirID, resources);
[B]availableAir=[SIZE=4]resources[0].amount[/SIZE];[/B]
Air = string.Format("{0:0.000}",availableAir);

You're only retrieving the first result in this snippet. If this is the code you're using, then you've forgotten to add all the other results in resources

Link to comment
Share on other sites

You're only retrieving the first result in this snippet. If this is the code you're using, then you've forgotten to add all the other results in resources

That's it. Many thanks. For some reason I was thinking that the list was if I wanted to poll more than one resource.

Working code for anyone in the future:


var resources = new List<PartResource>();
this.part.GetConnectedResources(IntakeAirID, resources);
availableAir=resources.Sum(r => r.amount);

Edited by Vulkan
forgot the code
Link to comment
Share on other sites

Btw, someone might find this interesting for setting up a development environment. It skips the main menu and automatically loads a specific quicksave instead. All you have to do is to add it to your mod.dll (do not forget to remove it for shipping^^) and change the SaveFolder = "...", to match your save.


[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class Debug_AutoLoadQuicksaveOnStartup: UnityEngine.MonoBehaviour
{
public static bool first = true;
public void Start()
{
if (first)
{
first = false;
HighLogic.SaveFolder = "Dev";
var game = GamePersistence.LoadGame("quicksave", HighLogic.SaveFolder, true, false);
if (game != null && game.flightState != null && game.compatible)
{
FlightDriver.StartAndFocusVessel(game, game.flightState.activeVesselIdx);
}
CheatOptions.InfiniteFuel = true;
}
}
}

Thanks for the info Faark,

Is there a way to not have it start in the SPH/VAB or even just the space center? instead of an active vessel?

If found a way

I used HighLogic.LoadScene(GameScenes.EDITOR); instead of the FlightDriver line.

Edited by UbioZur
answer my own question
Link to comment
Share on other sites

I'm trying to write a plugin that will send and receive data to KSP via a serial connection. Whenever I initialize the SerialPort though the plugin does not seem to load. If I remove the serialPort line, the debug window spews out "test" as expected. I know people have done this before, but I'm not sure how.


[KSPAddon(KSPAddon.Startup.Flight,false)]
public class PluginTutorial : MonoBehaviour
{
private SerialPort serialPort;
public void Awake() {
serialPort = new SerialPort("COM4", 9600);
}
public void Start() { }

public void Update()
{

print("test");

}
}

Link to comment
Share on other sites

A SerialPort class is not included in KSPs runtime. Jop, that sadly is no standard .net 3.5 runtime. You should be able to verify this in your KSP.log, pretty near to the beginning. I stumbled about this a few times as well, especially when using 3rd party libraries. The most promising approach is to include the source yourself, for example by extracting it from mono. Its a bunch of copy-paste work, though. You might also find other 3rd party libs... but make sure they don't need sth KSP doesn't have as well...

Link to comment
Share on other sites

Okay, I'm a Comp Sci major so I know coding, but I've never actually worked on a mod for a game before, That being said I'm taking baby steps and figuring things out one at a time. I've been looking at how ships are saved from the VAB and noticed that they save the parts which doesn't include the fuel levels. So if I wanted to have a GUI with a slider for a tank to set it to half full, I would have to take the value from the slider and save it to be called when the rocket hits the launch pad at which point it could load the fuel level %number and edit the tank to the appropriate fuel levels. From creeping on other mods source code I think that this would be done with


[KSPFIELD]
public float fuel_adjust_percent = fuel_adjust_slider_value;

And then some on load function I still have to figure out.

So, I guess any pointers would be awesome or even just let me know if that sounds somewhat correct.

Edited by Ashtoruin
Link to comment
Share on other sites

Okay, I'm a Comp Sci major so I know coding, but I've never actually worked on a mod for a game before, That being said I'm taking baby steps and figuring things out one at a time. I've been looking at how ships are saved from the VAB and noticed that they save the parts which doesn't include the fuel levels. So if I wanted to have a GUI with a slider for a tank to set it to half full, I would have to take the value from the slider and save it to be called when the rocket hits the launch pad at which point it could load the fuel level %number and edit the tank to the appropriate fuel levels. From creeping on other mods source code I think that this would be done with


[KSPFIELD]
public float fuel_adjust_percent = fuel_adjust_slider_value;

And then some on load function I still have to figure out.

So, I guess any pointers would be awesome or even just let me know if that sounds somewhat correct.

Not something I've done myself, but take a look at the TAC Fuel Balancer mod for code relating to controlling the level of fuel in fuel tanks to get you started.

The Spaceport link for the mod here.

D.

I allow setting the amount of fuel (while still prelaunch) in a part here:

https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/MainWindow.cs#L359

And here is an example of where I change it during flight:

https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/FuelBalanceController.cs#L404

vessel.parts gives you a List<Part>, part.Resources gives you a List<PartResource>, and PartResource.amount is the current amount. Also take a look at: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1005 .

Link to comment
Share on other sites

I allow setting the amount of fuel (while still prelaunch) in a part here:

https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/MainWindow.cs#L359

And here is an example of where I change it during flight:

https://github.com/taraniselsu/TacFuelBalancer/blob/master/Source/FuelBalanceController.cs#L404

vessel.parts gives you a List<Part>, part.Resources gives you a List<PartResource>, and PartResource.amount is the current amount. Also take a look at: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1005 .

Alright, I get what that's doing I think.

Is it possible to test if a resource is enabled? ie Say I want to add Kethane Support, can I check to see if Kethane is a Resource and have it return a true/false? I've been looking through the documentation, but sometimes its hard to understand what exactly its saying :/

Link to comment
Share on other sites

Alright, I get what that's doing I think.

Is it possible to test if a resource is enabled? ie Say I want to add Kethane Support, can I check to see if Kethane is a Resource and have it return a true/false? I've been looking through the documentation, but sometimes its hard to understand what exactly its saying :/

There is a PartResourceLibrary that you can ask for the ID# of a resource. You pass the name as a string, i.e. "Kethane". I forget what it does if it cannot find the resource, but I think it returns -1 instead of throwing an exception. That would tell you if the resource has been defined.

See: https://github.com/Anatid/XML-Documentation-for-the-KSP-API/blob/master/Assembly-CSharp.xml#L1746

Example: https://github.com/taraniselsu/TacLifeSupport/blob/master/Source/Settings.cs#L67

Edit: Scratch that. It is even simpler. You can get access to the PartResourceDefinitionList, which has a Contains() method:

PartResourceLibrary.Instance.resourceDefinitions.Contains("Kethane")

Not sure why it doesn't show up in the XML documentation.

Edited by TaranisElsu
Too easy
Link to comment
Share on other sites

A SerialPort class is not included in KSPs runtime. Jop, that sadly is no standard .net 3.5 runtime. You should be able to verify this in your KSP.log, pretty near to the beginning. I stumbled about this a few times as well, especially when using 3rd party libraries. The most promising approach is to include the source yourself, for example by extracting it from mono. Its a bunch of copy-paste work, though. You might also find other 3rd party libs... but make sure they don't need sth KSP doesn't have as well...

I did figure out that was the problem after moving the initialization of the SerialPort into the update function, but I have not been able to resolve it. Any chance you could give me a pointer on how I would go about getting the source from mono? Am I to basically copy the SerialPort class into my own program?

Link to comment
Share on other sites

Am I to basically copy the SerialPort class into my own program?

I don't see another way... so either that or you find a third 3rd library that does not use System.IO.Ports. Here is the SerialPort source and where you will find other missing dependencies. This appears to be their license, btw.

Link to comment
Share on other sites

So, before I headdesk any more about this--has anyone had success in programmatically overriding a part's FX?

I've looked at part.SaveEffects(ConfigNode node) [hoping then to change the configs, and then call part.LoadEffects(node)] but that didn't seem to work right--it started playing all effects.

I'd really rather not have to manually create emitters, I just want to change a part's existing FX defines.

(Disclosure: this is so engine FX can be changed based on their fuels)

Link to comment
Share on other sites

Hey Guys,

in the cfg of a part there is that line:

module = Part

can be Part, ControlSurface, Strut, Winglet

Is there a why I can access that value in game?

I couldn't find it in the stored config file from GameDatabase.Instance.GetConfigs

or any variable of Parts or AvailablePart

It looks like it's from pre .15.

Link to comment
Share on other sites

Any idea of how to hack into the ship editor? I was thinking about making some kind of economy mod (like actually add cash to the game and make use of the cost attribute of parts) but I have no idea on how to start, as there is no documentation on the VAB/SPH functionality nor does there seem to be any official api on that part of ksp.

Link to comment
Share on other sites

[..]

The part class contains a "List<FXGroup> fxGroups" field. Have you checked it out? You should be able to edit simply edit it for the prefab via PartLoader.LoadedPartsList[some_index].partPrefab.fxGroups

It looks like it's from pre .15.

Yes, back from the time when there wasn't any PartModules and part / its derived types contained all the actual behavior. It by the way looks like all those old & "dead" types are still in the released 0.21 assembly. Though those few parts you mentioned appear to still use a custom type derived from Part. So using c#'s primitive GetType() on Part should help you.

[..]

Some ideas:

- Search for editor mods and inspect their source. There are a bunch of Part Filters, for example.

- Get into KSP's internals like us others. The object browser does have quite some hits for "Editor"

- The KSP API doc does in fact contain an EditorLogic class that has an "public ShipConstruct ship;" that might be useful for you.

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