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

If Detection Help.

Started by SNWLeader, 22 January 2013 - 02:46 AM
SNWLeader #1
Posted 22 January 2013 - 03:46 AM
I am going to be producing a Food Market with a computer. I wanted to know how to set up an if statement to when someone presses a button.
It would be nice to have it detect based on the Red Power Color.

So Ya I am new to the detection system……so I am playing around with it.
remiX #2
Posted 22 January 2013 - 04:24 AM
For this, you will need to use a loop for it to constantly check for the redstone signal


while true do
    os.pullEvent("redstone") -- so it only continues when a redstone event is trigger and it will act as a yield
    if rs.testBundledInput("side", colours.red) then -- so if the colours.red part of the bundled cable is ON
        --code
    else
        --code when it's not
    end
end

You can test multiple colours with if..elseif..elseif..end


if .. then

elseif .. then

elseif .. then

end
DrLancer #3
Posted 22 January 2013 - 04:51 AM
You can test multiple colours with if..elseif..elseif..end


if .. then

elseif .. then

elseif .. then

end

Would it be possible to use OR in this case ex.



while true do
  os.pullEvent("redstone")
  if rs.testBundledInput("side", colours.red) or rs.testBundledInput("side", colors.blue) then
    --code if it's red or blue
  elseif rs.testBundledInput("side", colours.green) then 
    --code if it's green
  else
    --code when it's neither
  end
end
SuicidalSTDz #4
Posted 22 January 2013 - 04:56 AM
You can test multiple colours with if..elseif..elseif..end


if .. then

elseif .. then

elseif .. then

end

Would it be possible to use OR in this case ex.



while true do
  os.pullEvent("redstone")
  if rs.testBundledInput("side", colours.red) or rs.testBundledInput("side", colors.blue) then
	--code if it's red or blue
  elseif rs.testBundledInput("side", colours.green) then
	--code if it's green
  else
	--code when it's neither
  end
end

It would be possible but aren't you detecting a certain color? Or you want [said code] to run if the red OR blue cable is on? Which in that case it would be alright to use an OR statement. Also I wish you the best of luck with your Food Market ;)/>

EDIT: You will however have to change the rs.testBundledInput("side",colors.red) Since "side" is not a valid side unless you define a variable like so:

Side = "[Side the cable is on]" –This must be in the form of a string -> " "
–code

Then call the variable like so:
rs.testBundledInput(side,colors.red)
remiX #5
Posted 22 January 2013 - 05:03 AM
It would indeed work