Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Game Architecture

From Kerbal Space Program 2 Wiki

The Sim and the View[edit | edit source]

The game is divided into two conceptual layers, the "sim" and the "view." The sim layer contains all objects that exist anywhere in the game, visible or not. The view is only objects close enough to the player to be visible. Both layers can be access globally under the current `GameInstance`

When an object is created, it must first be created in the sim. The sim persists data and is what is stored in a save game. Generally, the sim layer does _not_ involve any Unity data (`GameObject`, `MonoBehaviour`, etc). The main object for managing the sim is `UniverseModel`.

The two layers are tied together by `SpaceSimulation`, which binds sim and view objects together, provides mappings between them, and provides common operations such as spawning vessels.

  • GameManager.Instance.Game.SpaceSimulation
  • GameManager.Instance.Game.UniverseModel
  • GameManager.Instance.Game.UniverseView

Sim objects rougly mimic how Unity objects work. That is, there is a base object type `SimulationObjectModel` that represents a "blank slate" object, and then the object must be decorated with one or more `Component` classes to be able to do anything.

When a view object is needed, a corresponding `SimulationObjectView` (`KerbalMonoBehaviour`) is created and populated with `Behavior`s that correspond to the sim object. View objects are unity `GameObject`s that connect the sim layer to the Unity game engine.

Common Components
Sim Component Name View Behavior Name Description
CelestialBodyComponent CelestialBodyBehavior Represent stars and planets. Provides data such as atmospheric drag, gravity, ocean levels, buoyancy data, PQS data, and terrain colliders.
VesselComponent VesselBehavior Vessel (or EVA'ing kerbal) global data, such as target, aggregate mass information, etc.
PartComponent PartBehavior Data about individual parts. Fuel storage, kerbals on board, etc. Parts are normally owned by a vessel, but can be free floating (orphaned) such as debris or discard fairings/decouplers.
PartOwnerComponent PartOwnerBehavior A part owner has a 1:1 relationship with `Vessel` and is a list of parts owned (1:many) by that vessel. Data about individual parts (mass, direction, rotation, etc) is aggregated by the Owner.
RigidbodyComponent RigidbodyBehavior represents physical properties (torque, thrust, inertia, etc) of a part or vessel. Vessel rigidbodies represent averages or other aggregation (eg, moment of inertia) of all of the part rigidbodies owned by the vessel.

Accessing a view object from a sim object[edit | edit source]

A view object (if it exists) can always be looked up using a global dictionary ModelViewMap in SpaceSimulation.

SimulationObjectView simulationObjectView = Game?.SpaceSimulation?.ModelViewMap.FromModel(simulationObject);

Note, some components have convenience accessors that act as a shortcut to getting the view object (if it exists.) For example, any sim object that is a vessel (I.E. it has a VesselComponent) and has an active VesselBehavior should have objVesselBehavior set and pointing to the corresponding VesselBehavior.

Accessing a sim object from a view object[edit | edit source]

SimulationObjectView has a property called Model that always points to the corresponding sim object.

Game update order[edit | edit source]

At the top level, game updates are driven by the Unity game loop update order. However, most updates in the game are not driven directly by Unity. Instead, most view objects register for updates from GameInstance, and most sim object register for updates from SpaceSimulation. SpaceSimulation registers its self with GameInstance to receive updates, and in turn drives updates for all sim objects.

Objects registering for updates from GameInstance must implement at least one of IFixedUpdate, IUpdate, or ILateUpdate interfaces. They may optionally implement IPriorityOverride to control the order in which they are called with respect to other updates.

Updates are run in ascending order of priority (lowest to highest).

Component IPriorityOverride IFixedUpdate? IUpdate? ILateUpdate? other Sub-process
UniverseModel 0
WaterInitPerFrameAction 0
OceanUpdateAction 1
PhysicsSpaceProvider 1
ClearWaterDepthAction 2
FloatingOrigin 2
HandOfKraken 2
DrawLandBasedWaterAction 3
RigidbodyBehavior (vessels) 3
RigidbodyBehavior (parts) 4
KerbalBehavior 5
SpaceSimulation 6 IViewActionQueue UniverseModel.UpdateSimObjects() (unordered)
CelestialBodyProxy 10
PartBehavior 40 WaterManager.IWaterDetectObject
PartBehaviourModule 50
ViewController 50
VesselBehavior 150
UniverseView 500
TimeWarp 1000
DrawOceanAction 2001
WaitWaterDetectionAction 2002
MediaWiki Appliance - Powered by TurnKey Linux