Jump to content




Combing three simple programs into one


  • You cannot reply to this topic
4 replies to this topic

#1 ledz99

  • Members
  • 7 posts

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.

Posted Image

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.

Posted Image

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!

#2 ledz99

  • Members
  • 7 posts

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... :((

#3 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

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 by BlueBird, 24 December 2016 - 08:30 AM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 24 December 2016 - 12:28 PM

Yami's post from Google Cache 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

PS: Thanks for indenting your code. It's really helpful ;)


#5 ledz99

  • Members
  • 7 posts

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users