CompuerCraft, BundledCable Event API« Wróć do listy pytań
how to attach event to bundledcable?
|
▲ ▼ |
Create file ”edit bc” Paste this custom API
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
-- 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 |