controllables.MicroController

class controllables.MicroController(port: Optional[int] = None)

Reference to a microcontroller part.

The following example forwards all of the messages it receives on to every microcontroller that is assigned in its ports.

from controllables import MicroController
from scriptruntime import Runtime
from ports import Port

# get a message stream
stream = Runtime.message_stream()
# get a list of all microcontrollers in my ports
microcontrollers = [
        MicroController(port) 
        for port in Port.ports()
        if Port.port_type(port) == MicroController
    ]

while True:
    # for each message we receive
    for message in stream:
        # forward it on to all of our microcontrollers
        for microcontroller in microcontrollers:
            microcontroller.message(message)
static self() MicroController

Returns the microcontroller to which the calling script is attached.

The following example prints the name of the microntroller on which it runs.

from controllables import MicroController

print(MicroController.self().name())
message(message: str)

Sends a message which can be read via message_stream() on the script attached to this microcontroller.

Note

The message is not sent instantly, but will arrive within a few milliseconds.

The following example sends the message “hello world” when it starts.

from controllables import MicroController

# sending script
mc = MicroController(0)
mc.message("hello world")

The next example polls its message stream and prints them out.

from scriptruntime import Runtime

# recieving script
message_stream = Runtime.message_stream()

while True:
    for message in message_stream: 
        print("recieved me message: " + message)
name() str

Returns the user editable name of the controllable as found in the properties tab of the game.

The following example assumes that any controllable or sensor is assigned to port 0 and prints out its name.

from ports import PortReference

print(PortReference(0).name())