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
Revision as of 12:02, 13 September 2024 by Foonix (talk | contribs)

The Sim and the View

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

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

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

Game update order

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.

MediaWiki Appliance - Powered by TurnKey Linux