Click or drag to resize

IPartModule Interface

Interface for KSP part module.

Namespace:  KSPDev.KSPInterfaces
Assembly:  KSPDev_Utils.2.0 (in KSPDev_Utils.2.0.dll) Version: 2.0 for KSP v1.8+
Syntax
C#
public interface IPartModule
Request Example View Source

The IPartModule type exposes the following members.

Methods
  NameDescription
Public methodOnAwake
Initializes a new instance of the module on the part.
Public methodOnFixedUpdate
Notifies about a physics frame update.
Public methodOnInitialize
Called on a vessel when it's time to start the physics on it.
Public methodOnLoad
Notifies that the part's config is loaded.
Public methodOnSave
Notifies about saving module state.
Public methodOnStart
Initializes module's state after all other modules have been created.
Public methodOnStartFinished
Notifies that all the modules on the part have been started.
Public methodOnUpdate
Notifies about a frame update.
Top
Remarks

Naturally, KSP doesn't declare any part module interface (unfortunately), and all modder's modules just inherit from PartModule. This interface is introduced for the better OOP approach. It reveals methods that a regular module can override, and provides documentation for each of them.

Some methods of the module interface look familiar to the ones from Unity but they are not behaving in the same way in every scene. Moreover, not all methods get called in every scene.

In the loading scene the callbacks are executed in the following order:

OnAwake Notifies about creating new module. If it's a clone operation then all KSPField annotated fields have values from the part's config. Otherwise, all the fields are in the initial states.
OnLoad(ConfigNode) The provided config node is the original configuration from the part's definition. All the annotated fields are populated before this method gets control.
OnStart(PartModuleStartState) Is not called since the parts being created are prefabs and icon models. They are not real parts that behave on a vessel.

In the editor the callbacks are executed in the following order:

OnAwake Notifies about creating new module. If it's a clone operation then all KSPField annotated fields have values from the part's config. Otherwise, all the fields are in the initial states.

New parts in the editor are created via the clone operation. I.e. each time a part is dragged from the toolbar it's get cloned from the prefab.

OnLoad(ConfigNode)Is not called for the new parts since they are clonned. When a saved vessel is loaded in the editor every part on the vessel gets this method called with the values from the save file. The annotated fields are populated from the file before this method gets control, so it's safe to use them.
OnInitializeHard to say what it means for the edtior, but important difference from the flight scenes is that this method is called before OnStart(PartModuleStartState).
OnStart(PartModuleStartState)The code must check if the current scene is editor, and do the behavior changes as needed. In the editor parts must not have active behavior.

In the fligth scenes the callbacks are executed in the following order:

OnAwakeNotifies about creating new module. All KSPField annotated fields have initial values.
OnLoad(ConfigNode)The provided config node is the config from the save file. The annotated fields are populated from the file before this method gets control, so it's safe to use them.
OnStart(PartModuleStartState)This method is called when all parts in the vessel are created and loaded. The code must check if the current scene is flight, and do the behavior changes as needed.
OnInitializeIndicates that part should start handling physics if any. It may be called multiple times during the part's life. First time it's called when vessel is completely loaded in the secene and all parts are started. Other calls may happen when game returns from a physics suspended state (e.g. from a warp mode back to x1 time speed).

Code must check if editor scene is loaded since this method is called differently in the editor.

Examples
public class MyModule : PartModule, IPartModule {
  /// <inheritdoc/>
  public override void OnAwake() {
  }
}
See Also