Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

In other news I think I'm trying to tackle a problem that's a bit big for an noob like me to tackle.

I realized I want a list of targets for my plugin, looking at the scripts prior but can't figure out how I would use it.

Ideally I would have buttons or a roll-out and a search feature that would list targets in a select-able list...

but that's insane, I wouldn't even know where to start.

Your WIP post made it clearer what you were after. Refer to Unity GUI for related documentation. This should get you off the ground:

Rect windowRect = new Rect(120f, 120f, 300f, 500f);
Vector2 scrollPos = Vector2.zero;

void OnGUI()
{
windowRect = GUI.Window(12345, windowRect, MyWindowFunction, "Test Window");
}

void MyWindowFunction(int winid)
{
GUI.skin = HighLogic.Skin;

GUILayout.BeginVertical(GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

// center label
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
GUILayout.FlexibleSpace();
GUILayout.Label("Available Targets");
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();

// if there are a lot of vessels, it's going to make the window
// stupid huge so let's make more efficient use of the space with
// a scrolling section

scrollPos = GUILayout.BeginScrollView(scrollPos);

foreach (var vessel in FlightGlobals.Vessels)
if (GUILayout.Button(vessel.name))
{
ScreenMessages.PostScreenMessage(string.Format("Setting target to {0}", vessel.name), 3f, ScreenMessageStyle.UPPER_CENTER);
FlightGlobals.fetch.SetVesselTarget(vessel);
}

GUILayout.EndScrollView();

GUILayout.EndVertical();

GUI.DragWindow();
}

Link to comment
Share on other sites

Since we are talking about resources, how can I add one to an EVA kerbal?

I'm looking at the source for TAC life support, but I don't really understand exactly how and when he does it.

Right now I am doing this: if the active vessel is an eva, look for a part named "kerbalEVA" and add the resource. However, I always stop at the log line telling me that it couldn't find a part with that name... what am I doing wrong?

EDIT: forgot to say that I don't want the transfer to be automatic, like in TACLS, but it must happen on an event. You go to the part, activate the event, and receive your spare parts.

EDIT AGAIN: ok, so after putting debug lines everywhere, I found out the source of the problem. Even when ActiveVessel.isEVA, the ActiveVessel is not the EVA kerbal, thus the code I had posted is awfully wrong.

Actually the name of the vessel is not KerbalEVA. KerbalEVA is a PartModule. To find an EVA, you need to find a part where part.Modules.Contains("KerbalEVA") is true.

Ok, I managed to find the active EVA and add a resource to it.

However, this resource doesn't show up in the resource panel... does anybody know why this could be?

You are using PartResourceList.Add(ConfigNode) for this, right?

Edited by stupid_chris
Link to comment
Share on other sites

Actually the name of the vessel is not KerbalEVA. KerbalEVA is a PartModule. To find an EVA, you need to find a part where part.Modules.Contains("KerbalEVA") is true.

I got that idea from the code of Taranis, but then after experimenting I settled for this:

 int idx = FlightGlobals.Vessels.FindIndex(v => ((v.vesselType == VesselType.EVA) && v.isActiveVessel)); 

You are using PartResourceList.Add(ConfigNode) for this, right?

Yes, specifically this code:


if (!evaPart.Resources.Contains("SpareParts"))
{
Debug.Log("BANANA: the eva part doesn't contain spares, adding the config node");
// Create the resource node and add it to the EVA kerbal
ConfigNode node = new ConfigNode("RESOURCE");
node.AddValue("name", "SpareParts");
node.AddValue("maxAmount", DangIt.Spares.MaxEvaAmount);
node.AddValue("amount", amountTaken);
evaPart.AddResource(node);
}
else
{
Debug.Log("BANANA: the eva part already contains spares, editing the amount");
evaPart.Resources["SpareParts"].amount += amountTaken;
}

(banana is very easy to find in the log file, until squad steals my idea)

Maybe I missed some property in the config node?

Edited by Ippo
Added a piece of code
Link to comment
Share on other sites

Okay so for the past couple days I've been having a thought, and I wanted to think it aloud here before making a whole thread about it.

Procedural Fairings, NRAP, Stretchy [insert thing here]s, Procedural Parts, etc. exist. In fact there's even a mod now whose whole purpose is to let you arbitrarily resize the stock tanks and such. And I've noticed that when one installs several mods, there inevitably end up being pages and pages of parts in some or all of the tabs, many of which are practically redundant.

So I've been thinking KSP needs some kind of stock overhaul, reducing the tank selection, for instance, to only a few options (e.g. "white tank", "orange tank", "long white tank", "toroidal tank") and then having them be tweakable to any standard size. They'd change fuel capacities based on size while maintaining the same fuel mass fraction (and in the interest of Career progression, perhaps tank types with different base mass fractions and collision tolerances would unlock at different levels, e.g. oil barrels unlock early while reinforced lightweight composite fuselages come later). Something similar could perhaps be applied to engines, wings, and probe cores. This would have the advantage that, for example, PFairings has over the other fairing mods as far as having only a few parts needed, and make tons of room for mod parts without making the menu overflow as easily.

The end result of this train of thought seems to be either to ask SQUAD to overhaul their whole part selection (which would be awesome, but probably an unreasonable request), or make an epic total conversion mod that is comprehensive enough to allow users to delete all (or at least most) of their SQUAD folder if they choose. What are my fellow modders' thoughts on this matter? Has anyone already tried this? Did I miss some fatal variable?

Link to comment
Share on other sites

I got that idea from the code of Taranis, but then after experimenting I settled for this:

 int idx = FlightGlobals.Vessels.FindIndex(v => ((v.vesselType == VesselType.EVA) && v.isActiveVessel)); 

Yes, specifically this code:


if (!evaPart.Resources.Contains("SpareParts"))
{
Debug.Log("BANANA: the eva part doesn't contain spares, adding the config node");
// Create the resource node and add it to the EVA kerbal
ConfigNode node = new ConfigNode("RESOURCE");
node.AddValue("name", "SpareParts");
node.AddValue("maxAmount", DangIt.Spares.MaxEvaAmount);
node.AddValue("amount", amountTaken);
evaPart.AddResource(node);
}
else
{
Debug.Log("BANANA: the eva part already contains spares, editing the amount");
evaPart.Resources["SpareParts"].amount += amountTaken;
}

(banana is very easy to find in the log file, until squad steals my idea)

Maybe I missed some property in the config node?

You're using part.AddResource. I'd try part.Resources.Add.

Link to comment
Share on other sites

You're using part.AddResource. I'd try part.Resources.Add.

Same thing: both DO add the resource (if after that I print the value, the kerbal does contain the resource I added) but it's still not displayed in the panel.

EDIT: I swear, I have no idea what changed but now it works. I didn't really change anything, just refactored things around, and now it works. What the...? :rolleyes:

Edited by Ippo
Link to comment
Share on other sites

Same thing: both DO add the resource (if after that I print the value, the kerbal does contain the resource I added) but it's still not displayed in the panel.

It might simply be a matter of the ResourceDisplay not being told to update. Have you tried calling ResourceDisplay.Instance.Refresh() (or perhaps ClearResourceList/CreateResourceList) after adding your resource to the kerbal?

Link to comment
Share on other sites

It might simply be a matter of the ResourceDisplay not being told to update. Have you tried calling ResourceDisplay.Instance.Refresh() (or perhaps ClearResourceList/CreateResourceList) after adding your resource to the kerbal?

Thank you, this fixes the last issue that remains now. However, when I first posted it simply didn't show up, even when closing and reopening the panel.

For some reason, moving it around fixed it (most likely, moving things around I accidentally removed the bug I had put there).

Link to comment
Share on other sites

@Parameciumkid

I totally agree that the parts need an overhaul, not so much that it should be deleted, but sorted.

what is lacking subcatagories. Squad have the system ready to go but haven't implimented it.

they need to be organized into solid fuels, tanks, jets, etc etc. as its at a point where its a mess of items and 5 main tabs aren't enough.

Link to comment
Share on other sites

--snip--

Holy WOW, I would have never gotten that by myself. finding resources is one thing but how can you know something without knowing what you're looking for in the first place?

Reeper, you have my thanks.

I spent the last 3 hours fiddling with it and learning what it does (just need to apply the skin, technical reasons but when I used "GUI.skin" it actually made my GUI invisible)

Here is it in action! LINK

Link to comment
Share on other sites

so, I have a question that I can't find on the wiki tutorials. anyone know how to make a ship rotate using the modules like SAS? or even better specify a custom reaction wheel and make that give torque?

it needs to find the cannon module, and find its downward vector and align that towards the target, then update the difference in current angle/target angle and return that as a variable.

something like what Mechjeb does.

Edited by DIGI_Byte
Link to comment
Share on other sites

Okay so I've successfully made a tank with fairing caps, which is working spendidly in the VAB: it has round ends, and when I stick an engine on the bottom it becomes cylindrical.

Only problem now is that I can jettison the caps in flight, which seems dumb. Does anyone know a trick to disable ModuleJettison, or some alternate way to have toggleable mesh pieces? I'll write a plugin only if I absolutely have to.

Link to comment
Share on other sites

Okay so I've successfully made a tank with fairing caps, which is working spendidly in the VAB: it has round ends, and when I stick an engine on the bottom it becomes cylindrical.

Only problem now is that I can jettison the caps in flight, which seems dumb. Does anyone know a trick to disable ModuleJettison, or some alternate way to have toggleable mesh pieces? I'll write a plugin only if I absolutely have to.

Not sure exactly, but I did see some API stuff on the wiki, you would have to make a plugin if you want to disable it automatically. or disable it manually on your ship.

Alternatively, you could not have the module on the part itself, instead use the plugin to attach the module type when it meets certain conditions (like being active, or landed)

Read over these for a bit;

http://wiki.kerbalspaceprogram.com/wiki/API:ConfigNode & http://wiki.kerbalspaceprogram.com/wiki/API:Part

Reference: http://wiki.kerbalspaceprogram.com/wiki/Community_API_Documentation

Link to comment
Share on other sites

What is exactly the proper way to initialize my module before the flight? Right now I am doing this:


public override void OnStart(PartModule.StartState state)
{
if (state == StartState.PreLaunch)
{
this.Log("OnStart, performing initialization");
// and other stuff
}
else
{
this.Log("OnStart, not initializing");
// and other stuff
}
}

And of course, in the log I get that it's not initializing:


DangItBattery [-184916]: Entering OnStart: state is PreLaunch, Landed, enabled is True
DangItBattery [-184916]: OnStart, not initializing

Link to comment
Share on other sites

What is exactly the proper way to initialize my module before the flight?

This is what I've been using:


public override void OnStart(PartModule.StartState state)
{
if (state == StartState.Editor || state == StartState.None) return;

this.Log("OnStart, performing initialization");
// and other stuff
}

Link to comment
Share on other sites

This is what I've been using:

Nope, it doesn't do the trick for me :( I need to keep track of the part's age: if I re-do the init when state is, say, orbital or flight, I lose the recorded age.

Right now I am trying this:


if (state == (StartState.PreLaunch | StartState.Landed))

Given that StartState has the [Flags] attribute, in theory this means that it should only be true right before launching.

It is kinda true: the problem is that

1) if you launch, fly a while, land, then go back to the space center and to the ship again, you are still getting (PreLaunch | Landed)

2) when you launch a space place, the state is only Landed.

:mad:

Basically, I need to run some code when the flight starts, and never again. Maybe someone has encountered this problem before?

Edited by Ippo
Link to comment
Share on other sites

...
...

Thank you, it works :) Also thanks to PrivateFlip for suggesting the same via PM.

Now I have to understand why everything screws up when going to an inactive vessel -.-

EDIT: for the record, I found that this approach can be bugged by using the revert to launch button. You also need to store at what UniversalTime you performed the init, and if you revert to that time, re-do it.



if (HighLogic.LoadedSceneIsFlight)
{
if (Planetarium.GetUniversalTime() < timeInitted)
{
this.needsInit = true;
this.timeInitted = (float)Planetarium.GetUniversalTime() + 10;
}


if (!this.needsInit) return;


this.Log("OnStart, performing initialization");

...

Edited by Ippo
Link to comment
Share on other sites

How do I just get work done efficiently?

Ok, where I am:

I've set up Visual Studio 2012, with .Net Framework 3.5 and I'm able to copy my DLL into the GameData folder and see things like print statements. I've also been able to create right-click pop-up menus on in-flight craft by extending PartModule, etc, and I'm trying to get persistence with ConfigNodes to work.

Problem:

How to I develop efficiently? Right now I have to copy the DLL, wait for KSP to start, and find a NullReferenceException and *restart*. Slllooooowwwwwwww. It builds up to where I'm viscerally angry at every delay, like how long it takes the main menu text to float in (like one second). I mean, I don't want to be. I want to have fun.

I tried deleting a lot of parts. This speeds up the boot process, but, I notice that I have NRE's internal to KSP that prevent me from launching ships. (The first ship launches, I land it around KSC, and then the second ship won't launch. I can go back and replicate it to get the stack trace if you want, but I didn't save it.) This happens even when I'm using a fresh copy of KSP that I haven't installed my plugin into. The NREs go away if I don't delete any parts.

Is there a "blessed" lean configuration I can use? I can't justify the time I'm spending on this if I can't work for an hour and see some incremental improvement. How does everyone else handle this?

Link to comment
Share on other sites

How do I just get work done efficiently?

Ok, where I am:

I've set up Visual Studio 2012, with .Net Framework 3.5 and I'm able to copy my DLL into the GameData folder and see things like print statements. I've also been able to create right-click pop-up menus on in-flight craft by extending PartModule, etc, and I'm trying to get persistence with ConfigNodes to work.

Problem:

How to I develop efficiently? Right now I have to copy the DLL, wait for KSP to start, and find a NullReferenceException and *restart*. Slllooooowwwwwwww. It builds up to where I'm viscerally angry at every delay, like how long it takes the main menu text to float in (like one second). I mean, I don't want to be. I want to have fun.

I tried deleting a lot of parts. This speeds up the boot process, but, I notice that I have NRE's internal to KSP that prevent me from launching ships. (The first ship launches, I land it around KSC, and then the second ship won't launch. I can go back and replicate it to get the stack trace if you want, but I didn't save it.) This happens even when I'm using a fresh copy of KSP that I haven't installed my plugin into. The NREs go away if I don't delete any parts.

Is there a "blessed" lean configuration I can use? I can't justify the time I'm spending on this if I can't work for an hour and see some incremental improvement. How does everyone else handle this?

Welcome to coding KSP. You can write yourself a small KSPAddon to jump from main menu to flight, but that's about all else you can do to go faster. Apart from that, if you know some NRE's are internally to KSP, ignore them. There's a bunch floating around, like when launching a ship directly from the launchpad without going to the editor. But yes that's how we do. Code, build, copy, launch, wait, test, NRE, fix. Wash, rinse, repeat.

Link to comment
Share on other sites

How do I just get work done efficiently?

...

Theres a few things you can try, but if you are making plugins you do have to unload the game to change the dll between tests - so these are really just things to speed up the cycle. I documented up how I do stuff here: http://forum.kerbalspaceprogram.com/entries/1253-An-Adventure-in-Plugin-Coding-3-A-Fast-Dev-Environment-I-hope and it works for me without any issues launching craft.

See how you go... of and I feel your pain

- - - Updated - - -

Welcome to coding KSP. You can write yourself a small KSPAddon to jump from main menu to flight, but that's about all else you can do to go faster. Apart from that, if you know some NRE's are internally to KSP, ignore them. There's a bunch floating around, like when launching a ship directly from the launchpad without going to the editor. But yes that's how we do. Code, build, copy, launch, wait, test, NRE, fix. Wash, rinse, repeat.

ah, washing and rinsing... I was missing those steps :)

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