Microcontroller Globals - iimurpyh/pilot-lua GitHub Wiki
Microcontroller-specific globals. These are the bridge from the code to the game.
Table of contents
Getting parts
Part GetPartFromPort(int | Part port, string partType)
Gets a part from the port number you specify and the part type. One port can be attached to multiple parts, and
partType
selects which one based on its type (e.g."Screen"
).The
port
argument lets you specify which port you want to find the part in by its ID. You can see and change a port's ID by configuring it. You can also pass a Part to search for things which are attached to it. The function considers any table with an index ofGUID
to be a Part, so you can also do something likeGetPartFromPort({GUID = SandboxID}, partType)
to get parts attached directly to the microcontroller.
Code example
This example gets the Screen connected to port 1, then it clears it.
local myScreen = GetPartFromPort(1, "Screen") -- Gets a Screen from port 1
myScreen:ClearElements() -- Clears the Screen. See the docs for the "Screen" part
array GetPartsFromPort(int port, string partType)
Gets an array of parts from the given port number in no particular order.
Code example
This example gets all Switches connected to port 1 and toggles each to be active.
local switches = GetPartsFromPort(1, "Switch") -- Gets all switches on port 1.
for _, switch in ipairs(switches) do -- Loops over each switch in the list of switches.
switch:Configure({ SwitchValue = true }) -- Toggles all switches.
end
Ports
Port GetPort(int port)
Returns the
Port
specified by the port number you input. This is the same port number fromGetPartsFromPort
.
void TriggerPort(int port)
Triggers a
Port
with the port number you input. This is the same port number fromGetPartsFromPort
.
Global Values
string SandboxID
The GUID of the microcontroller running this code.
number SandboxRunID
A randomly generated numeric ID that represents the current running microcontroller script. Each time the microcontroller is ran this value will be randomized, so it is a good choice for generating unique random seeds.
Miscellaneous
void Beep(float pitch)
Beeps with the given
pitch
. **Beep!**
table JSONDecode(string json)
Takes a
json
string and decodes it, giving you a complex piece of data.
string JSONEncode(table dataToEncode)
Encodes the
data
you give as JSON. This can be used to store complex data as a simple string. A ton of modern web services use JSON. JSON can only encode basic data, like numbers, strings, and tables. Anything else will just encode likenil
does. That means that you cannot encode Instances, userdatas, functions, threads, etc.
void Communicate()
Unimplemented. Meant to be for cross-microcontroller communication, but it does nothing. Replaced by :Send() and :Receive() in unstable.