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

pullevent issue

Started by junithorn, 03 October 2012 - 06:05 PM
junithorn #1
Posted 03 October 2012 - 08:05 PM
Anyone gimme a hand with this? It runs the elevate function upon starting or any key being pressed for some reason that evades me.

thanks


function clear()
   term.clear()
   term.setCursorPos (1,1)
end

local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), (y / 2))
write(text)
end

function elevate()
for i = 1,40 do
rs.setBundledOutput("bottom", colors.blue)
sleep(0.2)
rs.setBundledOutput("bottom", colors.black)
sleep(0.2)  
end
  
   sleep(4)
for o = 1,40 do
	rs.setBundledOutput("bottom", colors.red)
	sleep(0.2)
	rs.setBundledOutput("bottom", colors.black)
	sleep(0.2)
end	

  sleep(1)
 shell.run("elevator")

end

while true do
   clear()
   rednet.open("back")
  
   centerText ("Secure area, clearance required.")
   local event, param1 = os.pullEvent ()
if event == "char" or "rednet_message" then
   if param1 == "]" or "215" then
	
	elevate()
  
   else
	   sleep (1.5)
	   shell.run("elevator")
   end
  else
   sleep(1.5)
   shell.run("elevator")
end
end
GopherAtl #2
Posted 03 October 2012 - 08:11 PM
the problem is with "event == "char" or "rednet_message"". == only compares two values, and resolves first, so this as written means "If the event is equal to char, or, the literal string "rednet_message" is not false." A string literal is never false, so the second condition is always true.

You have to do a full condition on both sides of or, ex, 'event=="char" or event=="rednet_message"'
junithorn #3
Posted 03 October 2012 - 08:50 PM
hmm, good observation but with that change it doesn't seem to accept anything as a command
junithorn #4
Posted 03 October 2012 - 09:45 PM
Here's what it looks like now, nothing seems to make it elevate()


function clear()
   term.clear()
   term.setCursorPos (1,1)
end
local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), (y / 2))
write(text)
end
function elevate()
for i = 1,40 do
rs.setBundledOutput("bottom", colors.blue)
sleep(0.2)
rs.setBundledOutput("bottom", colors.black)
sleep(0.2)
  
end
  
   sleep(4)
for o = 1,40 do
    rs.setBundledOutput("bottom", colors.red)
    sleep(0.2)
    rs.setBundledOutput("bottom", colors.black)
    sleep(0.2)
end	 
sleep(1)
shell.run("elevator")
end
while true do
   clear()
   rednet.open("back")
  
   centerText ("Secure area, clearance required.")
   local event, param1 = os.pullEvent()
   if param1 == "*" then
	
    elevate()
  
   elseif param1 == "215" then
    elevate()
   else
	
   sleep(1.5)
   shell.run("elevator")

end
end
Cranium #5
Posted 03 October 2012 - 11:04 PM
You shouldn't keep using shell.run("elevator"). That will eventually crash your terminal. Instead, use os.reboot() if you want the program to restart.
If you are trying to use a specific key to activate the elevator, use this: http://www.minecraftwiki.net/wiki/Key_Codes
It should look something like this:

local event, p1 = os.pullEvent("key")
if p1 == 200 then --(this is the keycode for up arrow)
elevate()
else
--do whatever else if not up arrow
end