Interacting with Parts

The following examples demonstrate use of the Parts functionality to achieve various tasks. More details on specific topics can also be found in the API documentation:

Deploying all Parachutes

Sometimes things go horribly wrong. The following script does its best to save your Kerbals by deploying all the parachutes:

using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;

class DeployParachutes
{
    public static void Main ()
    {
        using (var connection = new Connection ()) {
            var vessel = connection.SpaceCenter ().ActiveVessel;
            foreach (var parachute in vessel.Parts.Parachutes)
                parachute.Deploy ();
        }
    }
}

‘Control From Here’ for Docking Ports

The following example will find a standard sized Clamp-O-Tron docking port, and control the vessel from it:

using System;
using System.Linq;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;

class ControlFromHere
{
    public static void Main ()
    {
        using (var conn = new Connection ()) {
            var vessel = conn.SpaceCenter ().ActiveVessel;
            var part = vessel.Parts.WithTitle ("Clamp-O-Tron Docking Port") [0];
            vessel.Parts.Controlling = part;
        }
    }
}

Combined Specific Impulse

The following script calculates the combined specific impulse of all currently active and fueled engines on a rocket. See here for a description of the maths: https://wiki.kerbalspaceprogram.com/wiki/Specific_impulse#Multiple_engines

using System;
using System.Linq;
using KRPC.Client;
using KRPC.Client.Services.SpaceCenter;

class CombinedIsp
{
    public static void Main ()
    {
        using (var connection = new Connection ()) {
            var vessel = connection.SpaceCenter ().ActiveVessel;

            var activeEngines = vessel.Parts.Engines
                                .Where (e => e.Active && e.HasFuel).ToList ();

            Console.WriteLine ("Active engines:");
            foreach (var engine in activeEngines)
                Console.WriteLine ("   " + engine.Part.Title +
                                   " in stage " + engine.Part.Stage);

            double thrust = activeEngines.Sum (e => e.Thrust);
            double fuel_consumption =
                activeEngines.Sum (e => e.Thrust / e.SpecificImpulse);
            double isp = thrust / fuel_consumption;
            Console.WriteLine ("Combined vacuum Isp = {0:F0} seconds", isp);
        }
    }
}