API:Part

From Kerbal Space Program Wiki
Revision as of 18:21, 29 December 2012 by RolanDecoy (talk | contribs)
Jump to: navigation, search

Part Class

A Part is the C# object representing a single part on a vessel. You can subclass Part, creating a new type of part that users can place on their ships. The C# class that controls a part is specified in that part's "part.cfg" file by the line "module = <Part subclass>" where <Part subclass> is the name of your new Part subclass. You can also create PartModules, which add to the behavior of an existing part. From within a PartModule, access the Part you are attached to via the "part" field.

If you build a plugin by subclassing Part, you will want to override some of the Part methods. See API:Part class overrides.

Below are some of the interesting members of the Part class. Note that the list of members is far from complete.

Properties

The following are public properties available in Part.

Signature Description
BaseEventList Events { get; } A list of the events that the Part will respond to. For example, landing legs deploy in response to certain events.
bool isAttached { get; set; }
bool isAttached { get; set; }
bool isControllable { get; }
Orbit orbit { get; } Don't use this!! Use vessel.orbit instead.
UnityEngine.RigidBody rigidbody { get; } This object lets you do physics to your part: for example, apply forces to it. Check out the Unity documentation on RigidBody.
PartStates state { get; } Whether the Part is IDLE (not activated yet), ACTIVATED (e.g., an activated engine), DEACTIVATED, or DEAD.

Fields

The following is a list of publically-accessible fields in Part. This includes protected items, which can be accessed by subclasses.

Signature Description
List<Part> children A list of all the Parts attached to this Part (except this Part's parent, i.e., the part this part was attached to).
int inverseStage The stage in which this Part will activate.
float mass The mass of this Part.
float maxTemp The temperature at which this Part will explode.
Callback onEditorAttach Add your own function to this Callback to have it called when this Part is attached to another part in the VAB. The Part object is created when the user clicks on a part in the VAB part selection menu. When that happens, you can set up this Callback to have something happen when the part gets attached to the ship.
Callback onEditorDetach Like onEditorAttach, but the Callback gets invoked when the part is pulled off the ship in the VAB.
Callback onEditorDestroy Like onEditorAttach, but the Callback gets invoked when the part is deleted in the VAB.
Part parent The Part that this Part is attached to.
Part.PhysicalSignificance physicalSignficance Some parts have no physics: that is, their masses and drags don't actually get added to the ship. For these parts, physicalSignificance == Part.PhysicalSignificance.NONE. For regular parts it's equal to Part.PhysicalSignificance.FULL.
float temperature The current temperature of the part.
Vessel vessel The vessel of which this Part is a part. Note that vessel==null in the VAB.

Methods

The following are methods included in Part.

Signature Description
bool activate(int currentStage)
void deactivate() Deactivates the part.
void decouple(float breakForce) Decouples the part from it's parent part. breakForce is the force it will be pushed away with.
void disconnect(bool controlledSeparation)
void explode() Calling this method makes the part explode.
void force_activate() Activates the part.
void onXXXXX() There are many functions of the form onXXXXX() that you can override in a Part subclass in order to respond to various circumstances. See API:Part class overrides.
void propagateControlUpdate(FlightCtrlState st) This probably lets you give the Part flight input commands (pitch, yaw, roll, etc.) as if they came from the user. See the API:FlightCtrlState class.
bool RequestFuel(Part source, float amount, uint reqId,) Asks the part for a given amount of fuel. This will drain the requested amount of fuel either from the Part you call this on, or from some other part(s) that this part can draw fuel from. You must pass in a fuel request ID generated by the static funtion Part.getFuelReqId(). This function returns true if it was able to fulfill the fuel request. One way you can use this function is to request 0 fuel, and use the return value to test whether the part has access to fuel (it will return false if it has nowhere to draw fuel from).
bool RequestRCS(float amount, int earliestStage) Presumably this is used to request RCS fuel.
float RequestResource(string resourceName, float demand) Requests a part resource. Returns the provided amount of the resource.
float RequestResource(int resourceID, float demand) Same as above, but takes the hashcode of the resource name.
void SendEvent(string eventName) Sends an event to this part. You can find out what events this part responds to by looking at the Events property. Events seem to be propagated throughout the part tree, so to send an event to the whole ship you can probably just send it to the root part of the vessel.


It has been suggested that this page or section be merged into API:Part_class_overrides. (Discuss)