Jump to content

Plugin for "all parts" on ship?


Recommended Posts

Hey there.

I am looking to add a plug in that adds a resource tank to all parts on the ship.

With a capacity equal to their mass.

Ideally, these would be invisible except on a few parts that I build.

Why?

I am working on an anti-mass set of parts, and while they work well on their own - it only works for the parts that have access to the negative-mass resource. This is why I need the plug-in to add it to all the parts on my ship.

Ideally, not every part would show this tank - and would simply work when used.

Thanks,

- A

Link to comment
Share on other sites

If I understand you correctly you want a plugin not associated with any one part - if this is the case there's a (attribute? - sorry new to C#) like below

[KSPAddon(KSPAddon.Startup.Instantly, true)]

public class ModuleTestAddon : MonoBehaviour

The "Startup" controls where/when your plugin runs

Edit: On re reading your post I think I may have been answering a different question - how to make a partless plugin. If you have a plugin module on a ship it can iterate over the parts. The question would be where to put it so that it would load everything up when the ship became active - OnStart is one place you might check. You'd also need to cover cases where a part was added - the GameEvents callbacks would be my guess for that.

Edited by wasml
Link to comment
Share on other sites

If this would only be for some ships and not others my though would be to have a part you add to the ships you want to change. The part could be very small if needed. This part could then hook OnStart and add the resource by iterating over the ship when it's loaded.

BTW recently found an example to add a resource - if you haven't found it yet:

part.Resources.list.Clear(); // Only if you want to get rid of current resources

ConfigNode newConfigNode = new ConfigNode("RESOURCE");

newConfigNode.AddValue("name", "ResourceName"); // Needs to be a defined resource name

newConfigNode.AddValue("maxAmount", "3200");

newConfigNode.AddValue("amount", 0.0f);

envelopeResource = part.AddResource(newConfigNode);

If your adding this to every ship then you might look into using a partless plugin and watching for ships loading - not sure what the options are as I haven't tried this before.

PS: You might want to look into OnStart before using - I think this is the correct place to put this type of code but no guarantees.

Edited by wasml
Link to comment
Share on other sites

The resource adding I don't know much about, but one option for adding this to the parts of a ship dynamically, and only when the ship is in flight (thus, not the parts in the editor, just the flight scene) would be to create a module that inherits "VesselModule" instead of "PartModule" and then iterate through all of the parts on the vessel through something through the interal part list "Vessel.Parts" to add a resource to all those parts.

Another option might be to create two "PartModule" modules, one which would contain the code necessary to add the other one to every part on the ship. You would use MM to add the first module to a part that you tend to use on every ship (I would have it add the module to all parts that have a command/probe ability). You then set up the first module to, upon flight scene initialization, add a component to every part on the ship (the component is the other module which would then add the resource and self-remove itself) which does not already contain the resource required.

Parameters such as ship mass, and the mass of currently-loaded resources and such should be easy enough to find. I believe the part's mass is actually something the "Part" class keeps track of. Resource information would then need to be looked up via something like "GetComponents<Resource>()" (no guarantees that this little bit of code is even valid, you'll have to do some research) and taking stock of the total capacity vs. the currently filled amount. This would allow you to find the mass of the part itself minus the added mass of the resources currently present.

If I was in my right mind (dealing with a head cold, and it's 4:00 or so in the morning) I would quickly paste in some working code to get you started, but I'm not nearly awake enough to even start coding right now. Just thinking out loud.

What's the end result here? Are you working on some new and amazing mod that no one knows they need yet, but will want it when they know about it?

Edited by Gaalidas
Link to comment
Share on other sites

What's the end result here? Are you working on some new and amazing mod that no one knows they need yet, but will want it when they know about it?

I am making a set of anti-mass parts.

And so far they work great!.... except it only reduces the mass of parts I can get my anti-mass resource into.

Which is why I want to just put that resource (starting at 0, tweak = false) on every part.

And why the capacity of the resource needs to be equal to the mass of the part.

http://forum.kerbalspaceprogram.com/threads/135861-Making-Anti-Mass-Parts-%28help%29?p=2262166&viewfull=1#post2262166

Link to comment
Share on other sites

Well, it shouldn't be too difficult really. As long as it doesn't mess up the module index of any parts the user is using in a flight, it should be pretty smooth simply adding the resource to all the parts at startup using MM. However, if you want a more reliable way of detecting the mass of a part, as it is when the flight is happening and possibly including updates to said mass due to fuel usage and whatnot, then using a part module to track the part mass would be a better option, and if you want to avoid adding that module to all parts using MM then you'll want to start off with a vessel module that adds the part module as a component to all the parts on a vessel when in flight. The component part module would then handle the adding of the resource and maintaining the resource amount which is then evaluated by whatever module you have for making the anti-mass stuff work. Sounds funky enough to be awesome, so I can't wait to see the results. If i feel industrious tomorrow, perhaps I'll write out some preliminary code as an example of what I'm talking about, but I can't make any promises yet.

Link to comment
Share on other sites

Okay, here is a very basic, and untested example for you.

[COLOR=#008000][B]using[/B][/COLOR] System;
[COLOR=#008000][B]using[/B][/COLOR] System.Linq;
[COLOR=#008000][B]using[/B][/COLOR] UnityEngine;

[COLOR=#008000][B]namespace[/B][/COLOR] your_name_space_here
{
[COLOR=#008000][COLOR=#808080]///[/COLOR] [COLOR=#808080]<[B]summary[/B]>[/COLOR][/COLOR]
[COLOR=#008000][COLOR=#808080]///[/COLOR] Creates a new module that will attach itself to any vessels that are launched, or created via detatchment or undocking.[/COLOR]
[COLOR=#008000][COLOR=#808080]///[/COLOR] [COLOR=#808080]<[B]/[/B][B]summary[/B]>[/COLOR][/COLOR]
[COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#ff0000]class[/COLOR] [COLOR=#004085]TestModule[/COLOR] : [COLOR=#004085]VesselModule[/COLOR]
{
[COLOR=#008000][COLOR=#808080]///[/COLOR] [COLOR=#808080]<[B]summary[/B]>[/COLOR]The current vessel this module is attached to.[COLOR=#808080]<[B]/[/B][B]summary[/B]>[/COLOR][/COLOR]
[COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#004085]Vessel[/COLOR] [I]thisVessel[/I];

[COLOR=#0000ff][B]public[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]OnStart[/B][/COLOR]()
{
[I]thisVessel[/I] = [COLOR=#191970][B]GetComponent[/B][/COLOR]<[COLOR=#004085]Vessel[/COLOR]>();
[COLOR=#008000]// The parts of the vessel are contained in a list within KSP that you can reference via "thisVessel.Parts"[/COLOR]
[COLOR=#008000]// which takes the form of a "List<Part>" data type.[/COLOR]

[COLOR=#0000ff][B]foreach[/B][/COLOR] ([COLOR=#004085]Part[/COLOR] part [COLOR=#0000ff][B]in[/B][/COLOR] [I]thisVessel[/I].Parts)
{
[COLOR=#008000]// I have put a few useful items into their proper data types below for use in a formatted string.[/COLOR]

[COLOR=#ff0000]string[/COLOR] partname = part.name;
[COLOR=#ff0000][B]float[/B][/COLOR] partdrymass = part.[I]mass[/I];
[COLOR=#ff0000][B]float[/B][/COLOR] partwetmass = part.[COLOR=#191970][B]GetResourceMass[/B][/COLOR]();
[COLOR=#ff0000][B]float[/B][/COLOR] parttotalmass = partdrymass + partwetmass;

[COLOR=#008000]// Here I am searching for every part in the list and spewing out the part mass to the log.[/COLOR]
[COLOR=#008000]// I do not recommend running this in the game itself, but it should work if you choose to do so.[/COLOR]
[COLOR=#004085]Debug[/COLOR].[COLOR=#191970][B]Log[/B][/COLOR]([COLOR=#ff0000]string[/COLOR].[COLOR=#191970][B]Format[/B][/COLOR]([COLOR=#0000ff]"{0} has a dry mass of {1} and a resource-mass of {2} for a total mass of {3}"[/COLOR], partname, partdrymass, partwetmass, parttotalmass));
}
}
}
}

Hope it helps. I'm pressed for time right now, but feel free to inquire further if you need help. I'm not the greatest of the modders here, but I can, at least, point you in a good direction.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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