scriptruntime.Runtime

class scriptruntime.Runtime

Class for grouping various methods that interact with the script enviroment.

static require_force_close(value: bool = True)

To keep a script running after exiting live mode, call this function with True. To return to the default behavior of being interrupted immediately, call it with False.

The following sample demonstrates how to execute code on exiting live mode.

from scriptruntime import Runtime
from time import sleep

Runtime.require_force_close()

def main():
    while not Runtime.quitting():
        CoreLoop()
    OnQuit()

def CoreLoop():
    print("in the core loop")
    pass

def OnQuit():
    print("quiting...")
    sleep(3) # some expensive operation like writing out a file
    print("done quitting.")
    pass

if __name__ == "__main__":
    main()
static quitting() bool

Returns true if the runtime is currently waiting for this script to finish before exiting live mode.

static message_stream() streams.Stream[str]

Creates a stream to listen to messages sent to this MicroController.

from controllables import MicroController

#sending script
mc = MicroController(0)
mc.message("hello world")
from scriptruntime import Runtime

#recieving script
message_stream = Runtime.message_stream()

while True:
    for message in message_stream: 
        print("recieved me message: " + message)
static signal() float

Returns the signal sent to the microcontroller via the transmitters hooked up to it transmitter tab in the game.

static signal_stream() streams.Stream[float]

Creates a stream that captures the signals sent to the microcontroller via the transmitters hooked up to it transmitter tab in the game.

from scriptruntime import Runtime

signals = Runtime.signal_stream()

while True:
    for signal in signals:
        print(signal)
static sample_duration() float

Returns the length in seconds between samples in the input and sensor streams. When two events in a stream come one after the other, that means that the first event occured sample_duration() seconds before the second.

The following example uses sample duration to integrate the acceleration of an InertialMotionUnit on port 0 and outputs the velocity to a TextScreen on port 1.

from sensors import InertialMotionUnit
from matrix import Matrix4x4
from scriptruntime import Runtime
from vector import Vector3
from controllables import TextScreen

imu = InertialMotionUnit(0)
output = TextScreen(1)

stream = imu.motion_data_stream()
dt = Runtime.sample_duration()
velocity = Vector3(0,0,0)

while True:
    for sample in stream:
        acc          = sample.acceleration
        rot          = sample.rotation_axis
        rot_matrix   = Matrix4x4.from_rotation(rot)
        #rotate the acceleration to get a vector relative to a fixed frame
        dv           = rot_matrix * acc

        #use the sample duration here to do the integration
        velocity += dv * dt

    output.text = f"{velocity:+4.1f}"