Game Scenes and Saves#
KSP is divided into scenes: the space center, flight, the tracking station, and the editors, the VAB and the SPH. A remote call works in some scenes and not others. This guide describes the scenes, how to move between them, and how to save, load and launch.
The Current Scene#
KRPC.GameScene gives the current scene: SpaceCenter, Flight, TrackingStation,
EditorVAB or EditorSPH. While a facility in the space center is open, it gives that
facility instead: AstronautComplex, MissionControl, ResearchAndDevelopment or
Administration.
The server runs only while a save is loaded. Returning to the main menu stops it, and closes every connection.
What Works Where#
A call outside the scenes it supports raises KRPC.UnsupportedSceneException. For example:
Control,Flight,AutoPilot,CameraandNodework in flight only.SpaceCenter.Editorworks in the VAB and the SPH only.Time warp works in flight, the space center and the tracking station.
Saving, loading and launching work in every scene.
A stream of a call that the current scene does not support pauses, keeps its last value, and resumes when the scene supports the call again. The streams and events guide describes this.
Changing Scene#
Setting KRPC.GameScene changes scene, or opens or closes a facility. It returns straight away,
and the scene changes over the next few frames. This example goes to the space center, and waits
until it is there:
GameScene = conn.krpc.GameScene
conn.krpc.game_scene = GameScene.space_center
while conn.krpc.game_scene != GameScene.space_center:
time.sleep(0.1)
krpc.GameScene = GameScene.SpaceCenter;
while (krpc.GameScene != GameScene.SpaceCenter)
Thread.Sleep (100);
using GameScene = krpc::services::KRPC::GameScene;
krpc.set_game_scene(GameScene::space_center);
while (krpc.game_scene() != GameScene::space_center)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
krpc_KRPC_set_GameScene(conn, KRPC_KRPC_GAMESCENE_SPACECENTER);
krpc_KRPC_GameScene_t scene;
while (krpc_KRPC_GameScene(conn, &scene) == KRPC_OK &&
scene != KRPC_KRPC_GAMESCENE_SPACECENTER)
sleep_ms(100);
krpc.setGameScene(GameScene.SPACE_CENTER);
while (krpc.getGameScene() != GameScene.SPACE_CENTER) {
Thread.sleep(100);
}
local GameScene = conn.krpc.GameScene
conn.krpc.game_scene = GameScene.space_center
while conn.krpc.game_scene.value ~= GameScene.space_center.value do
socket.sleep(0.1)
end
const GameScene = conn.krpc.GameScene;
await conn.krpc.setGameScene(GameScene.spaceCenter);
while ((await conn.krpc.getGameScene()) !== GameScene.spaceCenter) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
Setting the scene to Flight resumes the save’s active vessel. Leaving flight saves the game
first, unless the game cannot be saved. SpaceCenter.CanSave gives whether it can, and is false
while the vessel accelerates or moves over the surface.
Setting SpaceCenter.ActiveVessel switches to another vessel. Outside flight, it enters flight
with that vessel. The call returns once the switch has finished.
Saving and Loading#
SpaceCenter.Save and SpaceCenter.Load save and load a game by name, in the folder of the
current save. SpaceCenter.Quicksave and SpaceCenter.Quickload use the game’s quicksave.
Loading enters flight with the save’s active vessel, and the call returns once that vessel has
loaded. SpaceCenter.RevertToLaunch reverts the flight in the same way.
SpaceCenter.CanRevertToLaunch gives whether a revert is possible. A revert that is not
possible does nothing.
Objects you obtained before a load go on working, and read the loaded game. The object
lifetime guide describes this. KRPC.GameStateLoads goes up by
one each time a load, a revert or a change of scene finishes, so a stream of it tells you when the
game has been replaced.
Launching Vessels#
SpaceCenter.LaunchableVessels lists the craft in the save’s VAB or SPH folder.
SpaceCenter.LaunchVesselFromVAB launches one onto the launch pad, and
SpaceCenter.LaunchVesselFromSPH onto the runway. The call returns once the vessel has loaded
and settled:
for name in space_center.launchable_vessels("VAB"):
print(name)
space_center.launch_vessel_from_vab("Kerbal X")
print(space_center.active_vessel.name)
foreach (var name in spaceCenter.LaunchableVessels ("VAB"))
Console.WriteLine (name);
spaceCenter.LaunchVesselFromVAB ("Kerbal X");
Console.WriteLine (spaceCenter.ActiveVessel.Name);
for (const auto& name : space_center.launchable_vessels("VAB"))
std::cout << name << std::endl;
space_center.launch_vessel_from_vab("Kerbal X");
std::cout << space_center.active_vessel().value().name() << std::endl;
krpc_list_string_t names = KRPC_NULL_LIST;
krpc_SpaceCenter_LaunchableVessels(conn, &names, "VAB");
for (size_t i = 0; i < names.size; i++) {
printf("%s\n", names.items[i]);
krpc_free(names.items[i]);
}
KRPC_FREE_LIST(names);
krpc_SpaceCenter_LaunchVesselFromVAB(conn, "Kerbal X", true);
krpc_SpaceCenter_Vessel_t vessel;
krpc_SpaceCenter_ActiveVessel(conn, &vessel);
char* name = NULL;
krpc_SpaceCenter_Vessel_Name(conn, &name, vessel);
printf("%s\n", name);
krpc_free(name);
for (String name : spaceCenter.launchableVessels("VAB")) {
System.out.println(name);
}
spaceCenter.launchVesselFromVAB("Kerbal X", true);
System.out.println(spaceCenter.getActiveVessel().getName());
for _, name in ipairs(space_center.launchable_vessels('VAB')) do
print(name)
end
space_center.launch_vessel_from_vab('Kerbal X')
print(space_center.active_vessel.name)
for (const name of await spaceCenter.launchableVessels("VAB")) {
console.log(name);
}
await spaceCenter.launchVesselFromVAB("Kerbal X");
console.log(await (await spaceCenter.getActiveVessel()).getName());
SpaceCenter.LaunchVessel also takes the launch site, the crew and a flag. The launch runs the
game’s pre-flight checks, and raises an exception if they fail. By default it recovers a vessel
already on the launch site. Otherwise an occupied site raises an exception.
In the VAB or the SPH, Editor.LoadVessel loads a craft into the editor, and
Editor.LaunchVessel launches the vessel being built.
Calls That Take Several Ticks#
Loading, reverting, launching, switching vessel and loading a craft into the editor each take many physics ticks. A call to one of them returns once it has finished, and ends any hold on the tick that your client has.