Extending kRPC#

The kRPC Architecture#

kRPC consists of two components: a server and a client. The server plugin runs inside the game. It provides a collection of procedures that clients can run. These procedures are arranged in groups called services to keep things organized. It also provides an in-game user interface that can be used to start/stop the server, change settings and monitor active clients.

Clients run outside of the game. This gives you the freedom to run scripts in whatever environment you want. A client communicates with the server to run procedures using one of the supported Communication Protocols. kRPC comes with several client libraries that implement one of these protocols, making it easier to write programs in these languages.

kRPC comes with a collection of standard functionality for interacting with the game. This provides procedures for things like getting flight/orbital data and controlling a vessel. Services are pluggable, and a new one is written using the Service API. Dropping a new service DLL into the mod directory will cause kRPC to automatically load it, and provide its functionality to clients.

Example Service#

Third party mods can add functionality to kRPC using the Service API. This is done by adding attributes to your own classes, methods and properties to make them visible through the server. When the kRPC server starts, it scans all the assemblies loaded by the game, looking for classes, methods and properties with these attributes.

The following example implements a service that can control the throttle and staging of the active vessel. To add this to the server, compile the code and place the DLL in the mod directory.

using KRPC.Service;
using KRPC.Service.Attributes;
using KSP.UI.Screens;

namespace LaunchControl
{
    /// <summary>
    /// Service for staging vessels and controlling their throttle.
    /// </summary>
    [KRPCService (GameScene = GameScene.Flight)]
    public static class LaunchControl
    {
        /// <summary>
        /// The current throttle setting for the active vessel,
        /// between 0 and 1.
        /// </summary>
        [KRPCProperty]
        public static float Throttle {
            get { return FlightInputHandler.state.mainThrottle; }
            set { FlightInputHandler.state.mainThrottle = value; }
        }

        /// <summary>
        /// Activate the next stage in the vessel.
        /// </summary>
        [KRPCProcedure]
        public static void ActivateStage ()
        {
            StageManager.ActivateNextStage ();
        }
    }
}

The following example shows how this service can then be used from a python client:

import krpc

conn = krpc.connect()
conn.launch_control.throttle = 1
conn.launch_control.activate_stage()

The Python and Lua clients pick up new or changed services automatically when they connect. The other clients need stubs, code generated from the service assembly. See clientgen for how to generate them.

Service API Reference#

The following pages cover each part of the Service API:

In-Game Tests covers testing a service against a live copy of the game.