scriptruntime.Runtime
- class scriptruntime.Runtime
Class for grouping various methods that interact with the script enviroment.
While starting new threads is generally NOT supported, this class in particular will not work correctly when used on user made threads.
- 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 withFalse
.Use this if you create resources or open files that you need to clean up or close before the script is interrupted.
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.
from scriptruntime import Runtime Runtime.require_force_close() while not Runtime.quitting(): pass print("quitting now")
- static message_stream() 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.
from scriptruntime import Runtime while True: print(f'{Runtime.signal() : 0.2f}')
- static signal_stream() 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
InertialSensor
on port0
and outputs the velocity to aTextScreen
on port1
.from sensors import InertialSensor from matrix import Matrix4x4 from scriptruntime import Runtime from vector import Vector3 from controllables import TextScreen imu = InertialSensor(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}"