Difference between revisions of "Redstone (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Added a second more complete example of usage, and how you might treat this event.)
m (The NeedsWork note seems to have already been fixed.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NeedsWork|A demonstration on the use and handling of this event would be beneficial. ''[[User:AfterLifeLochie|AfterLifeLochie]] 16:13, 30 November 2012 (MSK)''}}
 
 
 
{{Event
 
{{Event
 
|name=redstone
 
|name=redstone
Line 9: Line 7:
 
|code=
 
|code=
 
  while true do
 
  while true do
   event = os.pullEvent("redstone")
+
   local event = os.pullEvent("redstone")
 
   print("Redstone input changed.")
 
   print("Redstone input changed.")
 
  end
 
  end
|output=This will tell one when the redstone event is triggered. Note this doesn't tell you the side of the event.
+
|output=This will tell one when the redstone event is triggered. Note that this doesn't tell you the side of the event.
 
}}=== Application ===
 
}}=== Application ===
 
{{Example
 
{{Example
Line 18: Line 16:
 
|code=
 
|code=
 
  -- Set up a list which contains the sides as keys, and the current redstone state of each side as a boolean value
 
  -- Set up a list which contains the sides as keys, and the current redstone state of each side as a boolean value
  statelist = {
+
  local statelist = {
 
   ["top"] = rs.getInput("top"),
 
   ["top"] = rs.getInput("top"),
 
   ["front"] = rs.getInput("front"),
 
   ["front"] = rs.getInput("front"),
Line 32: Line 30:
 
   
 
   
 
  while true do -- Start an endless loop
 
  while true do -- Start an endless loop
   os.pullEvent("redstone") -- Yeild the computer until some redstone changes
+
   os.pullEvent("redstone") -- Yield the computer until some redstone changes
 
   -- We don't care what the event returns, since the first variable will be "redstone" and the rest will be nil.  
 
   -- We don't care what the event returns, since the first variable will be "redstone" and the rest will be nil.  
 
   
 
   
   --Now we check each side to see if it's changed.
+
   -- Now we check each side to see if it's changed.
 
   for side, state in pairs(statelist) do -- Run the lines of code in this loop for each key/value pair in statelist
 
   for side, state in pairs(statelist) do -- Run the lines of code in this loop for each key/value pair in statelist
 
     if rs.getInput(side) ~= state then -- If the side we're checking doesn't match what it was last time we checked then
 
     if rs.getInput(side) ~= state then -- If the side we're checking doesn't match what it was last time we checked then

Latest revision as of 04:35, 25 March 2014



Grid Modem.png  Event redstone
Fired when any Redstone inputs change on any of the sides of the computer.
Returned Object 1 Nothing

Basic Usage

Grid paper.png  Example
Alerts via console output when a redstone input changes.
Code
while true do
  local event = os.pullEvent("redstone")
  print("Redstone input changed.")
end
Output This will tell one when the redstone event is triggered. Note that this doesn't tell you the side of the event.


Application

Grid paper.png  Example
This program will store the values of each redstone signal on load, and on each state change. It will further display on the screen which state was changed.
Code
-- Set up a list which contains the sides as keys, and the current redstone state of each side as a boolean value
local statelist = {
  ["top"] = rs.getInput("top"),
  ["front"] = rs.getInput("front"),
  ["left"] = rs.getInput("left"),
  ["right"] = rs.getInput("right"),
  ["back"] = rs.getInput("back"),
  ["bottom"] = rs.getInput("bottom"),
}

-- Ready the terminal for printing to
term.clear()
term.setCursorPos(1,1)

while true do -- Start an endless loop
  os.pullEvent("redstone") -- Yield the computer until some redstone changes
  -- We don't care what the event returns, since the first variable will be "redstone" and the rest will be nil. 

  -- Now we check each side to see if it's changed.
  for side, state in pairs(statelist) do -- Run the lines of code in this loop for each key/value pair in statelist
    if rs.getInput(side) ~= state then -- If the side we're checking doesn't match what it was last time we checked then
      print(side.." is now "..tostring(rs.getInput(side))) -- Print the new state of the side.
      statelist[side] = rs.getInput(side) -- Update the statelist with the new change
    end
  end
end