Skip to content

An overview of the lifecycle of KSP modules

Swamp-Ig edited this page May 7, 2014 · 4 revisions

Parts contain two things - the logic for the part itself, plus a number of modules. Most part functionality is coded in modules, which is useful as it enables mixing together of functionality. For example, all engine and fuel tank functionality are defined in modules in stock KSP. This enables you to create SRBs, which are a combination of both an engine and a tank.

KSP loads parts in a few phases:

IMMEDIATLY

During this phase all the .cfg files, including those for PART files are loaded up by the game. After this, any KSPAddon modules that are declared as being run during this phase are run. This includes module manager, which gives it a chance to edit the loaded .cfg files

The LOADING scene

This is when the Squad monkey is shown up on the screen.

For each module defined within a part, KSP will:

  1. Construct the object and the modules within it
  2. Call OnAwake
  3. Call OnLoad, passing in the ConfigNode for all the MODULEs as defined in the PART config
  4. Then call GetInfo on each one
  5. Store the constructed part in the PartLoader class, within the AvailablePart object.

The stored part is then cloned and turned into an icon for display in the VAB. If you want to fiddle with how the icon looks, do it within GetInfo.

Adding a part in the editor (either VAB or SPH)

  1. Clone the part that is the result from above
  2. Call OnInitialize on each module - this is called in the same order as they appear in the stored part
  3. Call OnStart on each module, again in the same order

There's no chance for extra modules to sneak in here, since it's a direct clone and there's no loading.

Once the part is actually created and running, one could call AddModule on the part to add in extra modules at any stage. This will add modules at the end of the list.

You then go an attach parts to your ship - but how that happens is outside the scope of this document.

Saving a ship (or a subassembly)

The same process is used to save ships in every scene - both in the editors and in flight mode. For every part on the ship, and for each module in the part (in order):

  1. OnSave gets called to save the data for the part.

This is generally only the data that can change over time for the part - its state. For example an engine will store its current throttle setting and a docking clamp will store if it is connected.

Loading a ship

Both in the editor (for saved ships and subassemblies) and in flight mode the same process occurs:

  1. The part is cloned from the stored part in the LOADING scene
  2. OnLoad is called with the persistent state from the save file for each module in the same order as in the stored part
Clone this wiki locally