This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

OCS problem

Started by Sewbacca, 11 April 2016 - 11:58 AM
Sewbacca #1
Posted 11 April 2016 - 01:58 PM

os.loadAPI('ocs/apis/sensor')
while true do
  handle = sensor.wrap('left').getTargets()
  if handle.sewbacca then
    local pos = handle.sewbacca.Position.X
    local pos2 = handle.sewbacca.Position.Z
    if pos > pos2 then pos = pos else pos = pos2 end
    if pos <= 2 then
      redstone.setOutput('top', true)
    else
      redstone.setOutput('top', false)
    end
  else
	redstone.setOutput('top', false)
  end
  sleep(0)
end

I want to create an auto door opener.
This script doesn't works (The door doesn't open/close)

(Scanner Proximity Card Mk. I)
Edited on 11 April 2016 - 02:59 PM
Bomb Bloke #2
Posted 11 April 2016 - 02:46 PM
Using print() calls, pinpoint the exact "if" branch that isn't going in the direction you expect, and get it to tell you what the relevant variables are set to at that time.
Sewbacca #3
Posted 11 April 2016 - 04:40 PM
Using print() calls, pinpoint the exact "if" branch that isn't going in the direction you expect, and get it to tell you what the relevant variables are set to at that time.
It seems like the while loop will runs just one time.
EveryOS #4
Posted 11 April 2016 - 05:01 PM
Check the API file. It may be calling 'break'
SquidDev #5
Posted 11 April 2016 - 05:38 PM
Check the API file. It may be calling 'break'

That isn't how break works. Break isn't a function rather than a statement, you can only break out of a loop in the same function.
Sewbacca #6
Posted 11 April 2016 - 06:34 PM
Check the API file. It may be calling 'break'

SquidDev is right, but also I have just this code.
Edited on 11 April 2016 - 04:38 PM
EveryOS #7
Posted 11 April 2016 - 07:07 PM
Check the API file. It may be calling 'break'

That isn't how break works. Break isn't a function rather than a statement, you can only break out of a loop in the same function.
I meant, like,
function breakAPI.doBreak()
  break
end
SquidDev #8
Posted 11 April 2016 - 08:22 PM
I meant, like,
function breakAPI.doBreak()
  break
end

That isn't how break works. Break isn't a function rather than a statement, you can only break out of a loop in the same function.


while true do
  break -- Valid. Inside a loop
end

break -- Invalid: outside a loop (you get a "invalid: no loop to break" syntax error when compiling)

local function doBreak() break end
while true do
  doBreak() -- Invalid: doBreak doesn't know anything about this loop. From its perspective, it isn't in a loop. You'll get the same error as above.
end
Edited on 11 April 2016 - 06:24 PM
Bomb Bloke #9
Posted 11 April 2016 - 11:34 PM
If the loop isn't repeating, then the most likely point of failure is your sensor call. Break it up into multiple lines, stick in more print statements, isolate the exact call that terminates the script.