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

Using if statements for combinations of multicolor inputs

Started by Lazymuffin, 29 June 2012 - 03:30 PM
Lazymuffin #1
Posted 29 June 2012 - 05:30 PM
I'm trying to build a long monitor that changes the display based on which of the three levers connected are turned on

The only way I can imagine to do this is by hardcoding the 9 possible combinations of the levers to say
Online Offline Offline
Online Online Offline
etc…

but I can't figure out how to use conditional statements to detect more than one input at once.

this is how I'm failing horribly right now


if (rs.testBundledInput("left", colors.white) (rs.testBundledInput("left", colors.orange) then
        print("online online offline")
  elseif (rs.testBundledInput("left", colors.white) then
        print("online offline offline")
end

I'm new to whole coding scheme and I feel like this is probably a really inefficient way to go about everything but I don't enough to do it any other way.
Mads #2
Posted 29 June 2012 - 05:38 PM
You can use this:

if (condition 1) and (condition 2) then –[
] end
Lazymuffin #3
Posted 29 June 2012 - 05:50 PM
this is returning a "Too long without yielding" error


if  (rs.testBundledInput("left", colors.white)) and (rs.testBundledInput("left", colors.orange)) then
print("online online offline")
else
print ("offline offline offline")
end
OmegaVest #4
Posted 29 June 2012 - 05:51 PM
Or, you can use standard ifs and have it output to a string or table to string.


function checkItOut()
   tablet = {"offline", "offline", "offline"}

   if rs.testBundledInput("left", colors.white) then
	  tablet[1] = "online"
   end
   if rs.testBundledInput("left", colors.orange) then
	  tablet[2] = "online"
   end
   if rs.testBundledInput("left", 4) then	--Forgot what third color was.
	  tablet[3] = "online"
   end
   outString = tablet[1] .. " " .. tablet[2] .. " " .. tablet[3]
   print(outString)
end

Put this in a function, and call the function when you check for the inputs. It will automatically reset to saying "offline" before checking to see if they are active. Therefore, when you pull up the readout, if any are offline after having been online, it will show that.


Too long without yielding: You have this in a while loop with no pauses. Use a os.pullEvent(). Either have a timer or a redstone change as the yield.





while true do
   ticker = os.startTimer(1.0)
   event, arg1, arg2 = os.pullEvent()
   if event  == "timer" and arg1 == ticker then
	  checkItOut()
	  shell.run("clear")   -- Clears the screen. Omit if you want a readout log to be left
   elseif event == "redstone" then
	  checkItOut()
	  shell.run("clear")   -- Clears the screen. Omit if you want a readout log to be left
   elseif event == "key" and key = 16 then  -- Press q I think. That or tab.
	  break  -- Ends the loop
   end
end
Lazymuffin #5
Posted 29 June 2012 - 06:16 PM

elseif event == "key" and key = 16 then

throws an "Expecting 'then'" error


elseif (event == "key") and (key = 16) then

throws an "Expecting ')'" error
Lyqyd #6
Posted 29 June 2012 - 06:22 PM

elseif event == "key" and key = 16 then

throws an "Expecting 'then'" error


elseif (event == "key") and (key = 16) then

throws an "Expecting ')'" error

No parentheses needed. Just add the second = in the second comparison.


key == 16
Lazymuffin #7
Posted 29 June 2012 - 06:34 PM
Thanks! I don't really understand the tablet thing that OmegaVest gave me

does

tablet = {"offline", "offline", "offline"}
automatically assign the first "offline" to tablet[1] and everything after it subsequently?

the full chunk is


function checkItOut()
   tablet = {"offline", "offline", "offline"}

   if rs.testBundledInput("left", colors.white) then
          tablet[1] = "online"
   end
   if rs.testBundledInput("left", colors.orange) then
          tablet[2] = "online"
   end
   if rs.testBundledInput("right", colors.magenta) then
          tablet[3] = "online"
   end
   outString = tablet[1] .. " " .. tablet[2] .. " " .. tablet[3]
   print(outString)
end


it doesn't seem to be outputting info when i run the program even with a loop, I don't know what I have to change.

I kinda want to understand whats going on just so I can use this function for other programs either way.
OmegaVest #8
Posted 29 June 2012 - 07:21 PM
Err. . . That should have put everything into an array/table that places it in slots 1, 2 and 3.

If you prefer, replace that line with


tablet = {}
tablet[1] = "offline"
tablet[2] = "offline"
tablet[3] = "offline"

Other than that, it should work correctly. If not, someone will probably show me how stupid my code is. And if you can't tell, I'm not the best at this. Though, I'm making this code up as I go so . . .

To debug, though, put a set of prints prior to the concatenation (where I use .. 's) that print out the table's supposed slots. If none come up, I need to rethink. Presently, I'm not able to test this in CC myself.



Further: I was calling a array originally, then a table, which in Lua is more or less an expanded array. When you call a table, you can use anything for its index. So, I can make a table where a greek letter's english spelling is its index, such as tablet["alpha"] or tablet["upsilon"]. Arrays, generally, are called as a single line. When I did the tablet = {…}, it was supposed to make a single-dimension table whose indexes were ordered integers. So, it should have had tablet[1], tablet[2] and so forth. For whatever reason I have either forgotten something or mixed up a character.

And I did. I should have put the above code in anyway, instead of how i had them before. Yay for learning. :P/>/>
Lazymuffin #9
Posted 29 June 2012 - 08:16 PM
Yeah, I have no clue, it's not working.

Doesn't print anything. might have something to do with the clock.

Edit:

So yeah, it was the timer. no clue what was wrong with it. might not have registered "Checkthisout" as a function properly
OmegaVest #10
Posted 29 June 2012 - 09:45 PM
Hmm. Well I only put that in there because I was unsure of your intentions with the program. For a simple monitor system, then you only need the redstone check, and you will probably want to think about using a clear function, if you are using it like a readout in a factory or nuclear plant.
Lazymuffin #11
Posted 30 June 2012 - 12:08 AM
Yes it will be updated via redstone.

I seriously can't thank you enough, it was driving me crazy!