krpc-go is a Go client for kRPC, a Kerbal Space Program mod for controlling the game with an external program.
go get github.com/atburke/krpc-go
This sample program will launch a vessel sitting on the launchpad. Error handling is omitted for brevity.
package main
import (
"context"
krpcgo "github.com/atburke/krpc-go"
"github.com/atburke/krpc-go/krpc"
"github.com/atburke/krpc-go/spacecenter"
)
func main() {
// Connect to the kRPC server with all default parameters.
client := krpcgo.DefaultKRPCClient()
client.Connect(context.Background())
defer client.Close()
sc := spacecenter.New(client)
vessel, _ := sc.ActiveVessel()
control, _ := vessel.Control()
control.SetSAS(true)
control.SetRCS(false)
control.SetThrottle(1.0)
control.ActivateNextStage()
}
This section describes type mappings from the kRPC protocol.
- Primitives are mapped to Go primitives.
- Arrays are mapped to slices. Dictionaries and sets are mapped to maps.
- Tuples are mapped to a special tuple type in the
types
package. For example, a tuple of strings would map totypes.Tuple3[string, string, string]
.types
also contains some convenience types that can be converted to/from the appropriate tuple, such astypes.Vector2D
,types.Vector3D
, andtypes.Color
.
- Classes and enums are mapped to local structs and constants defined in the appropriate service. For example, a Vessel will be mapped to a
*spacecenter.Vessel
, and a GameScene will be mapped to akrpc.GameScene
. - Existing protobuf types can be found in the
types
package. For example, a Status will be mapped to a*types.Status
.
krpc-go uses Go's built-in channels to handle streams.
Here's an example of using streams to autostage a vessel until a specific stage is reached.
func AutoStageUntil(vessel *spacecenter.Vessel, stopStage int32) {
go func() {
control, _ := vessel.Control()
stage, _ := control.CurrentStage()
for stage > stopStage {
resources, _ := vessel.ResourcesInDecoupleStage(stage-1, false)
amountStream, _ := resources.AmountStream("LiquidFuel")
// Wait until this stage runs out of liquid fuel.
for amount := <-amountStream.C; amount > 0.1 {}
amountStream.Close()
control.ActivateNextStage()
stage--
}
}()
}
See tests in integration/
for more usage examples.
TODO
TODO krpc-go docs link kRPC documentation