Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

I offer the following question to the coding oracle:

Where can I initialize and destroy a TCPListener reliably, so its only active while I\'m flying a rocket.

At the moment I\'m doing this (link to other post). In short I\'m overriding OnPartUpdate and check every tick whether my Listener is null. I hav no idea where to close down the socket yet.

Thanks for your help.

You can be assured that:

OnPartStart is always called before any other operations.

OnFlightStart is always called when the part get in the flight scene. Here is a good place to create your TCPListener.

OnPartDestroy is always called before your part gets removed from the game. It may have been deactivated, disconnected, packed or exploded before it gets here, but it always does. I recommend destroying your TCPListener here, and checking if the part is still usable before doing anything.

Link to comment
Share on other sites

Hi all,

Does anyone know how to modify the active ship\'s position? I have spent many hours trying to do this without success. The Vessel class doesn\'t seem to store a position, only an Orbit, and I have tried modifying that without any success. This seems like it should be trivial but I haven\'t managed it yet.

Given the lack of docs and the rules not to look inside the box, are there some pointers to existing plugin sources that are known to use acceptable APIs? In C/C++ you\'d have header files, in Java you\'d have interfaces -- I don\'t understand what the combination of 'how does that work in C#' and 'what is allowed by the developers' is.

Thanks!

You can browse through all the classes and methods available to you in the KSP .dlls using an IDE; I am using Microsoft Visual C# as described here. The class and method names are often informative enough that you can guess what they do. That is what everyone\'s work is based on so far. If you want to see what other people have figured out based on this you can go the to project releases forum and look for plugins, which should always come with source code. You can also look at the example code page here: http://kspwiki.nexisonline.net/wiki/Module_code_examples . I am trying to get more people to contribute to this wiki page so if you figure out how to do something, please post your code there!

Link to comment
Share on other sites

I\'m just talking about the list of exported class names / method names / parameters (which is all the VC# object browser shows) -- basically what I\'d find in the header file in a C/C++ project. It sounded like the concern was about decompilation and the like and I certainly understand that being considered off-limits, but if I misunderstood, apologies. I\'m happy to stand by whatever rules the developers and moderators want here.

It would definitely be helpful (for avoiding breakage if nothing else) if there was a list of which classes were considered part of the 'public' addon API as it were -- presumably Part and some other stuff, which you have to extend to implement new part behaviours. I assume that everything is subject to change as it\'s early yet and things are in heavy development, but guidelines are always helpful.

EDIT: I\'m blind. There are pointers to some getting started wiki articles (setting up VC#, building a basic do-nothing plugin, etc) in the Posting Rules stickied post.

EDIT2: Deleted some posts asking about things clearly pointed out in the rules thread or the wiki pages it links to. Sorry about the noise!

Link to comment
Share on other sites

I\'m just talking about the list of exported class names / method names / parameters (which is all the VC# object browser shows)

Well, I was talking about the same...

It would definitely be helpful (for avoiding breakage if nothing else) if there was a list of which classes were considered part of the 'public' addon API as it were.

Yes, it would. And it has already been requested several times. We have to wait.

and make a good use of the classes that we\'ve already... sh, I\'m not speaking that :D

Link to comment
Share on other sites

hey anyone know how i can find the number of parts below and above my part as well as their combined weight and other stuff

just cant find the functions needed

edit: found a way :)

used FlightGlobals.ActiveVessel.parts.Count() in a for loop

Link to comment
Share on other sites

Umm, kinda hard to start without any guidelines.

How do you \'connect\' plugin with a part?

How to make a \'global\' plugin, that won\'t need parts at all?

And, how the hell start with this thing? I\'ve got an empty template, and completly no idea what to do. :)

Link to comment
Share on other sites

hey tosh, would you mind explaining me how to set the direction the spotlights point to? I\'m looking at your code but i can\'t figure it out

It simply shines along the \'forward\' direction (positive Z) of its parent GameObject.
Link to comment
Share on other sites

hey anyone know how i can find the number of parts below and above my part as well as their combined weight and other stuff

just cant find the functions needed

You can check out here for an example of recursively scanning through the part tree of the ship. It\'s not too hard to modify that code to total up the mass of parts under a given part (just use part.mass to get the mass of a given part).

Link to comment
Share on other sites

I see some crew management objects and functions and was wondering if anyone has been able to monitor a crewman\'s emotional state?

Also, does anyone know how to play an mp3 or wav audio clip in unity?

I had the idea of making a comm system where you can hear their squeals, giggles, or screams depending on the crew\'s mental health and maybe add some for other events in the flight as well.

Link to comment
Share on other sites

C# newbie, Is there a valid way to do this?


if(FlightGlobals.Vessels.Exists(vessel.GetInstanceID(selVesselID)){
}

I want to search the Vessels list by instanceID, but the instanceID isn\'t a public value, you get it by calling a function. I\'ve got it working by looping through all vessels, setting a variable to the ID, and checking that against selVesselID. It seems like it should be one line of code though.

Link to comment
Share on other sites

C# newbie, Is there a valid way to do this?


if(FlightGlobals.Vessels.Exists(vessel.GetInstanceID(selVesselID)){
}

I want to search the Vessels list by instanceID, but the instanceID isn\'t a public value, you get it by calling a function. I\'ve got it working by looping through all vessels, setting a variable to the ID, and checking that against selVesselID. It seems like it should be one line of code though.

The list of vessels isn\'t a dictionary, so no. You\'ll have to use a foreach loop to see if it\'s there.
Link to comment
Share on other sites

I\'m working on a test plugin that has me launch a ship with my part on it into orbit; afterwards, I switch to another ship and launch it so its trajectory intersects with the part-ship. I\'m trying to make something happen when my ship is within a certain distance of my part-ship, but it appears that onActiveUpdate is not run for inactive ships (in hindsight, this should be pretty obvious...) Is there a way to have a part that\'s not in active control still run code?

Link to comment
Share on other sites

GUI question:

I have this code:


if(computerRunning != GUILayout.Toggle(computerRunning, 'Throttle', GUI.skin.GetStyle('Button')))
{
if(computerRunning) stopComputer();
else startComputer();
}

That works fine for turning the computer on and off with the toggle button.

But if I turn the computer on or off by another means (in this case a key press), the toggle button doesn\'t get updated (and other weird behaviour).

Does anyone know what I should be doing instead?

Link to comment
Share on other sites

GUI question:

I have this code:


if(computerRunning != GUILayout.Toggle(computerRunning, 'Throttle', GUI.skin.GetStyle('Button')))
{
if(computerRunning) stopComputer();
else startComputer();
}

That works fine for turning the computer on and off with the toggle button.

But if I turn the computer on or off by another means (in this case a key press), the toggle button doesn\'t get updated (and other weird behaviour).

Does anyone know what I should be doing instead?

The Toggle will have the state you are passing in computerRunning when you run this code... Are you updating the variable correctly?

Link to comment
Share on other sites

The Toggle will have the state you are passing in computerRunning when you run this code... Are you updating the variable correctly?

The variable is definitely updating correctly, as it\'s the same variable being used to determine whether or not to take control of the throttle.

What\'s happening:

Game starts, GUI loads, toggle not set (correct behaviour).

I press the input key. The computer takes over control of the throttle (correct behaviour), but the toggle remains unset (incorrect behaviour).

Or, I can click on the toggle, it sets, and the computer takes over throttle control (correct behaviour). Ditto for turning it off.

What\'s also odd is that I can click on the toggle, but pressing the input key won\'t turn the computer off. If I then click on the toggle again (setting the toggle back to the off), the computer continues running until I press the input key a second time. Ditto for the other way around (input key first, toggle second).

It\'s as if there are two separate [tt]computerRunning[/tt] variables, and to determine whether or not to control the throttle, the program checks [tt]if (computerRunning1 || computerRunning 2)[/tt].

Link to comment
Share on other sites

The variable is definitely updating correctly, as it\'s the same variable being used to determine whether or not to take control of the throttle.

What\'s happening:

Game starts, GUI loads, toggle not set (correct behaviour).

I press the input key. The computer takes over control of the throttle (correct behaviour), but the toggle remains unset (incorrect behaviour).

Or, I can click on the toggle, it sets, and the computer takes over throttle control (correct behaviour). Ditto for turning it off.

What\'s also odd is that I can click on the toggle, but pressing the input key won\'t turn the computer off. If I then click on the toggle again (setting the toggle back to the off), the computer continues running until I press the input key a second time. Ditto for the other way around (input key first, toggle second).

It\'s as if there are two separate [tt]computerRunning[/tt] variables, and to determine whether or not to control the throttle, the program checks [tt]if (computerRunning1 || computerRunning 2)[/tt].

Sounds like the problem is on the key side, if it can\'t turn it off after you turned it on with the GUI... Can I see the key handling code?

Link to comment
Share on other sites

Its really hard to say what\'s going on without actually seeing your code. But I\'ll cover some of the most common cases I can think of.

First, I would make sure you\'re using the right key press method.

UnityEngine.Input.GetKey() - This will fire every frame the key is held down. While good at throttles and brakes, its bad for toggles. You can toggle things on and off 20 times by the time you release the key. This essentially checks if the key is 'held'

UnityEngine.Input.GeyKeyDown() or Input.GetKeyUp() - These will only fire once per individual keypress. Keydown will activate once the key changes from being up to down. Key up, simply works in reverse. These are what I use for toggling an action or something like a computer ;)

The other thing I would check is variable scope. If computerRunning is declared in the class, and then again in a function. You will indeed have two variables with different values depending on when you call the variable.

Link to comment
Share on other sites

Thanks for the suggestions, what\'s really really odd, is after posting the other day, I shut down KSP to do some work. When I came back to it today, it was working correctly! I swear I haven\'t touched the code!

Maybe the real problem was I forgot to copy over the correct dll or something equally stupid. Sorry for bugging you!

Link to comment
Share on other sites

UnityEngine.Input.GeyKeyDown() or Input.GetKeyUp() - These will only fire once per individual keypress. Keydown will activate once the key changes from being up to down. Key up, simply works in reverse. These are what I use for toggling an action or something like a computer ;)

I\'ll add that code using Input.GeyKeyDown() or Input.GetKeyUp() should be at the Update methods else than the FixedUpdate methods, or you will miss some of the events.

Link to comment
Share on other sites

I\'ll add that code using Input.GeyKeyDown() or Input.GetKeyUp() should be at the Update methods else than the FixedUpdate methods, or you will miss some of the events.

...and one more :): Update method is called for all the parts in scope, not only for active vessel\'s ones. So you might want to test vessel.isActiveVessel before processing keypresses.
Link to comment
Share on other sites

Is there any example of a model with child objects for the collider?

I\'m working on a Solar Panel Part that unfolds in multiple segments. The DAE has an attachpoint mesh which has a panel1 child, which has a panel2 child, etc. There\'s also a node_collider mesh with panel1, panel2, etc children. The game recognizes the node_collider and pulls it out (taking the children with), but the game is rendering the collider children, which is kinda weird.

Link to comment
Share on other sites

Is there any example of a model with child objects for the collider?

I\'m working on a Solar Panel Part that unfolds in multiple segments. The DAE has an attachpoint mesh which has a panel1 child, which has a panel2 child, etc. There\'s also a node_collider mesh with panel1, panel2, etc children. The game recognizes the node_collider and pulls it out (taking the children with), but the game is rendering the collider children, which is kinda weird.

The game supports only one node_collider (LanderLeg being the only exception). If you wish your part to have several collideable meshes you should create MeshCollider objects for them at run-time.

I.e. in OnPartStart you\'ll need to find all the additional colliders\' objects and hide them (renderer.enabled = false), then at some moment (I still don\'t know the right place for doing that) create MeshColliders child to that invisible objects and based on the same mesh vertices. Don\'t forget to destroy these additional colliders in OnPack.

See my Cart plugin for an example (I\'m using WheelColliders but the principle is the same). Note that carts explode on flight restart :); that\'s because I don\'t know when to create additional physics objects (comments are in code).

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