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

light/alarm system manager

Started by cmurtheepic, 25 November 2012 - 01:41 PM
cmurtheepic #1
Posted 25 November 2012 - 02:41 PM
if someone could fix this up to where it works that would be great :D/>/>


local w,h = term.getSize()

local options={
"option1",
"option2",
"option3"
}
local n=CUI(options)
print(n)
local function logo()
print[[
(string.rep(" " w/2)cnet light/alarm manager(string.rep(" " w/2)
(string.rep("-" w
]]
sleep(2)
term.clear()
end
function CUI(m)
n=1
lights=#m
while true do
term.clear()
term.setCursorPos(w/2, 2)
for i=1, l, 1 do
if i==n then print(i, " ["..m[i].."]") else term.setCursorPos(w/2, 4) print(i, "alarm system", m[i]) end
end
term.setCursorPos(w/2, 6)
print("Select a number[arrow up/arrow down]")
a, b = os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=l then n=n+1 end
if b==28 then -- how would i toggle it. the bundled cable is on the back?
end
if b==28 and n==2 then
--toggle alarm system?
--again bundled cable is on the back.
end
end
term.clear() term.setCursorPos(1,1)
return n
end

logo()
Dlcruz129 #2
Posted 25 November 2012 - 02:48 PM
Format your code and we'll talk.
cmurtheepic #3
Posted 25 November 2012 - 04:11 PM
what do you mean

?
Dlcruz129 #4
Posted 25 November 2012 - 04:16 PM
what do you mean

?

The tab button is your friend.
Kingdaro #5
Posted 25 November 2012 - 04:26 PM
Vague sarcasm is a very rude and inefficient method of teaching.

@cmur, It's usually best to say the error/whatever isn't working instead of throwing a bunch of code at us and saying "fix it". We can't simply do all of your dirty work for you.
cmurtheepic #6
Posted 25 November 2012 - 09:57 PM
yeah ok. but the thing is i don't know :D/>/> how to make it toggle the bundled cable color so everytime the program runs it wil toggle it on or off.
ok.
thats what i am asking. :P/>/>
ChunLing #7
Posted 26 November 2012 - 07:26 AM
My spidy sense says you've got errors when you try to run this. Those would be helpful to trouble-shooting.

It also tells me that you haven't fully described the desired behavior of this program. That is kinda essential to giving you meaningful help.

Indentation is helpful, and it shows that you have made at least some effort to track the logical structure of the program. We like to see evidence that you are actually trying to fix the problem because there are people who are either lazy or outright trolls who waste time by posting code that doesn't work, then complain that it doesn't work right and ask everyone to fix it without bothering to say exactly what would constitute correct operation or updating the code with the suggested modifications.

I am not saying that you are lazy or a troll, just pointing out that you happen to be doing exactly what such people do.
cmurtheepic #8
Posted 26 November 2012 - 08:11 AM
i said listen to my earlier comment i don't know how to make it toggle bundled cable!!
ChunLing #9
Posted 26 November 2012 - 01:47 PM
Yes, you mentioned that. Now, check here and particularly here and then explain exactly what you don't understand, and how exactly you're trying to toggle the outputs.
Lyqyd #10
Posted 26 November 2012 - 02:24 PM
Guys, let's settle down a bit in here.

See if either of the useful snippets I've got posted in that thread are useful.
ChunLing #11
Posted 26 November 2012 - 02:54 PM
…those snippets might be more useful to novice programmers if you described what they do. Particularly the second, cause I had a moment trying to figure that out and I'm positive most novices would have no idea what it's doing or where to use it.
cmurtheepic #12
Posted 26 November 2012 - 05:17 PM
i agree with the guys who is being awfully rude! :(/>
ChunLing #13
Posted 26 November 2012 - 06:17 PM
It's okay, they do basically what the names suggest, they're just coded in an intimidating fashion and not commented.

setBundledColor(side, color, state) takes a string like "left", "right", "back" or whatever side the cables are on, an integer color value you want to set, and a boolean (true/false).

if state is true, then it checks whether the color you specified is off and turns it on (without affecting the other colors). If state is false it does the opposite, checks to see if the color is on and turns it off (again without bothering the other colors).

I would have written it like:
function setBundledColor(side, color, state)
    local currentOutput = rs.getBundledOutput(side)
    if state then
	    if not colors.test(currentOutput, color) then
		    rs.setBundledOutput(side, colors.combine(currentOutput, color))
	    end
    else
	    if colors.test(currentOutput, color) then
		    rs.setBundledOutput(side, colors.subtract(currentOutput, color))
	    end
    end
end
Okay, I would have written it totally different, but maybe this way is more readable? Probably not.

Anyway, toggleBundledColor(side, color) takes a string like "left", "right", or whatever, and an integer color value. Then it does something a little clever and sets the colors so that if the specified color was already on it is set to off and if it was off it gets set to on, it uses the logical operators to return colors.subtract() if colors.test() is true and colors.combine() if it is false. Again, I would have put
rs.getBundledOutput(side) in a local variable and used that rather than calling it two or three times, but I'm pretty certain that wouldn't help you understand what the function does.

With these functions, it's probably important to feed them color values that correspond to a single color rather than giving them color values corresponding to a combination of colors, because when the color.test returns false the code assumes that all of them were off, so the toggling doesn't quite work and the setting a color combo to off might fail even though some of the colors were on.
Doyle3694 #14
Posted 26 November 2012 - 09:02 PM
@dlcruz129, its kinda ironic how you tell him to format, indent, his code, and then when he asks howto, you tell him to use tab. tab sucks for indentingm, especially when you are going to move the code around. So you have a little to learn there too.
ChunLing #15
Posted 27 November 2012 - 12:17 AM
Tab is fine, as long as you are consistent about the editor you use and just copy/paste from/to it.

It's better than fine, I basically insist on having my indents performed by tab, because it's very convenient and friendly. Avoiding it because some editors don't support using it is like avoiding the spacebar because you once broke a keyboard playing Quake so that the spacebar didn't work…just use a keyboard with a working spacebar and don't worry that you happen to know that there are keyboards with broken spacebars somewhere in the world.
Doyle3694 #16
Posted 27 November 2012 - 03:03 AM
well yeah but code on the forum is doomed to be moved around. And that was not a very good comparison. because tab can be easy replaced. its more like saying you wont use a macro because the game you are playing doesn't support macros, so you just double press the button instead.

Easy: TAB is very unoptimal for moving code and doesnt work in some programs. TAB can easily be replaced.

Now back to topic!
cmurtheepic #17
Posted 27 November 2012 - 10:34 AM
ok just could someone please post a working version of my code :)/>
thank you.
KaoS #18
Posted 27 November 2012 - 10:54 AM
hey man, I will be leaving work soon so I don't have time to fully go through your code, I will just give you a function to toggle the colour of a bundled cable ok?


local function toggle(side,colour)
  if not colour then print('please enter a colour nex time...') return false end
  local current=rs.getBundledOutput(side)
  rs.setBundledOutput(side,colors[colours.test(current,colour) and 'subtract' or 'combine'](current,colour))
end

it is tested and it works

SYNTAX: toggle('back',colors.red)
cmurtheepic #19
Posted 28 November 2012 - 10:40 AM
thanks KaoS
KaoS #20
Posted 28 November 2012 - 10:57 AM
always a pleasure. good luck
Dlcruz129 #21
Posted 28 November 2012 - 12:58 PM
@dlcruz129, its kinda ironic how you tell him to format, indent, his code, and then when he asks howto, you tell him to use tab. tab sucks for indentingm, especially when you are going to move the code around. So you have a little to learn there too.

How would you format your code? (And don't say spacebar, the tab key in ComputerCraft is basically a macro for double-tapping space)
Lyqyd #22
Posted 28 November 2012 - 02:00 PM
That's enough off-topic chat about formatting, gents. If you'd like to continue your discussion, please take it to PM.