CompuerCraft, BundledCable Event API« Wróć do listy pytań

how to attach event to bundledcable?
Wysłane przez mrmucha
Zadane przez 04/10/2015 10:27
0

Create file ”edit bc”

Paste this custom API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
local tCableState = {}  -- Holds the state of all bundled cables on the computer.
 
-- Get the current state for all 6 sides.
for _, v in pairs( rs.getSides() ) do
  tCableState[ v ] = rs.getBundledInput( v )
end
 
-- If a bundled cable changes its state, then this function returns the event 'bundled_cable' and a table.
-- The returned table contains only sides that have changed and it has this format: { side = { new_value, old_value } }
function pullEvent()
  local bStatesChanged = false
  local tChangedCables = {}
  local event, p1, p2, p3, p4, p5 = os.pullEvent()
 
  if event == "redstone" then
    -- Iterate over all 6 sides of the computer.
    for _, v in pairs( rs.getSides() ) do
          local nBCInput = rs.getBundledInput( v )  -- Get cable input for current side.
          if  nBCInput ~= tCableState[ v ] then  -- Has the state changed?
            tChangedCables[ v ] = { nBCInput, tCableState[ v ] }  -- Add the changed side + its new and old values to the return table.
            tCableState[ v ] = nBCInput  -- Update the local state table.
            bStatesChanged = true
          end
    end
  end
 
  -- Turn the 'redstone' event into a 'bundled_cable' event if the state of a bundled cable has changed.
  if bStatesChanged then
    event = "bundled_cable"
    p1 = tChangedCables
  end
 
  return event, p1, p2, p3, p4, p5
end
local tCableState = {}  -- Holds the state of all bundled cables on the computer.

-- Get the current state for all 6 sides.
for _, v in pairs( rs.getSides() ) do
  tCableState[ v ] = rs.getBundledInput( v )
end

-- If a bundled cable changes its state, then this function returns the event 'bundled_cable' and a table.
-- The returned table contains only sides that have changed and it has this format: { side = { new_value, old_value } }
function pullEvent()
  local bStatesChanged = false
  local tChangedCables = {}
  local event, p1, p2, p3, p4, p5 = os.pullEvent()
 
  if event == "redstone" then
    -- Iterate over all 6 sides of the computer.
    for _, v in pairs( rs.getSides() ) do
          local nBCInput = rs.getBundledInput( v )  -- Get cable input for current side.
          if  nBCInput ~= tCableState[ v ] then  -- Has the state changed?
            tChangedCables[ v ] = { nBCInput, tCableState[ v ] }  -- Add the changed side + its new and old values to the return table.
            tCableState[ v ] = nBCInput  -- Update the local state table.
            bStatesChanged = true
          end
    end
  end
 
  -- Turn the 'redstone' event into a 'bundled_cable' event if the state of a bundled cable has changed.
  if bStatesChanged then
    event = "bundled_cable"
    p1 = tChangedCables
  end
 
  return event, p1, p2, p3, p4, p5
end

and activate it by creating new filee ”edit core” with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- Load API
if not os.loadAPI( "bc" ) then
  error( "Bundled-Cable Event API could not be loaded. :unsure:/>/>" )
end
 
while true do
  local sEvent, p1, p2, p3, p4, p5 = bc.pullEvent()  -- Here we call our customized event listener bc.pullEvent(), instead of os.pullEvent()
 
  if sEvent == "bundled_cable" then
        -- p1 will contain a table with all the sides that have changed, mapped to their new and old values, i.e. p1 := { "side" = { new_value, old_value } }
        local new = 1
        local old = 2
        for side, tValue in pairs( p1 ) do
          print( "\""..side.."\" has changed from "..tValue[ old ].." to "..tValue[ new ] )
        end
  end
 
  if sEvent == "char" and string.lower( p1 ) == "q" then break end
end
-- Load API
if not os.loadAPI( "bc" ) then
  error( "Bundled-Cable Event API could not be loaded. :unsure:/>/>" )
end

while true do
  local sEvent, p1, p2, p3, p4, p5 = bc.pullEvent()  -- Here we call our customized event listener bc.pullEvent(), instead of os.pullEvent()

  if sEvent == "bundled_cable" then
        -- p1 will contain a table with all the sides that have changed, mapped to their new and old values, i.e. p1 := { "side" = { new_value, old_value } }
        local new = 1
        local old = 2
        for side, tValue in pairs( p1 ) do
          print( "\""..side.."\" has changed from "..tValue[ old ].." to "..tValue[ new ] )
        end
  end

  if sEvent == "char" and string.lower( p1 ) == "q" then break end
end

source : http://www.computercraft.info/forums2/index.php?/topic/416-bundles-cables-do-not-generate-events/page__view__findpost__p__2644

Wysłane przez mrmucha
Odpowiedziane 04/10/2015 10:40