Jump to content

Beginning mod maker and developer, requesting assistance on first plugin code


Recommended Posts

Hi everyone! I'm new to C# (though I studied Java in the past) and KSP mod making; and I'm unfortunately stuck already! I've got a simple goal in mind to start me off:

I've created a "PartKannister" part which uses the Kerbal Attachment System module "KASModuleContainer" in the .cfg to allow storage of extra solar panels, batteries, etc. This function works fine.

I've also setup a simple door-opening animation which uses the built-in "ModuleAnimateGeneric" to allow you to toggle the doors on the launchpad.

Both of these functions work currently, but there are two separate action buttons in the right-click menu and of course I'd like to tie the actions together, such that when a Kerbal on EVA accesses the contents the doors will open, and when you close the container GUI the doors would then close.

It seems like I need to write a plugin to override the "ViewContents" action for KAS for this part. I've tried looking into how the stock Materials Bay and Goo Cannister work, as the functionality of these parts is similar to what I hope to accomplish, but have run into dead ends here as well.

I'm sure an experienced KSP mod developer will find this task laughably simple (at least that is my hope!) so please forgive my nubbish post. Any pointers or suggestions on where to look for further inspiration on this is greatly appreciated!

Heck if someone could even just suggest the correct logic to accomplish this in a plugin I will work out the code needed! I just need to make sure I'm at least following the right track here.

Edited by al.bebop
Link to comment
Share on other sites

You really don't need KASModuleContainer. I just finished a part similar to what you describe. The animation opens and closes the door in the VAB/SPH (requires FSanimateGeneric in firespitter.dll). I then store parts within it. Each part has the KAS module in its part.cfg file (or you can just add it to the the KAS addModule.cfg file). While in flight, a Kerbal can open the container to view and retrieve the parts.

IMPORTANT: the collision mesh (node_collider) must extend inside the container and must be continuous.

No additional plugin needed.

part.cfg contains:


MODULE
{
name = FSanimateGeneric
animationName = doors
startEventGUIName = Open
endEventGUIName = Close
toggleActionName = Toggle
availableInEVA = True
EVArange = 10
}

Note that this contains only one context menu button which automatically toggles "Open" and "Close" in the menu . This functionality is the same for ModuleAnimateGeneric and FSanimateGeneric. You do NOT need two buttons defined in Unity.

Each of the parts you see in there (batteries, solar panel, etc.) can be retrieved by Kerbal on EVA.

xJt2N1k.jpg

This is a cargo container I created for SpeedyB's Hex Truss System (a great mod; check it out).

Edited by Apollo13
Link to comment
Share on other sites

snippy snip

Very cool stuff! Thanks for this; I'll definitely consider implementing this same technique for my part; with a little re-working it will definitely work as a small "miscellaneous science/utility" part container. Two questions about the part you've shown above (very cool btw!):

1) Regarding the collision mesh, do you mean it should fill the entire internal space? (because your screenshot seems to negate that, unless the animation plugin works around that) or is it instead a "sleeve" that occupies the same space as the walls of the container, with a void in the middle where the cargo/kerbals fit above? If you can provide a SS or further elaboration I'd greatly appreciate it!

2) Do you know if this implementation shields internal items from Deadly Reentry and FAR effects or does that require an additional module? Thanks and great work once again!

edit: Remembered what #2 was :)

Edited by al.bebop
Link to comment
Share on other sites

I think I figured it all out, except for the reentry/FAR shielding, but I know there's another module for that available. For the collision mesh, I actually omitted the separate mesh normally used for the collider in Unity, and added a Mesh Collider component to the model parts instead, seems to work! Thanks again for the help!

Link to comment
Share on other sites

I've run into a problem before when trying to make cargo bays that even if you follow the shape of the hull with the collider the missing side gets 'filled in' when you check the 'convex' button in unity... and if you DON'T check the convex button in Unity bad things can happen... from your part not loading, to the whole screen going black when physics activates, to your part sinking into the ground for no good reason.

The way I solved this was to use multiple collision meshes in the model - one for each wall of the cargo bay. That way each wall can be convex while the overall shape is still concave.

Turning the actual model on as a 'mesh collider' works fine with really simple models, but constantly checking every face of a complicated collision mesh puts a lot of load on the CPU. So if you can aproximate your mesh's shape with less polygons, it's worth it to try. I think 256 poly's is a hard limit for collision mesh's... but I haven't tried that, I'm just repeating what I've read here on the forum. Could be more of a rule than a guideline?

One easy Blender trick that frequently works... If you duplicate the model (in object mode) and then apply the Decimation modifier, you can wiggle the settings until Blender automatically creates a low poly mesh that mostly keeps your part's shape. :)

Apollo13, that cargo carrier piece is really awesome! Have you released it yet?

And al.bebop, if I were you, I'd look at the KAS code and see if there's a way to add the functionality you need into it, and then request a pull and let them add your changes back to the main codebase, as I can see how that would be a really cool feature for KAS to just have. An optional KASModuleContainer parameter to include an animation, so you don't have to use a separate animation module at all? (You could look at how firespitter's landing gear module includes an animation without a dedicated animation module.)

Or if you want the simple answer... you could always just do it with two parts -an outer bay that has an animation module and an inner container with the KAS module. With well-placed nodes the inner container could snap in place in the VAB easily, and then it all just works. :)

Link to comment
Share on other sites

Hey guys,

Sorry I haven't been back to this thread to answer earlier.

The collision mesh actually follows the inside of the bay. I tried the technique of creating a collision mesh for each face inside, but found it too laborious. Each of the walls of the bay has a thickness. So, I just duplicated the entire bay (without the doors) (SHIFT-D in Blender), and that became the collision mesh. Quick and easy.

To make ANY part KAS compliant, so you can remove it and place it on another vehicle, add this code to that part: (change the parameters to suit your needs).

MODULE

{

name = KASModuleGrab

evaPartPos = (0.0, 0.0, -0.15)

evaPartDir = (0,0,-1)

storable = true

storedSize = 2

attachOnPart = True

attachOnEva = True

attachOnStatic = False

}

If you want to make any part into a KAS container and behave like it, just add this to the part.cfg:

MODULE

{

name = KASModuleContainer

maxSize = 40

}

That will call the KAS.dll. Of course, you can't distro KAS.dll with your mod part. Just let the user know it's needed.

Thanks for the compliment on the JebEx part. I'm releasing it as part of SpeedyB's incredible Hex Truss System. JebEx can be found at: JebEx Cargo Bay. License is the same as SpeedyB's Hex Truss System

JebEx works with FAR. I don't use Deadly Reentry, so I'm not certain of the effects.

Edited by Apollo13
Link to comment
Share on other sites

  • 2 weeks later...

Thanks to both of you for the continued discussion! I particularly appreciate the info regarding collision meshes; I think I was able to use a low-poly version of the model in this case for the collision mesh, with animations. And regarding KAS container animations, KospY himself actually responded to my inquiry to confirm he intends to include an animation component in the container module for the next version, with exactly the functionality I was asking for XD. Still, your discussion is invaluable as it's helped me to look at creating solutions from multiple approaches rather than just getting frustrated when the first plan hits a snag.

Link to comment
Share on other sites

Thanks again! If you happen to check back here and see this; are you able to confirm what cases not setting the "convex" to active will break things? I probably shouldn't be monkeying around with it since everyone seems to say the same as you, but I've found it doesn't always seem to cause problems when I don't do this; though I'll grant none of my parts have been really rigorously tested, just enough to make sure nothing explodes on the launchpad or on its way to orbit.

Link to comment
Share on other sites

my 2 cents, maybe someone finds it usefull..

from what I can tell, convex does only one thing different then "normal" mesh collider:

It prevents the object from falling through the ground/terrain..

So in case of your box, you'd need only some seperated corner peaces as convex and the rest could be a simple mesh-collider that can easely be concave without problems..

just eight square boxes, one at each corner, would be minimum..

if you land on a hilltop, it can look awekward if the hill shifts through a large wall peaces, so just slap some flat square colliders in the center of each side..

they don't need to span the whole area - just 1 sqare meter in the middle.

I've made complete star trek ships with several decks and had only 8 or 9 tiny convex colliders at logical points to stay above terrain

mesh collider work for ship2ship collisions and for EVA as advertized - no issues..

I'm not sure if this is the best way to go - but that is what I figured as working in the last few weeks..

Link to comment
Share on other sites

Lol so that's why people can't use my stanford torus as a gigantic wheel :).

As for the plugin itself. If you add a reference to the original plugin you can use it as the module you inherit settings from.

In visual studio under your project right click the references folder and click "Add Reference", then find the .dll from KAS.

In your c# you do this for your module class to "inherit" methods and features from the KASModuleContainer module.


public class MyPartContainer : KASModuleContainer
{
//define things...
}

Then you overload the events that you need. In visual studio (even the express versions) you can right click the KASModuleContainer after you've added it and go to definition. From there you can see all the events they added and find which ones you want to override. Play your animation in the overridden event, then call the base method. Be sure to play the animation in reverse or add a new close animation when you close the container. This is how I did my resource science system.



[KspEvent(...)] //replace the ellipsis with the parameters you need
new void OpenContainer() //the new part is necessary so you can intercept the event
{
animate(OpenContainerAnimation); //you can write this function yourself
base.OpenContainer(); //call the base method to invoke the original module's code
}

Edited by michaelhester07
Link to comment
Share on other sites

Thanks so much for the continued replies, you guys are awesome! If I understand you right Michael, I think my problem was trying to use the "override" in the function declaration; I guess the "override" function is only for adding to the base game functions? Sorry if I'm messing up the terminology here, and thanks again for the insight into C#! I hope to get back to seriously studying it for the purposes of mod-making soon!

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