Attributes#

The following C# attributes can be used to add functionality to the kRPC server.

KRPCService (string Name, uint Id = 0, KRPC.Service.GameScene GameScene = KRPC.Service.GameScene.All)#
Parameters:
  • Name – Optional name for the service. If omitted, the service name is set to the name of the class this attribute is applied to.

  • Id – Optional integer identifier for the service, used by clients that call procedures by id rather than by name. If omitted or set to 0, an identifier is generated from a hash of the service name. Identifiers 1 to 255 are reserved for the services that ship with kRPC. Set it only to resolve a clash with another service.

  • GameScene – The game scenes in which the services procedures are available. Defaults to all game scenes. If this is set to KRPC.Service.GameScene.Inherit the service will be available in all game scenes.

This attribute is applied to a static class, to indicate that all methods, properties and classes declared within it are part of the the same service. The name of the service is set to the name of the class, or – if present – the Name parameter. One class declares each service. Service names are unique, compared case-insensitively.

The type to which this attribute is applied must satisfy the following criteria:

  • The type must be a class.

  • The class must be public static.

  • The name of the class, or the Name parameter if specified, must be a valid kRPC identifier.

  • The class must not be declared within another class that has the KRPCService attribute. Nesting of services is not permitted.

Services are configured to be available in specific game scenes via the GameScene parameter. If the GameScene parameter is not specified, the service is available in any scene. If a procedure is called when the service is not available, it will throw an exception.

Examples

  • Declare a service called EVA:

    [KRPCService]
    public static class EVA {
        // ...
    }
    
  • Declare a service called MyEVAService (different to the name of the class):

    [KRPCService (Name = "MyEVAService")]
    public static class EVA {
        // ...
    }
    
  • Declare a service called FlightTools that is only available during the Flight game scene:

    [KRPCService (GameScene = GameScene.Flight)]
    public static class FlightTools {
        // ...
    }
    
KRPCProcedure (bool Nullable = false, KRPC.Service.GameScene GameScene = KRPC.Service.GameScene.Inherit, uint Id = 0)#
Parameters:
  • Nullable – Whether the return value of the procedure can be null. Defaults to false.

  • GameScene – The game scenes in which the procedure is available. Defaults to inherit this setting from the service the procedure is defined in.

  • Id – Optional integer identifier for the procedure, used by clients that call procedures by id. If omitted or set to 0, an identifier is generated from a hash of the procedure name. Set it only to resolve a clash with another procedure in the service.

This attribute is applied to static methods, to add them to the server as procedures.

The method to which this attribute is applied must satisfy the following criteria:

If the procedure might return a null value, the Nullable parameter of the attribute must be set to true. A parameter that may be null must likewise be made nullable. See Null Values.

Example

The following defines a service called EVA with a PlantFlag procedure that takes a name and an optional description, and returns a Flag object.

[KRPCService]
public static class EVA {
    [KRPCProcedure]
    public static Flag PlantFlag (string name, string description = "")
    {
        throw new NotImplementedException ();
    }
}

This can be called from a python client as follows:

import krpc

conn = krpc.connect()
flag = conn.eva.plant_flag("Landing Site", "One small step for Kerbal-kind")
KRPCClass (string Service, KRPC.Service.GameScene GameScene = KRPC.Service.GameScene.Inherit, uint Id = 0)#
Parameters:
  • Service – Optional name of the service to add this class to. If omitted, the class is added to the service that contains its definition.

  • GameScene – The game scenes in which the class’ methods and properties are available. Defaults to inherit this setting from the service the class is defined in.

  • Id – Optional integer identifier for the class. If omitted or set to 0, an identifier is generated from a hash of the class name. The classes, enumerations, structures and exceptions of a service share one set of identifiers. Set it only to resolve a clash with another type of the service.

This attribute is applied to non-static classes. It adds the class to the server, so that references to instances of the class can be passed between client and server.

The classes, enumerations, structures and exceptions of a service have unique names, compared case-insensitively. Their names also differ from the name of the service, ignoring case.

A KRPCClass must be part of a service, just like a KRPCProcedure. However, it would be restrictive if the class had to be declared as a nested class inside a class with the KRPCService attribute. Therefore, a KRPCClass can be declared outside of any service if it has the Service parameter set to the name of the service that it is part of. That service must be declared by a KRPCService.

A class’ methods and properties can be configured to be available in specific game scenes via the GameScene parameter on the class. If the GameScene parameter is not specified, the class’ methods and properties are available in the same game scenes as the service the class is defined in. Individual class methods and properties can override this setting.

The class to which this attribute is applied must satisfy the following criteria:

  • The class must be public and not static.

  • The name of the class must be a valid kRPC identifier.

  • The class must either be declared inside a class that is a KRPCService, or have its Service parameter set to the name of the service it is part of.

Examples

  • Declare a class called Flag in the EVA service:

    [KRPCService]
    public static class EVA {
        [KRPCClass]
        public class Flag {
            // ...
        }
    }
    
  • Declare a class called Flag, without nesting the class definition in a service class:

    [KRPCClass (Service = "EVA")]
    public class Flag {
        // ...
    }
    
KRPCMethod (bool Nullable = false, KRPC.Service.GameScene GameScene = KRPC.Service.GameScene.Inherit, uint Id = 0)#
Parameters:
  • Nullable – Whether the return value of the procedure can be null. Defaults to false.

  • GameScene – The game scenes in which the method is available. Defaults to inherit this setting from the class the method is defined in.

  • Id – Optional integer identifier for the method, as for KRPCProcedure. On a method that a subclass inherits, it applies only to the class that declares the method. See KRPCProcedureId.

This attribute is applied to methods inside a KRPCClass. This allows a client to call methods on an instance, or static methods in the class. It can also be applied to a C# extension method, adding a method to a class belonging to another service; see Extending Other Services.

The method to which this attribute is applied must satisfy the following criteria:

  • The method must be public.

  • The name of the method must be a valid kRPC identifier, unique in its scope.

  • The method must be declared in a KRPCClass, or be a public static extension method of one.

  • The parameter types and return type must be types that kRPC can serialize.

  • Parameters can have default arguments.

If the method might return a null value, the Nullable parameter of the attribute must be set to true. A parameter that may be null must likewise be made nullable. See Null Values.

Example

Declare a Remove method in the Flag class:

[KRPCClass (Service = "EVA")]
public class Flag {
    [KRPCMethod]
    public void Remove()
    {
        // ...
    }
}
KRPCProperty (bool Nullable = false, KRPC.Service.GameScene GameScene = KRPC.Service.GameScene.Inherit, uint GetterId = 0, uint SetterId = 0)#
Parameters:
  • Nullable – Whether the property can be null. This makes both the getter’s return value and the setter’s argument nullable, and on a field of a KRPCStruct it makes the field nullable. Defaults to false.

  • GameScene – The game scenes in which the property is available. Defaults to inherit this setting from the class the property is defined in.

  • GetterId, SetterId – Optional integer identifiers for the getter and the setter, as for KRPCProcedure, and applying only to the class that declares the property. Each can only be set if the property has that getter or setter. A field of a KRPCStruct sets neither.

This attribute is applied to class properties, and comes in three flavors:

  1. Applied to static properties in a KRPCService. In this case, the property must satisfy the following criteria:

    • Must be public static and have at least one publicly accessible getter or setter.

    • The name of the property must be a valid kRPC identifier, unique in its scope.

    • Must be declared inside a KRPCService.

  2. Applied to non-static properties in a KRPCClass. In this case, the property must satisfy the following criteria:

    • Must be public and not static, and have at least one publicly accessible getter or setter.

    • The name of the property must be a valid kRPC identifier, unique in its scope.

    • Must be declared inside a KRPCClass.

  3. Applied to a public static extension method of a KRPCClass, adding a property to a class belonging to another service. C# has no extension properties, so the getter and setter are a pair of methods; see Extending Other Services.

If the property getter might return a null value, or the setter should accept null, the Nullable parameter of the attribute must be set to true. A null write to a property that is not nullable fails with an error. See Null Values.

Examples

  • Applied to a static property in a service:

    [KRPCService]
    public static class EVA {
        [KRPCProperty]
        public static Flag LastFlag
        {
            get { throw new NotImplementedException (); }
        }
    }
    

    This property can be accessed from a python client as follows:

    import krpc
    
    conn = krpc.connect()
    flag = conn.eva.last_flag
    
  • Applied to a non-static property in a class:

    [KRPCClass (Service = "EVA")]
    public class Flag {
        [KRPCProperty]
        public string Name { get; set; }
    
        [KRPCProperty]
        public string Description { get; set; }
    }
    
KRPCProcedureId (string Member, uint Id = 0, uint GetterId = 0, uint SetterId = 0)#
Parameters:
  • Member – The name of the inherited method or property. Pass it with nameof so the compiler checks it.

  • Id – The integer identifier for a method.

  • GetterId, SetterId – The integer identifiers for the getter and the setter of a property.

This attribute is applied to a KRPCClass. It sets the ids of a member the class inherits from a base class, as KRPCMethod and KRPCProperty do for a member the class declares. It can be applied once per member.

Example

The Flag class inherits the Label property, and sets the id of its getter:

[KRPCClass (Service = "EVA")]
public class Marker {
    [KRPCProperty]
    public string Label { get; set; }
}

[KRPCClass (Service = "EVA")]
[KRPCProcedureId (nameof (Marker.Label), GetterId = 42)]
public class Flag : Marker {
    // ...
}

An id set on a member of Marker applies only to the Marker procedure. A base class that is not a KRPCClass sets no ids, on its members or with this attribute.

KRPCEnum (string Service, uint Id = 0)#
Parameters:
  • Service – Optional name of the service to add this enum to. If omitted, the enum is added to the service that contains its definition.

  • Id – Optional integer identifier for the enumeration, as for KRPCClass.

This attribute is applied to enumeration types. It adds the enumeration and its permissible values to the server. This attribute works similarly to KRPCClass, but is applied to enumeration types.

A KRPCEnum must be part of a service, just like a KRPCClass. Similarly, a KRPCEnum can be declared outside of a service if it has its Service parameter set to the name of the service that it is part of.

The enumeration type to which this attribute is applied must satisfy the following criteria:

  • The enumeration must be public.

  • The name of the enumeration must be a valid kRPC identifier.

  • The names of its values must follow the identifier rules, and be unique in the enumeration.

  • The enumeration must either be declared inside a KRPCService, or have it’s Service parameter set to the name of the service it is part of.

  • The underlying C# type must be an int.

An enumeration with [Flags] is a flags enumeration, whose values a client can combine. A value of it is any number holding no bit outside the union of the values it declares. It must also satisfy the following criteria:

  • It declares a value of 0, such as None.

  • Each value is 0, a single bit, or a union of single bits the enumeration declares.

  • No value is negative.

Examples

  • Declare an enumeration type with two values:

    [KRPCEnum (Service = "EVA")]
    public enum FlagState {
        Raised,
        Lowered
    }
    

    This can be used from a python client as follows:

    import krpc
    
    conn = krpc.connect()
    state = conn.eva.FlagState.lowered
    
  • Declare a flags enumeration, whose All value is the union of the others:

    [Flags]
    [KRPCEnum (Service = "EVA")]
    public enum SuitLights {
        None = 0,
        Helmet = 1,
        Shoulders = 2,
        All = Helmet | Shoulders
    }
    
KRPCStruct (string Service, uint Id = 0)#
Parameters:
  • Service – Optional name of the service to add this structure to. If omitted, the structure is added to the service that contains its definition.

  • Id – Optional integer identifier for the structure, as for KRPCClass.

This attribute is applied to a struct. It adds the structure and its fields to the server. A structure is a compound value with named fields. Its value is sent to the client in full, where a class sends a reference to an object held on the server.

The fields of the structure are its public instance properties that are annotated with KRPCProperty and have both a getter and a setter, in the order they are declared. An unannotated property is invisible to a client.

A KRPCStruct must be part of a service, just like a KRPCClass. Similarly, a KRPCStruct can be declared outside of a service if it has its Service parameter set to the name of the service that it is part of.

The struct type to which this attribute is applied must satisfy the following criteria:

  • The struct must be public and must not be generic.

  • The name of the struct must be a valid kRPC identifier.

  • The struct must either be declared inside a KRPCService, or have its Service parameter set to the name of the service it is part of.

  • It must have at least one field, and the type of every field must be a serializable type.

  • A field must not restrict the game scenes it is available in, so the GameScene parameter of its KRPCProperty attribute must not be set.

  • A field may be nullable. See Null Values.

  • The structure must not contain itself, whether directly or through the fields of another structure that it contains.

Fields may only be appended to a structure. A structure’s value is sent as the values of its fields in order, so reordering them, removing one, or changing the type of one breaks clients generated against the previous definition.

Choosing between a structure and a class. A structure sends the values of all of its fields in a single procedure call. Use one for compound data whose fields are almost always wanted together, such as a position and a velocity. Use a KRPCClass for something with mutable state, or whose members are expensive enough to read one at a time.

Examples

  • Declare a structure with three fields:

    [KRPCStruct (Service = "EVA")]
    public struct LandingSite {
        [KRPCProperty]
        public string Name { get; set; }
    
        [KRPCProperty]
        public CelestialBody Body { get; set; }
    
        [KRPCProperty]
        public double Latitude { get; set; }
    }
    

    This can be used from a python client as follows:

    import krpc
    
    conn = krpc.connect()
    site = conn.eva.landing_site
    print(site.name, site.latitude)
    
  • Declare a structure with two nullable fields:

    [KRPCStruct (Service = "Comms")]
    public struct CommNodeInfo {
        [KRPCProperty]
        public string Name { get; set; }
    
        [KRPCProperty (Nullable = true)]
        public Vessel Vessel { get; set; }
    
        [KRPCProperty]
        public double? SignalStrength { get; set; }
    }
    

    A reference-typed field is made nullable by the Nullable parameter of its KRPCProperty attribute. A field whose type is Nullable<T> needs no annotation.

KRPCException (string Service, Type MappedException, uint Id = 0)#
Parameters:
  • Service – Optional name of the service to add this enum to. If omitted, the enum is added to the service that contains its definition.

  • MappedException – Optional type of an exception to map to this exception. For example, can be used to map a built-in C# exception type onto this kRPC exception type.

  • Id – Optional integer identifier for the exception, as for KRPCClass.

This attribute is applied to an exception class type.

A KRPCException must be part of a service, just like a KRPCClass. Similarly, a KRPCException can be declared outside of a service if it has its Service parameter set to the name of the service that it is part of.

The class type to which this attribute is applied must satisfy the following criteria:

  • The class must be public.

  • The name of the class must be a valid kRPC identifier.

  • The class must either be declared inside a KRPCService, or have it’s Service parameter set to the name of the service it is part of.

KRPCDefaultValue (Type ValueConstructor)#
Parameters:
  • ValueConstructor – Type of a static class with a Create method that returns an instance of the default value.

This attribute can be applied to a kRPC procedure/class method parameter. It provides a workaround to set the default value of a parameter to a non-compile time constant. Ordinarily, C# only allows compile time constants to be used as the values of default arguments.

The ValueConstructor parameter is the type of a static class that contains a static method, called Create. When invoked, this method should return the default value.

Note: If you just want to set the default value to a compile time constant, use the C# syntax. kRPC will detect the default values and use them.

Examples

  • Set the default value to a list:

    public static class DefaultKerbals
    {
        public static IList<string> Create ()
        {
            return new List<string> { "Jeb", "Bill", "Bob" };
        }
    }
    
    [KRPCProcedure]
    public static void HireKerbals (
        [KRPCDefaultValue (typeof(DefaultKerbals))] IList<string> names)
    {
        // ...
    }
    
  • Set the default value to a compile time constant, which does not require the KRPCDefaultValue attribute:

    [KRPCProcedure]
    public static void HireKerbal (string name = "Jeb")
    {
        // ...
    }
    
KRPCNullable (params KRPC.Service.Attributes.Position[] Path)#
Parameters:
  • Path – Optional positions to step through to reach the nullable one, outermost first. An empty path, the usual case, is the value itself.

This attribute is applied to a kRPC procedure or class method parameter to indicate that the parameter is permitted to be null. It can be applied to a parameter of any serializable type, not only class types. Applied to a method it marks the return value instead. Applied to a property it marks both the getter’s return value and the setter’s argument, as Nullable on KRPCProperty does.

The server enforces nullability: a call that passes null for a parameter that is not nullable fails with an error. A parameter is nullable if it is marked with KRPCNullable, is assigned a default value of null, or has a Nullable<T> type. See Null Values.

Example

[KRPCService]
public static class EVA {
    [KRPCProcedure]
    public static void DestroyVessel ([KRPCNullable] Vessel vessel)
    {
        // vessel may be null
        // ...
    }
}

Nullable Positions#

Giving KRPCNullable a path marks a position nested inside a value, rather than the value itself. Each step names a position of the type the step before it reached, outermost first:

using static KRPC.Service.Attributes.Position;

[KRPCService]
public static class Fleet {
    [KRPCProcedure]
    public static void Assign (
        [KRPCNullable (Element)]
        IList<Vessel> crew, // the vessels may be null
        [KRPCNullable (Value)]
        IDictionary<string, Vessel> byName, // the values may be null
        [KRPCNullable (Item2)]
        Tuple<string, Vessel> named, // the second item may be null
        [KRPCNullable (Element, Element)]
        IList<IList<Vessel>> byStage) // the vessels may be null
    {
        // ...
    }
}

Without the using static each position is written Position.Element, as GameScene already is on KRPCProcedure.

A path step that names no position of the type it is applied to is an error, which the server reports when it loads the service. A dictionary key and a set element have no name here, and so cannot be made nullable. The positions are:

enum KRPC.Service.Attributes.Position#
Element#

The element of a list.

Value#

The value of a dictionary.

Item1#

The first item of a tuple. Item2 to Item7 name the ones after it.

The attribute may be applied more than once, for a type with several nullable positions:

[KRPCNullable]
[KRPCNullable (Element)]
IList<Vessel> maybeVessels // the list, and its vessels

A nested position whose type is Nullable<T> needs no attribute, exactly as a parameter of that type does. IList<int?>, IDictionary<string, double?> and Tuple<int?, string> are all nullable at the position Nullable<T> marks.

A path that reaches a value-typed position is an error. An int, an enum and a structure hold a null only as Nullable<T>, so IList<int> must be written IList<int?>.