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

Need Help With Events

Started by Mothraa, 17 February 2012 - 02:44 AM
Mothraa #1
Posted 17 February 2012 - 03:44 AM
I started learning lua about 2 days ago, and from various tutorials I constructed a security program. I then attempted to try to use functions and events in my next program, a flood gate control system. Everything works fine except the part where the program enters the main menu. It just continues to loop without doing the specified function when a certain key is pressed. Here's the link to the code: http://pastebin.com/TgtQ5HMx
Luanub #2
Posted 17 February 2012 - 04:16 AM
try changing

if event == "char" and param1 == "1" then open() end
if event == "char" and param1 == "2" then close() end
if event == "char" and param1 == "3" then break end

to

if event == "char" and param1 == "1" then
open()
elseif event == "char" and param1 == "2" then
close()
elseif event == "char" and param1 == "3" then
break
else
print ("invalid password entered")
menu()
end

you also need to add function to
password()
if user == debug then

so

function password()

you also need to call out your starting function in the script, so simply put menu() or password() or whatever as the last line of the code.
Luanub #3
Posted 17 February 2012 - 10:30 AM
Ok finally off work and got to play with this, been something I've wanted to work with some since I'm fairly new to lua scripting as well.

The first this I found is you were looking for the wrong event type, instead of char it should have been key. Rest was just tweaking the vars and the if else logic. Here is what I came up with.


function menu()
while true do
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint ("Main Menu")
print ("1. Open Gate")
print ("2. Close Gate")
print ("3. Exit")
local event, k1 = os.pullEvent()
if event == "key" then
	local event = k1
	if event == 2 then -- option 1 selected
		 open()
	elseif event == 3 then -- option 2 selected
		 close()
	elseif event == 4 then -- option 3 selected
		 break
	end
end
end
end

Hope that helps some.
Espen #4
Posted 17 February 2012 - 11:22 AM
You don't need to assign k1 to event before checking its value.
Instead, remove the line " local event = k1 " and change all the checks below that line from " event == " to "k1 ==" and it will do the same thing with less redundancy.
Mothraa #5
Posted 17 February 2012 - 11:13 PM
Thank you guys for the help!