FANDOM


KerboScript is a programming language that is derived from the language of planet Kerbin, which sounds like gibberish to non-native speakers but for some reason is written exactly like English. As a result, KerboScript is very English-like in its syntax. For example, it uses periods as statement terminators.

The language is designed to be easily accessible to novice programmers, therefore it is case-insensitive, and types are cast automatically whenever possible.

A typical command in KerboScript might look like this:

PRINT “Hello World”.

Output:

Hello World

ExpressionsEdit

KerboScript uses an expression evaluation system that allows you to perform math operations on variables. Some variables are defined by you. Others are defined by the system.

There are three basic types:

NumbersEdit

You can use mathematical operations on numbers, like this:

SET X TO 4 + 2.5. PRINT X.

Output:

6.5

The system follows the order of operations, but currently the implementation is imperfect. For example, multiplication will always be performed before division, regardless of the order they come in. This will be fixed in a future release.

Numbers are always floating point "real" numbers, rather than being rounded to integers. Therefore be careful when making comparisons using the equals operator (=). The number you expected to be 1234567 might actually be 1234567.000001, for example.

Resource TagsEdit

Resource tags allow you to quickly look up the amount of a resource your ship has. Any resource that appears at the top right resource panel can be queried.

PRINT <LiquidFuel>. // Print the total liquid fuel in all tanks.

StringsEdit

Strings are pieces of text that are generally meant to be printed to the screen. For example:

PRINT “Hello World!”.

To concatenate strings, you can use the + operator. This works with mixtures of numbers and strings as well.

PRINT “4 plus 3 is: “ + (4+3).

DirectionsEdit

Directions exist primarily to enable automated steering. You can initialize a direction using a vector or a rotation.

A Direction is different from a Vector in the important way that a Direction does not care about magnitude.  A Direction only stores the way something is pointing.  Directions don't have "length".

SET Direction TO V(0,1,0). // Set a direction by vector

SET Direction TO R(0,90,0). // Set by a rotation in degrees

You can use math operations on Directions as well. The next example uses a rotation of “UP” which is a system variable describing a vector directly away from the celestial body you are under the influence of.

SET Direction TO UP + R(0,-45,0). // Set direction 45 degress west of “UP”.

It is possible to get the three component parts of a Direction using the terms "yaw", "pitch", and "roll" after a colon. Like so:

PRINT D:yaw .
PRINT D:pitch .
PRINT D:roll .

An aside about Rotations: They are Euler angles. They break orthogonalityEdit

People who have never heard of the phrase "Euler Angle" will need to check this sidebar topic: Steering Angles The rotations system in KOS does NOT work intuitively.

VectorsEdit

Vectors also exist in the language, but as of this writing they are not very usable by the programmer because you cannot perform vector operations on them nor get access to their three XYZ component parts. Support for this is planned.

OperatorsEdit

The following mathematical (and other) operators exist:

* / + - Edit

These operate as you'd expect: multiply, divide, add, subtract, in the usual order of operations.

^ Edit

"To the power of": 2 ^ 5 gives a result of 32, for example. Fractional exponents are allowed, so you can make a square root with ^(0.5). Note that in the font of the kOS terminal window, the caret character (^) shows up as an arrow pointing upward. This is taken from an old Commodore 64 font.

( ) Edit

Parentheses work as expected, to group terms.

abs( number ) Edit

Absolute value of the number in the parentheses.

sin( angle ), cos( angle ), tan( angle ) Edit

Trigonometry functions..

Shell CommandsEdit

These commands are used to operate on files within the KOS system, but can be used within a file as well.  It should be noted that throughout this description "file", "program" and "script" mean the same thing. When you edit a program, you're making a file. When you copy a file, you're copying a program, etc.

To fully understand the COPY, DELETE, SWITCH, and LIST commands described below, it's important to understand the sidebar topic Archive and Volumes first.

COPYEdit

Copies a file to or from another volume. Volumes can be referenced by their ID numbers or their names if they’ve been given one. See LIST, SWITCH and RENAME. Example:

SWITCH TO 1. // Makes volume 1 the active volume

COPY file1 FROM 0. // Copies a file called file1 from volume 0 to volume 1

COPY file1 TO 0. // Copies a file called file1 from volume 1 to volume 0

DELETEEdit

Deletes a file. You can delete a file from the current volume, or from a named volume. Example:

DELETE file1. // Deletes file1 from the active volume.

DELETE file1 FROM 1. // Deletes file1 from volume 1


EDITEdit

Edits a program on the currently selected volume. Example:

EDIT filename.


LISTEdit

Lists the files on the current volume, or lists the currently available volumes. Lists files by default. Example:

LIST. // Lists files on the active volume

LIST FILES. // Lists files on the active volume

LIST VOLUMES. // Lists all volumes, with their numbers and names

LIST TARGETS. // Lists all target vessels in radio range


RENAMEEdit

Renames a file or volume. Example:

RENAME VOLUME 1 TO AwesomeDisk

RENAME FILE MyFile TO AutoLaunch.


RUNEdit

Runs the specified file as a program. Example:

RUN AutoLaunch.

A program can be given arguments like this if it was written with DECLARE PARAMETER in it:

RUN program(arg1, arg2).

SWITCH TOEdit

Switches to the specified volume. Volumes can be specified by number, or it’s name (if it has one). See LIST and RENAME. Example:

SWITCH TO 0. // Switch to volume 0.

RENAME 1 TO AwesomeDisk. // Name volume 1 as AwesomeDisk.

SWITCH TO AwesomeDisk. // Switch to volume 1.


Script CommandsEdit

These commands are used within a script, but some can also be used on the fly.

BREAKEdit

Breaks out of a loop. Example:

SET X TO 1. UNTIL 0 { SET X TO X + 1. IF X > 10 { BREAK. }. 
// Exits the loop when X is greater than 10 }.


CLEARSCREENEdit

Clears the screen and places the cursor at the top left. Example:

CLEARSCREEN.


DECLAREEdit

Declares a variable at the current context level. Alternatively, a variable can be implicitly declared by a SET or LOCK statement. Example:

DECLARE X.

DECLARE PARAMETEREdit

Declares that a program can be called with arguments from its caller, and if it is, what variables those parameters are stored in. In the example below, the program can be called with two parameters and they will be stored in variables A and B.

DECLARE PARAMETER A.
DECLARE PARAMETER B.

is equvalent to:

DECLARE PARAMETER A, B.

The order in which the declarations appear in the program file is the order in which they are expected in the parenthesized list when running of the program.


IFEdit

Checks if the expression supplied returns true. If it does, IF executes the following command block. Example:

SET X TO 1. IF X = 1 { PRINT "X equals one.". }. // Prints "X equals one."

IF X > 10 { PRINT "X is greater than ten.". }. // Does nothing

If statements can make use of boolean operators. Example:

IF X = 1 AND Y > 4 { PRINT "Both conditions are true". }.

IF X = 1 OR Y > 4 { PRINT "At least one condition is true". }.


LOCKEdit

Locks a variable to an expression. On each cycle, the target variable will be freshly updated with the latest value from expression. Example:

SET X TO 1. LOCK Y TO X + 2. PRINT Y. // Outputs 3 

SET X TO 4. PRINT Y. // Outputs 6


ONEdit

Awaits a change in a boolean variable, then runs the selected command. This command is best used to listen for action group activations. Example:

ON AG3 PRINT “Action Group 3 Activated!”.

ON SAS PRINT “SAS system has been toggled”.


PRINTEdit

Prints the selected text to the screen. Can print strings, or the result of an expression. Example:

PRINT “Hello”. PRINT 4+1.

PRINT “4 times 8 is: “ + (4*8).


SET.. TOEdit

Sets the value of a variable. Declares the variable if it doesn’t already exist. Example:

SET X TO 1.


STAGEEdit

Executes the stage action on the current vessel. Example:

STAGE.


TOGGLEEdit

Toggles a variable between true or false. If the variable in question starts out as a number, it will be converted to a boolean and then toggled. This is useful for setting action groups, which are activated whenever their values are inverted. Example:

TOGGLE AG1. // Fires action group 1.

TOGGLE SAS. // Toggles SAS on or off.


UNLOCKEdit

Releases a lock on a variable. See LOCK. Examples:

UNLOCK X. // Releases a lock on variable X.

UNLOCK ALL. // Releases ALL locks.


UNTILEdit

Performs a loop until a certain condition is met. Example:

SET X to 1. 
UNTIL X > 10 { 
PRINT X. 
SET X to X + 1. 
}. // Prints the numbers 1-10.


WAITEdit

Halts execution for a specified amount of time, or until a specific set of criteria are met. Note that running a WAIT UNTIL statement can hang the machine forever if the criteria are never met. Examples:

WAIT 6.2. // Wait 6.2 seconds.

WAIT UNTIL X > 40. // Wait until X becomes greater than 40.

WAIT UNTIL APOAPSIS > 150000. // You can see where this is going.


WHEN.. THENEdit

Executes a command when a certain criteria are met. Unlike WAIT, WHEN does not halt execution. Example:

WHEN BCount < 99 THEN PRINT BCount + “ bottles of beer on the wall”.

It should be noted that this sets up an event that only fires once.  A pending WHEN..THEN only remains "alive" as long as the condition continues to be false.  So long as it's still false, kOS will continue checking each cycle to see if it's become true.  Once it does become true and the "THEN" is executed, the checking on that WHEN..THEN is removed.  If a program appears to be able to trigger a WHEN..THEN more than once, then if you look closer you'll probably see that it has the WHEN..THEN inside a loop such if it does trigger, the next iteration of the loop initializes a new WHEN..THEN and puts it back in place.

..ONEdit

Sets a variable to true. This is useful for the RCS and SAS bindings. Example:

RCS ON // Turns on the RCS


..OFFEdit

Sets a variable to false. This is useful for the RCS and SAS bindings. Example

RCS OFF // Turns off the RCS

built-in Mathematical functions Edit

These can be used inside expressions when setting variables:not written explanation

SIN(a) COS(a) TAN(a)
ARCSIN(y) ARCCOS(x) ARCTAN(yxratio) ARCTAN2(y,x)
ABS(x)

Flight StatisticsEdit

You can get several useful vessel stats for your ships

VESSELNAME 

ALTITUDE 

ALT:RADAR // Your radar altitude

BODY // The current celestial body whose influence you are under

MISSIONTIME // The current mission time

VELOCITY // The current orbital velocity

VERTICALSPEED

SURFACESPEED

LATITUDE

LONGITUDE

STATUS // Current situation: LANDED, SPLASHED, PRELAUNCH, FLYING, SUB_ORBITAL, ORBITING, ESCAPING, or DOCKED

HEADING // 2-D heading by compass direction on ground (i.e. 90 = east).

FACING // 3-D way the craft is pointed, expressed as an R(). (i.e. R(101.2,150.041,0) ).  

MASS

MAXTHRUST // Combined thrust of active engines at full throttle (kN)

VectorsEdit

These return a vector object, which can be used in conjuction with the LOCK command to set your vessel's steering.

PROGRADE

RETROGRADE

UP // Directly away from current body

Orbit geometry valuesEdit

These values can be polled either for their altitude, or the vessel's ETA in reaching them. By default, altitude is returned.

APOAPSIS // Altitude of apoapsis

ALT:APOAPSIS // Altitude of apoapsis

PERIAPSIS // Altitude of periapsis

ALT:PERIAPSIS // Altitude of periapsis

ETA:APOAPSIS // ETA to apoapsis

ETA:PERIAPSIS // ETA to periapsis

Maneuver nodesEdit

NODE // Direction of next maneuver node

. . . can be used with . . .

LOCK STEERING TO NODE.  // point the ship to the blue marker of the maneuver node
MAG:NODE // Delta-v magnitude of maneuver node

ETA:NODE // ETA to active maneuver node

Stage specific valuesEdit

STAGE:LIQUIDFUEL

STAGE:OXIDIZER

STAGE:SOLIDFUEL

Flight ControlEdit

These values can be SET, TOGGLED, or LOCKED. Some values such as THROTTLE and STEERING explicity require the use of lock.

Controls which use ON and OFFEdit

These special variable names are boolean variables that control the item on the vessel when you change their values:

SAS,  RCS,  GEAR (see caveat below),  LIGHTS,  BRAKES

An example of use:

SAS ON.
RCS OFF.

Caveat: In order for a KOS program to affect landing legs with GEAR ON or GEAR OFF you must add them to the gear action group first. By default GEAR ON and GEAR OFF will only affect aircraft landing wheels rather than landing legs. (This is not KOS's fault and is due to KSP implementing the "G" key strangely.)

Controls that can be used with TOGGLEEdit

ABORT AGX // Where x = 1 through 10. Use toggle, example:

TOGGLE AG1.

Controls that must be used with LOCKEdit

THROTTLE // Lock to a decimal value between 0 and 1.

STEERING // Lock to a direction.

WHEELTHROTTLE // Seperate throttle for wheels

WHEELSTEERING // Seperate steering system for wheels

Other BindingsEdit

TARGET // Set a target using it's name in quotes

TARGET:HEADING // Gets the heading to the current target

TARGET:BEARING // Gets the bearing to the current targe

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.