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

[ERROR] [LUA]

Started by NegPos, 05 May 2012 - 06:13 PM
NegPos #1
Posted 05 May 2012 - 08:13 PM
I've been trying to make a simple interface for my base.
I get a bug where I start up the program, and it prints the "Not a valid command!" dialog, then shows the menu. However, upon a keypress, it exits the program.
Here's my code:


--Made by NegativePositive
--The +OS
sleep(1)
function home()
term.clear()
term.setCursorPos(1,1)
rednet.open("right")
rednet.open("top")
rednet.open("left")
rednet.open("back")
print("Welcome to the Mainframe!")
print("Choose one of the options:")
term.setCursorPos(1,4)
print("A: Turn the day/night sensor on or off.")
print("B: Harvest melons.")
print("C: Exit to CraftOS.")
event, param1, param2 = os.pullEvent()
end
term.clear()
if event == char and param1 == a then
rednet.send(1,"toggle")
print("Day/night sensor toggled!")
sleep(1)
home()
elseif event == char and param1 == b then
rednet.send(2, "harvest")
print("Melons harvested!")
sleep(1)
home()
elseif event == char and param1 == c then
	term.clear()
	shell.exit()
else
print("Not a valid command!")
sleep(1)
home()
end
Luanub #2
Posted 05 May 2012 - 09:42 PM
You never really call the home function to get things started so it runs your if statement first. So it immediately tries to check the answer even though there isn't one.

You also need to add ""(two double quotes) around the strings you are checking for in your if statements. Without the quotes its looking for a var named char or a etc… I corrected below and commented.

Also since it appears you are working with just the char event type you could change your event pull to event, param1, param2, = os.pullEvent("char") which will filter out any other event types. This would allow you to simplify your if statements some.

I did not test this so let me know if it doesnt work.

Try doing this..

--Made by NegativePositive
--The +OS
sleep(1)
function home()
term.clear()
term.setCursorPos(1,1)
rednet.open("right")
rednet.open("top")
rednet.open("left")
rednet.open("back")
print("Welcome to the Mainframe!")
print("Choose one of the options:")
term.setCursorPos(1,4)
print("A: Turn the day/night sensor on or off.")
print("B: Harvest melons.")
print("C: Exit to CraftOS.")
event, param1, param2 = os.pullEvent()
end

home() -- added so it will run home() before going on to check the answer
term.clear()
if event == "char" and param1 == "a" then
rednet.send(1,"toggle")
print("Day/night sensor toggled!")
sleep(1)
home()
elseif event == "char" and param1 == "b" then
rednet.send(2, "harvest")
print("Melons harvested!")
sleep(1)
home()
elseif event == "char" and param1 == "c" then
		term.clear()
		shell.exit()
else
print("Not a valid command!")
sleep(1)
home()
end
NegPos #3
Posted 05 May 2012 - 10:14 PM
You never really call the home function to get things started so it runs your if statement first. So it immediately tries to check the answer even though there isn't one.

You also need to add ""(two double quotes) around the strings you are checking for in your if statements. Without the quotes its looking for a var named char or a etc… I corrected below and commented.

Also since it appears you are working with just the char event type you could change your event pull to event, param1, param2, = os.pullEvent("char") which will filter out any other event types. This would allow you to simplify your if statements some.

I did not test this so let me know if it doesnt work.

Try doing this..

--Made by NegativePositive
--The +OS
sleep(1)
function home()
term.clear()
term.setCursorPos(1,1)
rednet.open("right")
rednet.open("top")
rednet.open("left")
rednet.open("back")
print("Welcome to the Mainframe!")
print("Choose one of the options:")
term.setCursorPos(1,4)
print("A: Turn the day/night sensor on or off.")
print("B: Harvest melons.")
print("C: Exit to CraftOS.")
event, param1, param2 = os.pullEvent()
end

home() -- added so it will run home() before going on to check the answer
term.clear()
if event == "char" and param1 == "a" then
rednet.send(1,"toggle")
print("Day/night sensor toggled!")
sleep(1)
home()
elseif event == "char" and param1 == "b" then
rednet.send(2, "harvest")
print("Melons harvested!")
sleep(1)
home()
elseif event == "char" and param1 == "c" then
		term.clear()
		shell.exit()
else
print("Not a valid command!")
sleep(1)
home()
end

thanks a lot man. I never worked with functions before, and didn't really even know what they did.

Edit: The same thing happens, but the "Not a command!" dialog waits until a key is pressed.
Luanub #4
Posted 06 May 2012 - 12:59 AM
Ya I figured it might have problems. Try this one. Unfortunately I'm not at home so I cant test anything.

I basically put everything in the function and placed it in a while loop. I also changed the os.pullEvent() into a read()


function home()
while true do  -- added to make it loop
term.clear()
term.setCursorPos(1,1)
rednet.open("right")
rednet.open("top")
rednet.open("left")
rednet.open("back")
print("Welcome to the Mainframe!")
print("Choose one of the options:")
term.setCursorPos(1,4)
print("A: Turn the day/night sensor on or off.")
print("B: Harvest melons.")
print("C: Exit to CraftOS.")
local input = string.lower(read()) -- changed to a read function, that converts user input to lowercase
term.clear()
if input == "a" then
   rednet.send(1,"toggle")
   print("Day/night sensor toggled!")
sleep(1)
elseif input == "b" then
   rednet.send(2, "harvest")
   print("Melons harvested!")
   sleep(1)
elseif input == "c" then
   term.clear()
   shell.exit()
else
   print("Not a valid command!")
   sleep(1)
end
end
end

home()

If this is all the program is going to do, you can remove the function, the end that finishes it and the function call and it should still work.
Edited on 05 May 2012 - 11:00 PM
NegPos #5
Posted 06 May 2012 - 06:47 PM
Ya I figured it might have problems. Try this one. Unfortunately I'm not at home so I cant test anything.

I basically put everything in the function and placed it in a while loop. I also changed the os.pullEvent() into a read()


function home()
while true do  -- added to make it loop
term.clear()
term.setCursorPos(1,1)
rednet.open("right")
rednet.open("top")
rednet.open("left")
rednet.open("back")
print("Welcome to the Mainframe!")
print("Choose one of the options:")
term.setCursorPos(1,4)
print("A: Turn the day/night sensor on or off.")
print("B: Harvest melons.")
print("C: Exit to CraftOS.")
local input = string.lower(read()) -- changed to a read function, that converts user input to lowercase
term.clear()
if input == "a" then
   rednet.send(1,"toggle")
   print("Day/night sensor toggled!")
sleep(1)
elseif input == "b" then
   rednet.send(2, "harvest")
   print("Melons harvested!")
   sleep(1)
elseif input == "c" then
   term.clear()
   shell.exit()
else
   print("Not a valid command!")
   sleep(1)
end
end
end

home()

If this is all the program is going to do, you can remove the function, the end that finishes it and the function call and it should still work.

It works, but I'm going to put a break in there so I can exit the program.