Skip to content

JS Microcontroller

Overview

The Image of JS Microcontroller

A JS Microcontroller can run JavaScript (JS). It is theoretically capable of doing anything.

Before using it, you need to edit it to specify which ports are inputs and which are outputs. Every time the voltages on the input ports change, it will execute the JavaScript saved inside once. If there is an error during execution, it will be reported in the game log.

Every runtime environment of JS Microcontroller is independent. The variables, methods created inside will always be retained (but not saved to the save file). Strongly recommend checking whether initialization is necessary before initialization, otherwise it will cause duplicate declaration errors.

Built-in Variables

Built-in VariablesDescription
P0The voltage of the Back Port. When the Back Port is an input, this variable can be used to read the input voltage, and assignment is invalid. When the Back Port is an output, assigning a value to this variable will make the Back Port outputs the value.
P1The voltage of the Top Port. Others are same as above.
P2The voltage of the Right Port. Others are same as above.
P3The voltage of the Bottom Port. Others are same as above.
P4The voltage of the Left Port. Others are same as above.
SystemThe System namespace in .Net.
GameThe Game namespace in the game.
EngineThe Engine namespace in the game.
GameEntitySystemThe GameEntitySystem namespace in the game.
ProjectThe Project object of the current game save.

Built-in Methods

Built-in MethodsDescription
getPosition()Returns the position of the JS microcontroller. The return type isEngine.Point3, which contains fieldsXYZto represent the coordinates.
getPortState(number)Returns the input/output state of the specified port. The parameter should be an integer from 0 to 4. If the port is an input, it returns the stringinput; if the port is an output, it returns the stringoutput; if the port is disabled, it returns the stringdisabled; if the parameter is incorrect, it returnserror.
setPortDisabled(number)Sets the specified port to disabled. The parameter should be an integer from 0 to 4.
setPortInput(number)Sets the specified port to input. The parameter should be an integer from 0 to 4.
setPortOutput(number)Sets the specified port to output. The parameter should be an integer from 0 to 4.
importNamespace(string)Imports and returns a namespace.
findSubsystem(string)Returns a Subsystem of the current game save based on the name. The parameter should not start with "Subsystem".
executeAgain(number)Allows the JS Microcontroller to execute the script again after n electric cycles, regardless of input changes. If this method is used multiple times in a single execution, the last set value of n takes precedence. If n is less than or equal to 0, it cancels the action. If inputs change before executing, the method will be cancelled.

Please make sure to specify which ports are inputs and which are outputs, otherwise the script will not run.