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

Combing three simple programs into one

Started by ledz99, 22 December 2016 - 02:52 PM
ledz99 #1
Posted 22 December 2016 - 03:52 PM
Hi, new here and to computercraft. New to coding aswell. Been hooked on it the past few days and with the help of some tutorials have done a few simple things.

Here I have created 3 simple programs, all the same thing, just going to different sides of the computer, and with different text/names to correspond to the light colors. They simply turn on and off a redstone output that leads to a light/set of lights, while giving a little text that tells you to input a character to switch them on and off.



Here is the code for the 'pinklight' program. The purple and blue ones have the same exact code except changing the "side" and some of the text to correspond correctly.



My question is what would I have to do in order to put all three of these programs into one program simply called 'lights' that when ran, gives the option to turn on/off any of them in any order, i.e. Turn pink on only, turn pink and blue. Purple and Pink. None, all.. you get the idea.

I know this could probably be a achieved a lot easier with Bundled cables, but I figured I'd try to do it with just running redstone into different sides for now, as a learning experience.

Thanks!
ledz99 #2
Posted 24 December 2016 - 06:50 AM
Wait what? Noo… what happened to the code example that Yami so kindly wrote for me? I didn't get a chance save it and now it's gone… :(/>(
Gumball #3
Posted 24 December 2016 - 08:29 AM
Use elseif statements.

Example:


a = math.random(1,2)
if(a == 1) then
  print("1")
elseif(a == 2) then
  print("2")
end

Also, when you set the value of a signal, make a variable the same, so that you can easily track if one is on or off, then set its state correspondingly.
Edited on 24 December 2016 - 07:30 AM
Bomb Bloke #4
Posted 24 December 2016 - 12:28 PM
Yami said:
When accepting user input, you can do multiple comparisons using an elseif. I would also recommend toggling input, rather than asking for on, then off.

If you have any trouble with either of these, I've written up a short example:

Spoiler
--#You don't have to draw each loop if the text doesn't change.
term.clear()
term.setCursorPos( 1, 1 )
print( "P - toggle pink" )
print( "B - toggle blue" )
print( "U - toggle purple" )
print( "To exit, press any key" )
--#we should loop while waiting for user input
while true do
  local event, userinput = os.pullEvent( "char" )
  userinput = userinput:upper() --# syntactical sugar for string.upper( userinput )
  if userinput == "P" then --# we can have an if / elseif statement for multiple comparisons
    rs.setOutput( "left", not rs.getOutput( "left" ) ) --# rs.getOutput returns whatever the current output is.  Not inverts that value
  elseif userinput == "U" then
    --#here you would toggle purple side
  elseif userinput == "B" then
    --#here you would toggle blue side
  else --# if none of the comparisons match, it must be a different key, and we should exit
    break
  end
end
--#after exiting the loop, we can do some cleanup
term.clear()
term.setCursorPos( 1, 1 )

PS: Thanks for indenting your code. It's really helpful ;)/>
ledz99 #5
Posted 25 December 2016 - 09:59 AM
Thanks guys much appreciated! With some tinkering and thinking it through I actually remembered it to my best and used basic logic to recreate what Yami had said and managed to get it working on my own! +1 for learning, right? :)/> Thanks again!