This short guide explains the basics for getting the kRPC server set up and running, and writing a basic Python script to interact with the game.
The Server Plugin¶
- Download and install the kRPC server plugin from one of these locations:
Start up KSP and load a save game.
You should be greeted by the server window:
Click “Start server” to, erm… start the server! If all goes well, the light should turn a happy green color.
You can hide the window by clicking the close button in the top right. The window can also be shown/hidden by clicking on the icon in the top right:
This icon will also turn green when the server is online.
The server is configured by clicking edit on the window displayed in-game:
- Protocol: this is the protocol used by the server. This affects type of client can connect to the server. For Python, and most other clients that communicate over TCP/IP you want to select “Protobuf over TCP”.
- Address: this is the IP address that the server will listen on. To only allow connections from the local machine, select ‘localhost’ (the default). To allow connections over a network, either select the local IP address of your machine, or choose ‘Manual’ and enter the local IP address manually.
- RPC and Stream port numbers: These need to be set to port numbers that are available on your machine. In most cases, they can just be left as the default.
There are also several advanced settings, which are hidden by default, but can be revealed by checking “Show advanced settings”:
- Auto-start server: When enabled, the server will start automatically when the game loads.
- Auto-accept new clients: When enabled, new client connections are automatically allowed. When disabled, a pop-up is displayed asking whether the new client connection should be allowed.
The other advanced settings control the performance of the server.
The Python Client¶
kRPC supports both Python 2.7 and Python 3.x.
- If you don’t already have python installed, download the python installer and run it: https://www.python.org/downloads/windows When running the installer, make sure that pip is installed as well.
- Install the kRPC python module, by opening command prompt and running the following command:
C:\Python27\Scripts\pip.exe install krpcYou might need to replace
C:\Python27with the location of your python installation.
- Run Python IDLE (or your favorite editor) and start coding!
- Your linux distribution likely already comes with python installed. If not, install python using your favorite package manager, or get it from here: https://www.python.org/downloads
- You also need to install pip, either using your package manager, or from here: https://pypi.python.org/pypi/pip
- Install the kRPC python module by running the following from a terminal:
sudo pip install krpc
- Start coding!
‘Hello World’ Script¶
Run KSP and start the server with the default settings. Then run the following python script.
1 2 3 4
import krpc conn = krpc.connect(name='Hello World') vessel = conn.space_center.active_vessel print(vessel.name)
This does the following: line 1 loads the kRPC python module, line 2 opens a new connection to the server, line 3 gets the active vessel and line 4 prints out the name of the vessel. You should see something like the following:
Congratulations! You’ve written your first script that communicates with KSP.