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

Double os.pullEvent

Started by Legless, 25 September 2012 - 09:55 AM
Legless #1
Posted 25 September 2012 - 11:55 AM
Sup, guys. I'm not very newbie in CC, but I can't solve one banal problem, may be I should just have a good sleep. But nevertheless I need your help.
What I have: Computer, bundled cable, a lot of colored cables laid to the various light sources. Simply speaking, it's a house illumination. And I want to manage it with computer. But I can't do one thing. So, the heart of the problem: for example, I want to switch on the light in the yard by white cable, I press "1". And if I want to switch off light in the same place, I should press "1" again. And so on. But in practice, I can't implement this part correct. Could you show me just a bare and correct framework of one PullEvent? I hope this can be solved without bunches of IF statements. May be advanced parameters?

If it's possible, show an initial part of code, where you set variables designating sum of all cables.
Or just some advices/hints, I really need them. Sorry for possible language mistakes.
Doyle3694 #2
Posted 25 September 2012 - 02:00 PM
local KeyPressed = 0
local LightOne = 0
local LightTwo = 0
local LightSum

while true do
event, KeyPressed = os.pullEvent("key")

if KeyPressed == 2 then
if LightOne == false then
LightOne = 1
else
LightOne = 0
end
end
if KeyPressed == 3 then
if LightTwo == false then
LightTwo = 2
else
LightTwo = 0
end
end
LightSum = LightOne + LightTwo
rs.setBundledOutput("back", LightSum)
end
Doyle3694 #3
Posted 25 September 2012 - 02:02 PM
OK THAT ONE WAS DUMB… not expandable at all, but what I came up with in 2 sec

EDIT: nvm my mind is backwards today :3 The code should work…
MetalMiner #4
Posted 25 September 2012 - 02:27 PM

local keys = {"1", "2", "3"}
local wires = {colors.white, colors.black, colors.brown}

while true do
  local event, key = os.pullEvent("char")
  for i = 1, #keys do
    if key == keys[i] then
      If colors.test(rs.getBundledInput("back"), wires[i]) then
        rs.setBundledOutput("back", colors.subtract(rs.getBundledInput("back"), wires[i]))
      else
        rs.setBundledOutput("back", colors.combine(rs.getBundledInput("back"), wires[i]))
      end
    end
  end
end
Can't test it now, but it should work :P/>/>
You can expand and change the values in the tables, but the first key in the first table must correspond to the first color in the second table, the second key to the second color, …
Hope I helped!
Legless #5
Posted 25 September 2012 - 04:40 PM
Wow, thank you, guys! I have never been working with tables, and I start regret of it. It really can replace a whole lot IF statements. And space. And time. It's time to study tutorials again.
Doyle3694 #6
Posted 25 September 2012 - 05:03 PM
Oh you did it the table way, thought I should not overcomplicate it…
MetalMiner #7
Posted 25 September 2012 - 06:07 PM
@legless
Yes, tables are verry useful, you can make much more using tables. And you can make it mich easier :P/>/>
Legless #8
Posted 25 September 2012 - 06:17 PM
Yeah, I managed to do this with IF statements like Doyle3694, but get confused after fifth cable (i'm coding in-game, SMP, with >500ms). And then suddenly creeper.
====

local keys = {"1", "2", "3"}
local wires = {colors.white, colors.black, colors.brown}

while true do
  local event, key = os.pullEvent("char")
  for i = 1, #keys do
	if key == keys[i] then
	  If colors.test(rs.getBundledInput("back"), wires[i]) then
		rs.setBundledOutput("back", colors.subtract(rs.getBundledInput("back"), wires[i]))
	  else
		rs.setBundledOutput("back", colors.combine(rs.getBundledInput("back"), wires[i]))
	  end
	end
  end
end

Hm, program switches on anything, but it terminates after second event key. [attempt to call nil]
This one
rs.setBundledOutput("back", colors.subtract(rs.getBundledInput("back"), wires[i]))
May be I should transfer colors into numbers?
Edited on 25 September 2012 - 05:13 PM
MetalMiner #9
Posted 25 September 2012 - 09:03 PM
Did you copy/pasted your code?
On my computer it works…
Perhaps you wrote something wrong?
If you write something wrong on a AIP (e.g. rs.setBdledOutput), it will throw this error.

P.S: You wrote If uppercase at the 2nd if statement. But that shouldn't be the problem.
Cranium #10
Posted 25 September 2012 - 09:15 PM
P.S: You wrote If uppercase at the 2nd if statement. But that shouldn't be the problem.
Lua is case sensitive, so yeah, it would be a problem. It is trying to use "If" as a variable.
MetalMiner #11
Posted 25 September 2012 - 09:42 PM
Yes, it is a problem, but not 'the' problem :P/>/>
This fault would throw an '=' expected error.
I guess, the fault is at a AIP, as i said.
Legless #12
Posted 26 September 2012 - 01:27 AM
Sorry, my fault. I have written "substract" instead of "subtract". All is working now! Thaks again.