Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

The first hurdle in any project is knowing what you want to do. Until you can work that out, Hello World! examples are really the best thing we can advise you to follow.

For example, my very first plugin was an animation partmodule. Sure, it was terrible, but I didn't care. I wanted a cargo bay on my shuttle, there wasn't any mods out there with a cargo bay for my shuttle, so come Hell or high water, I'm going to make a cargo bay for my shuttle. And thus, [Plugin][Part][0.16] Animated Cargo Bay was born.

If you want to know what functions a plugin can use, open up the Object Browser. Basically anything that is public or protected is free for plugin makers to make use of.

Thanks so much for your help. I know exactly what i want to do i am just unsure how to achieve it.

i have recently found a possible way to get the rest of what i need by using some of the source code of the Kethane mod (http://forum.kerbalspaceprogram.com/showthread.php/23979) and yes i am currently seeking their permission to do so.

But i still need to find a way to add the solar panel's sunlight detection and variable output (relative to the sun exposure) to a module. and preferably the auto rotation toward the sun too.

do you know where i can find scripts for these functions? or if the source code for the base modules is available somewhere?

EDIT: actually i probably just need a way to have the part detect facing the sun and output a variable 0 (not facing) to 1 (directly facing, unobstructed) and i can do the rest with maths. anyone know how to do that?

Edited by tomwillp
more detail
Link to comment
Share on other sites

In that case, I personally would just tell people to just live with it. However, if I really wanted to do that I would:

  1. Make a list of all the fields I would need to read from a part.cfg to configure my partmodule
  2. At runtime, dynamically add the ModuleAnimateGeneric and ModuleGenerator partmodules to every part (via looping through PartLoader.LoadedPartsList[].partPrefab.Modules) which has added my new partmodule, while making sure I don't add duplicates
  3. Go through all the actions on the newly added partmodules and prevent them from showing up in the GUI. I think setting BaseAction.active = false should do the trick, not sure though
  4. Added a new KSPAction which does nothing but re-enable the disabled Toggle actions, fire them, then disable again
  5. Repeat the previous step for every other group of actions (Enable and Disable).

There's probably more to it, but that's your job to find out, since this is your project and not mine.

Thank you very much Echo 8 ÉRÀ. This will help me tremendously. I will see how I go with this start. Thank you very much again.

Link to comment
Share on other sites

Is there a way to reload a .dll file without restarting KSP?

I thought the Reload All button would do that, but it does not seem like it does reload.

What I have done is to move the loaded dll from my folder under GameData\Flueworks, and into a temporary folder outside of KSP, before copying the new dll where the old one was.

But it does not seem to work.

When I press Reload all this is the interesting part:

[ERR 22:26:41.900] AssemblyLoader: Already initialized

[LOG 22:26:41.999] Load(Assembly): Flueworks/KspTestPlugin

[LOG 22:26:42.000] AssemblyLoader: Loading assembly at C:\Users\Flue\Documents\my games\Kerbal Space Program 21 - Dev\GameData\Flueworks\KspTestPlugin.dll

[LOG 22:26:42.001] AssemblyLoader: Loading assembly 'C:\Users\Flue\Documents\my games\Kerbal Space Program 21 - Dev\GameData\Flueworks\KspTestPlugin.dll'

[LOG 22:26:42.032] Config(PART) Flueworks/Antenna/part/flueAntenna

Anything I'm missing? Or is it just not possible without having to restart KSP every single time?

If not, is it possible to make a shortcut to start KSP in VAB, or with a craft on the launchpad?

Link to comment
Share on other sites

Does any one know how to write a script for a plugin to make a part know if it is exposed to the sun and rotated to face the sun. then output a value from 0.0 to 1.0 accordingly.

Please i have tried anything i can think of, without this the whole basis of my mod is moot.

Can anyone help? thanks.

Link to comment
Share on other sites

Does any one know how to write a script for a plugin to make a part know if it is exposed to the sun and rotated to face the sun. then output a value from 0.0 to 1.0 accordingly.

Please i have tried anything i can think of, without this the whole basis of my mod is moot.

Can anyone help? thanks.

Get part world position. Get Sun world position. Ray cast from part to sun. If anything was hit, the part is in shadow, otherwise apply the various factors to charging rate (angle between charging surface's normal and the direction to the sun, distance from sun, atmospheric effects, etc.).

Link to comment
Share on other sites

On a slightly similar vein, does this kind of thing exist in Xamarin Studio (aka MonoDevelop)? I've noticed that the code completer is EXCELLENT at parsing the available libraries (much better than CodeBlocks that I use at work), but does an Object Browser exist? I bet I've missed it in some really obvious place :D

Yeah, double click the assembly in the resources folder in the project and the assembly browser will open.

I don't know how to give it any other functions as i don't know what scripts the other functions use.

Neither does anyone else, it's just trial and error. Poke around in the public variables and methods signatures in your IDE's assembly browser and try the ones with the most likely names. Some of the basic stuff is documented in the unity documentation, that'll help with getting relative positions of things for example.

So figured I would try to make a module that would combine the ModuleAnimateGeneric and ModuleGenerator functions that are activated with a single activate option in game.

You generally want to do as little as possible. In this case you can make a module that you add in addition to those two that disables their events showing up (guiActive = false) and adds one of it's own that calls both their methods.

part.Modules holds the list of the PartModules on a part, and for each of those partModules their Event variables holds the KSPEvent attribute properties.

Link to comment
Share on other sites

Neither does anyone else, it's just trial and error. Poke around in the public variables and methods signatures in your IDE's assembly browser and try the ones with the most likely names. Some of the basic stuff is documented in the unity documentation, that'll help with getting relative positions of things for example.

Thanks, and can you send a link to the unity doc'' as i cant find it, thanks again.

Get part world position. Get Sun world position. Ray cast from part to sun. If anything was hit, the part is in shadow, otherwise apply the various factors to charging rate (angle between charging surface's normal and the direction to the sun, distance from sun, atmospheric effects, etc.).

Ok. Thanks. and i dont know how to do any of that... after 5 hours of attempting to write a usable code none of it would have worked and the "best" (last i did) i could do was;

public class TestModule : PartModule

{

public class sunexposure

{

public float partposition = get.partworldposition;

public float sunposition = get.sunworldposition;

public RaycastTest if true return(?)

}

}

and i know enough to know that is basically useless, as am i :(

so if you have the penitence could you show me how to "get" those values and how to run a "raycasttest" and Thanks so much for all your help so far.

and honestly do you think i should just throw in the towel? i came with some knowledge of LUA (form modding SupCom & FA) which wasn't easy for me to learn. and i just didn't expect C# to be as different as it is. and unlike SupCom i can find very little in the way of reference material.

And this goes to anyone. do you think i should scrap my project and just post it as a suggestion on the forum?

Edited by tomwillp
Link to comment
Share on other sites

Honestly, my suggestion is to stop trying to make mods for now and get started on C# tutorials. I suspected earlier, with your constant references to "scripts" (which is technically a more advanced concept in the context of C#), but it's quite clear now you don't know C#. While it is certainly possible to learn C# and KSP modding at the same time, you're just making it harder for yourself, and we can't exactly hold your hand through every step. If we did, we might as well make the mod ourselves and publish it under our own names.

Link to comment
Share on other sites

I need help on fuel balance;

i made an horizontal fuel tank and i need to know how i can consume my fuel verticaly instead of horizontaly.

I tried with these commands :

NoCrossFeedNodeKey = bottom

NoCrossFeedNodeKey = top

NoCrossFeedNodeKey = side

Not working at all.

Link to comment
Share on other sites

Thanks, and can you send a link to the unity doc'' as i cant find it, thanks again.

http://docs.unity3d.com/Documentation/ScriptReference/index.html

i came with some knowledge of LUA (form modding SupCom & FA) which wasn't easy for me to learn. and i just didn't expect C# to be as different as it is. and unlike SupCom i can find very little in the way of reference material.

It is LUA that's the oddity unfortunately. They'll probably be more reference material later on, remember the game is still in development and hasn't reached full release stage yet.

Some basic knowledge of C# is useful to begin with. I hate to say 'go off and read a tutorial' but you do need the basic knowledge of how the language is structured. Trying to nest classes like that, or access one particular copy of an object through it's class is fundamental language structure even before you get to the KSP stuff.

For part position PartModule maintains a reference to the Part it's related to in the part variable, so part.transform.position will give you the position of the part.

For the sun position you need to find something that gives you access to the sun object. In this case the FlightGlobals class maintains a static list of the celestialbodies in the game. Because this list is static (i.e. not associated with a particular copy of an object) you can access it directly through the class name - FlightGlobals.Bodies

Link to comment
Share on other sites

WOW thats a LOT to read. it'll take me a while. thanks

It is LUA

really? wow i am very bad at this stuff :(

Some basic knowledge of C# is useful to begin with. I hate to say 'go off and read a tutorial' but you do need the basic knowledge of how the language is structured. Trying to nest classes like that, or access one particular copy of an object through it's class is fundamental language structure even before you get to the KSP stuff.

For part position PartModule maintains a reference to the Part it's related to in the part variable, so part.transform.position will give you the position of the part.

For the sun position you need to find something that gives you access to the sun object. In this case the FlightGlobals class maintains a static list of the celestialbodies in the game. Because this list is static (i.e. not associated with a particular copy of an object) you can access it directly through the class name - FlightGlobals.Bodies

Thanks so much for your advice I'll keep at it for a week or maybe a month. in failing that i think i'll just cut and run.

as for the basic knowledge, i thought i had it with lua. maybe it's because they were written for different programs, or maybe i forgot more than i thought i did, or maybe i was never any good to begin with... (probably that) but this is stuff sooooooo far above my head :(

even though i will read some more tutorials and simply try some more i think my project and i are dentin to crash and burn ;.;

Thanks so much to this forum and it's community, and all the help you have been.

Edited by tomwillp
Link to comment
Share on other sites

WOW thats a LOT to read. it'll take me a while. thanks

Many of them won't be any use as that's for the whole unity engine (i.e. building an entire game on it, not just a mod), I'd just use it as a reference to look up the classes you come across. There are a few like Transform and GameObject that you'll use quite a lot but many you won't touch.

Thanks so much for your advice I'll keep at it for a week or maybe a month. in failing that i think i'll just cut and run.

as for the basic knowledge, i thought i had it with lua. maybe it's because they were written for different programs

You've got a lot of the required knowledge, there are just some elements that a more general purpose language like C# has to have as well as the general quirks that makes languages different.

You don't need to go into C# very deeply. You generally won't even be using the standard libraries much (with a few exceptions, collections - various structures similar LUA's tables - are very useful), you just need to know how to structure you code - where each bit goes relative to each other.

Link to comment
Share on other sites

I'm wondering if anyone has a better way to check and see if RCS and SAS are active on the current vessel? I'm using this for now:


//SAS flag
if (FlightInputHandler.state.killRot) GUI.DrawTextureWithTexCoords(new Rect(720 * Scale, 11 * Scale, 88 * Scale, 31 * Scale), Resources.HUD_singles, new Rect(0.3464f, 0.5833f, 0.2162f, 0.3229f));
//RCS flag
if (!FlightInputHandler.RCSLock) GUI.DrawTextureWithTexCoords(new Rect(255 * Scale, 11 * Scale, 86 * Scale, 31 * Scale), Resources.HUD_singles, new Rect(0.1220f, 0.5833f, 0.2113f, 0.3229f));

And it works great, except that if the vessel loads with SAS or RCS on, then I get true when toggled off instead of on. In effect, they are reversed. Nothing else that I've looked at seems to be any more reliable (and most are worse). Thanks.

Edited by Trueborn
Formatting.
Link to comment
Share on other sites

I wonder if someone could point me in the right direction using assets in unity. I want to create a mesh in the game (note not a part) that is not part of the vessel. How can I access the assets that are part of the game? I had thought I could use PrefabUtility.FindPrefabRoot to get at a preface for a Game Object, but it does not seem to be available to me as a reference. How can I just access a full list of all the assets that are part of the game.

On related thought, how can I add assets of my own to the game? (again not new parts) Can I export them from unity as a .unitypackage? If so where do I put it, and what do I call to use it?

Link to comment
Share on other sites

Hi, I'm interested in editing an existing modded part to have a clickable crew access point (hatch). I have no experience in Unity and programming in general so, is there a quick and dirty crash course guide that I can use to cobble something together?

Link to comment
Share on other sites

Hi, I'm interested in editing an existing modded part to have a clickable crew access point (hatch). I have no experience in Unity and programming in general so, is there a quick and dirty crash course guide that I can use to cobble something together?

There's a tutorial here, but I'm not sure how up-to-date it is. Hopefully it can get you started at least.

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;
}
}
}

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.

Great Tip Faark, thanks.

Link to comment
Share on other sites

I am wondering if someone can help me with finding out how to throttle up and down individual engines. I am aware of this

FlightInputHandler.state.mainThrottle = 0;

but it does all of the engines. I would like to know if there is a way of iterating through each engine and modifying the throttle of each one.

Link to comment
Share on other sites

Probably these have been asked before, but i cannot find it anywhere:

- Is there a class to represent celestial-body-relative positions (latitude/longitude/altitude/parent body)?

- I want to render a line between two such positions. I have created a Gameobject and added a LineRenderer component to it. How do i convert the positions into a unity coordinates Vector3 object?

Link to comment
Share on other sites

@mojo: i think you can set it via ModuleEngines.currentThrottle (0-1). But you have to make the part uncontrollable via part.isControllable = false, or the throttle set by you will be overwritten. ModuleEngine.throttleLocked appears to set the throttle to 100%, so you may want to make sure its false as well.

@Jodel: You haven't much looked around in existing mods or the Anatid Doc? There are a lot of body specific positions all over those objects, since thats what the games uses to e.g. calculate orbits. vessel.latitude, etc might be what you want. Also you basically want for two objects who already exist in the screen? Have you tried using a position properties somewhere within those GameObjects (rigidbody or transform).

Link to comment
Share on other sites

@Jodel: You haven't much looked around in existing mods or the Anatid Doc? There are a lot of body specific positions all over those objects, since thats what the games uses to e.g. calculate orbits. vessel.latitude, etc might be what you want. Also you basically want for two objects who already exist in the screen? Have you tried using a position properties somewhere within those GameObjects (rigidbody or transform).

Actually, i have looked into other mods sources, and i am seeing Transforms, Quaternions, Vectors, CelestialBodies and such all over the place (so much that it is quite confusing). I already knew that a Vessel has latitude,longitude and altitude. I was specifically asking how one could convert these into x-y-z coordinates for rendering.

Thanks for pointing out the Anatid doc!

Link to comment
Share on other sites

I already knew that a Vessel has latitude,longitude and altitude. I was specifically asking how one could convert these into x-y-z coordinates for rendering.

Well, if you have a Vessel reference, to get it's world position, you don't need to do anything with latitude and longitude. You can simply get the coords from Vessel.CoM (center of mass) or even Vessel.transform.position if you don't need fine resolution.

If you want to get world coords from latitude, longitude and altitude in reference to whatever CelestialBody is the mainBody of your object you can get the coords from mainBody.GetWorldSurfacePosition(latitude, longitude, altitude).

Link to comment
Share on other sites

thanks a lot, that worked!

Next :) Is there a way to get notified when the game is saved/the persistence file is written?

Also, i have added the Docs to the KSP_Data/Managed folder, but the object browser does not show the additional information (i have restarted Visual Studio already).

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