
rs
Redstone on the six faces of the computer: read inputs, drive outputs.
The rs library connects a program to redstone. Each of the six faces of the computer is an input and an output at the same time: rs.get reads the signal that arrives on a face, rs.set sends a signal out of it. Every computer has it, from the Tube Computer to the Modern Computer.
It is the shortest way to reach the world: a Redstone Lamp, a door, a piston, a Create Clutch, a lever, a button, a Redstone Comparator reading a chest. No setup and no cable: put the block against the computer, or lead redstone dust to it.
-- a lever on the left lights the lamp on top
while true do
rs.set("top", rs.get("left") > 0)
os.pull_event("redstone")
endSignals are whole numbers from 0 (off) to 15 (full power), as everywhere in Minecraft. rs.set also takes true (15) and false (0).
The faces are named from the screen, in English and in lower case, whatever the language of the game:
| name | face |
|---|---|
front | the screen |
back | opposite the screen: the shaft that drives the computer enters here (the Microcontroller is driven from below) |
left, right | as you see them standing in front of the screen |
top, bottom | above and below the computer |
Not sure which face is which? Type brass at the prompt, then rs.set("left", true): the lamp that lights up is on the left. The built-in devices program also shows, face by face, the redstone received and sent.
rs.get(side) | The redstone signal arriving on a face, from 0 (nothing) to 15. |
rs.set(side, power) | Sends a redstone signal out of a face. |
rs.get_output(side) | The strength the computer sends out of a face: the last value set there, or 0. It is the computer's own output, not what the face receives (that is rs.get). |
rs.sides() | The list of the six face names, to go over every face in a loop. |
Inputs
A face reads the strongest signal that arrives on it, the way a Redstone Lamp in the computer's place would light up: redstone dust leading to the face, a lever or a button on the computer or on a block touching it, a Repeater or a Comparator facing the computer, a solid block powered by any of these.
**The redstone event.** When the signal changes on any face, the computer queues a redstone event. The event table holds nothing but its name: it says neither which face changed nor the new strength. Read the faces you care about with rs.get, and compare with what you read last time. Waiting for the event costs nothing while nothing changes, unlike a loop that reads rs.get again and again.
local last = rs.get("left")
while true do
os.pull_event("redstone")
local now = rs.get("left")
if now ~= last then
print("left: " .. last .. " -> " .. now)
last = now
end
endos.pull_event("redstone") throws away every other event while it waits (clicks, timers, messages). A program that also needs those calls os.pull_event() without a name and looks at e.name, as the door below does. See Events and Events.
The redstone signal arriving on a face, from 0 (nothing) to 15.
sidestring- a face:
"front","back","left","right","top"or"bottom"
- number
- the strength of the signal arriving on that face, from 0 to 15
Reading is instant: the computer keeps its six inputs up to date as the blocks around it change, and rs.get only looks the value up.
if rs.get("left") > 0 then
print("the lever is on")
endA face name that does not exist stops the program: rs.get("up") fails with bad side 'up' (front, back, left, right, top, bottom). Names are case sensitive, so "Left" fails too.
Count pulses. A Smart Observer watching a belt gives a pulse each time an item passes in front of it (when the items are spaced out). To count pulses, count the moments the input goes from off to on, not the events: each pulse sends two events (when it starts and when it ends), and a change on another face sends one too. This is called edge detection. Here it is on a list of readings:
local was = false
local count = 0
for _, power in ipairs({0, 15, 15, 0, 15, 0, 0, 15}) do
local now = power > 0
if now and not was then
count = count + 1
end
was = now
end
print(count .. " pulses")3 pulses
And the same idea on a real face:
-- counts the items a Smart Observer on the left sees pass on a belt
local count = 0
local was = rs.get("left") > 0
while true do
os.pull_event("redstone")
local now = rs.get("left") > 0
if now and not was then
count = count + 1
print("items: " .. count)
end
was = now
endThe built-in counter program works the same way.
The event does not carry the strength: rs.get returns the input as it is when the program reads it. A program waiting in os.pull_event reads the face right away, so it catches even the short pulse of an Observer. A program busy elsewhere (a long computation, a sleep) reads the face later, when a short pulse may already be over: it then gets two events and sees no change.
Read how full a container is. A Redstone Comparator reading a chest, a barrel or a Create Item Vault gives 0 when it is empty, 15 when it is full, and a strength in between that grows with the contents. Point the comparator at the computer, and the face reads the level:
local function bar(level)
return "[" .. string.rep("#", level) .. string.rep(".", 15 - level) .. "]"
end
print(bar(15))
print(bar(6))
print(bar(0))[###############] [######.........] [...............]
-- a Comparator reads the iron vault and points at the left face
local function bar(level)
return "[" .. string.rep("#", level) .. string.rep(".", 15 - level) .. "]"
end
while true do
local level = rs.get("left")
term.clear()
term.set_cursor(1, 1)
print("Iron vault " .. bar(level))
if level == 0 then
print("EMPTY")
elseif level == 15 then
print("FULL")
end
os.pull_event("redstone")
endA comparator tells how full, not how many. For an exact count of each item, drive the vault as a peripheral: inventory.count().
See also rs.set() rs.sides() os.pull_event()
Outputs
A face that outputs powers the block in front of it strongly, the way a lever powers the block it is placed on: a Redstone Lamp, a door, a piston, redstone dust or a Create Clutch on that face reacts, and when that block is a solid block (stone, planks), the lamps, dust and doors touching it react too.
Applied at the end of the tick. The world sees an output change at the end of the computer's tick. When a program sets the same face several times within one tick, only the last value counts: a loop that switches a lamp on and off without waiting changes nothing. To blink, wait between the changes with sleep.
Outputs stay. An output keeps its strength until the program changes it. The end of the program does not switch it off, whether it ends normally, with an error or with Ctrl+T. A reboot and a shutdown do: they set all six faces back to 0 (and also stop the frequencies of link and empty the lines of display). A computer whose rotation stops keeps its outputs while it waits. When its chunk unloads, the computer shuts down and its outputs go back to 0; when the chunk loads again, the computer boots and runs its startup program.
rs.set(side, power)
Sends a redstone signal out of a face.
sidestring- a face:
"front","back","left","right","top"or"bottom" powernumber|boolean- a strength from 0 to 15, or
true(15) andfalse(0)
rs.set("top", 15) -- the lamp on top at full power
rs.set("top", true) -- the same
rs.set("top", false) -- off
rs.set("left", 7) -- a weaker signal, for a comparator circuitA strength above 15 becomes 15, below 0 becomes 0. It must be a whole number: rs.set("top", 7.5) stops the program with bad argument #2 to 'set' (number has no integer representation), so round it first with math.floor. A missing strength gives bad argument #2 to 'set' (number expected, got no value).
The built-in blink program blinks a lamp, and sequencer plays a sequence on two faces.
A door with a timer. A button on the left opens the iron door on the right for 5 seconds. Pressing it again while the door is open starts the 5 seconds over. The program waits for any event: a press of the button (a redstone event) or the end of its timer (a timer event, see os.start_timer).
local OPEN_TIME = 5
local timer = nil
local was = false
while true do
local e = os.pull_event()
if e.name == "redstone" then
local pressed = rs.get("left") > 0
if pressed and not was then
rs.set("right", true)
timer = os.start_timer(OPEN_TIME) -- a new timer: the old one no longer counts
end
was = pressed
elseif e.name == "timer" and e.id == timer then
rs.set("right", false)
timer = nil
end
endA number on four lamps. Four faces can show a number from 0 to 15 in binary, one lamp per bit. The back holds the shaft and the front the screen, so the bits use the four other faces (the computer can sit on its bottom lamp, a Redstone Lamp is a full block). The helper turns each bit into true or false and returns the bits as text:
local lamps = {"right", "top", "left", "bottom"} -- bits worth 1, 2, 4 and 8
local function show(n)
local bits = ""
for i = 1, 4 do
local on = n % 2 == 1
rs.set(lamps[i], on)
bits = (on and "1" or "0") .. bits
n = n // 2
end
return bits
end
print(show(11))
print(show(6))1011 0110
With it, a counter that climbs from 0 to 15, one step a second:
local lamps = {"right", "top", "left", "bottom"}
local function show(n)
for i = 1, 4 do
rs.set(lamps[i], n % 2 == 1)
n = n // 2
end
end
while true do
for n = 0, 15 do
show(n)
sleep(1)
end
endSee also rs.get_output() rs.get()
The strength the computer sends out of a face: the last value set there, or 0. It is the computer's own output, not what the face receives (that is rs.get).
sidestring- a face:
"front","back","left","right","top"or"bottom"
- number
- the strength this computer sends out of that face, from 0 to 15
It gives the new value as soon as rs.set returns, even before the world sees it at the end of the tick. A switch that flips the lamp on top each time it is called:
local function toggle(side)
rs.set(side, 15 - rs.get_output(side))
end
toggle("top")Since outputs outlive the program that set them, a program can also tidy up what an earlier one left on:
for _, side in ipairs(rs.sides()) do
if rs.get_output(side) > 0 then
print("switching off " .. side)
rs.set(side, 0)
end
endFaces
The list of the six face names, to go over every face in a loop.
- table
- the six face names:
front,back,left,right,top,bottom, in that order
for _, side in ipairs(rs.sides()) do
print(side, rs.get(side), rs.get_output(side))
endEach line shows a face, the signal it receives, then the signal it sends. With a lever switched on at the left and the lamp on top lit by the computer, the screen reads:
front 0 0 back 0 0 left 15 0 right 0 0 top 0 15 bottom 0 0
The same names work with peripheral: the block touching a face is peripheral.wrap("left").
Good to know
- Keep inputs and outputs on different faces. A face that powers a solid block can read its own signal back from that block.
- Far away? Rather than a long line of dust, use a Create Redstone Link with
link: the computer transmits and receives without any block. - Machines. A Create Clutch disconnects while powered and a Gearshift reverses, so
rs.setstops or reverses a whole line. For an exact speed, drive a Rotation Speed Controller instead:kinetic.set_speed(). - More on wiring computers to redstone:
Redstone and Create links.
See also rs.get() peripheral.list()