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

Not working for some reason.

Started by bloodless2010, 21 October 2012 - 12:22 PM
bloodless2010 #1
Posted 21 October 2012 - 02:22 PM
OK, So I've been working on my remote controlled turtle system,
I've managed to do most of it but now when I'm polishing it up it doesn't work somehow,
It starts off with a screen asking you if you want to use the command control or key control,
in command control you can type exit to go back to the choice menu, but it doesn't work, but it DOES work on the key one?

tid = 379
thing = "none"
rednet.open("right")
other = true
function s()
sleep(.4)
end
while true do
if other == true then
term.clear()
term.setCursorPos(1,1)
print("Blood-Corp Turtle Systems")
print("Please enter control type (command or key) : ")
local test = read()
if test == "command" then
thing = "command"
elseif test == "key" then
thing = "key"
end
end
if thing == "command" then
command = true
while command == true do
term.clear()
term.setCursorPos(1,1)
print("Blood-Corp Turtle Systems")
print("Commands : up,down,left,right,forward,back,exit.")
term.setCursorPos(1,3)
write("Enter Command : ")
local command = read()
if command == "forward" then
rednet.send(tid, "forward")
print("Sent Forward")
s()
elseif command == "left" then
rednet.send(tid, "right")
print("Sent Left")
s()
elseif command == "right" then
rednet.send(tid, "left")
print("Sent Right")
s()
elseif command == "back" then
rednet.send(tid, "back")
print("Sent Back")
s()
elseif command == "up" then
rednet.send(tid, "up")
print("Sent Up")
s()
elseif command == "down" then
rednet.send(tid, "down")
print("Sent Down")
s()
elseif command == "exit" then
command = false
other = true
s()
else
print("Unknown Command.")
print("Usage: up,down,left,right,forward,back,exit.")
sleep(1)
end
end
elseif thing == "key" then
term.clear()
term.setCursorPos(1,1)
print("Blood-Corp Turtle Systems")
print("Keys : ](up), [(down), a(right), d(left).\nKeys : w(forward), s(back), z(exit).")
key = true
while key == true do
local evt, c = os.pullEvent("char")
c = string.lower(c)
if c == "w" then
rednet.send(tid, "forward")
print("Sent Forward")
elseif c == "d" then
rednet.send(tid, "right")
print("Sent Left")
elseif c == "a" then
rednet.send(tid, "left")
print("Sent Right")
elseif c == "s" then
rednet.send(tid, "back")
print("Sent Back")
elseif c == "]" then
rednet.send(tid, "up")
print("Sent Up")
elseif c == "[" then
rednet.send(tid, "down")
print("Sent Down")
elseif c == "z" then
key = false
other = true
s()
else
print("Unknown Key.")
print("Usage : ](up), [(down), a(right), d(left).\nUsage : w(forward), s(back), z(exit).")
sleep(1)
end
end
end
end

Thanks!
casr144 #2
Posted 21 October 2012 - 02:43 PM
That's weird, there doesn't seem to be any obvious problem that I can see. What actually happens when you run the program and try command? Is there an error? Or does it just freeze?
bloodless2010 #3
Posted 21 October 2012 - 02:47 PM
No error, no freezes, they key exit works but not the command exit.

on the command one, when I type exit, it just goes back to how it normally does when you type a command, it just refreshes so you can type another command
casr144 #4
Posted 21 October 2012 - 02:48 PM
I think it might be a variable type error. You initially set the variable 'command' equal to true - making it a bool. But later you then set it to a string. I'm not sure if lua allows this so I recommend using a different variable at the line: local command = read()
instead of 'command' again.

[EDIT]Sorry that's not the problem if the rest of the commands are working *facepalm*. Try putting a 'break on the last elseif statement - the one with the 'exit' code.
casr144 #5
Posted 21 October 2012 - 02:52 PM
I had a similar problem in my OS and solved it by putting a few 'break's in.
bloodless2010 #6
Posted 21 October 2012 - 02:55 PM
But I don't want it to exit to program, I want it to go back to the choice menu where you pick key or command.
casr144 #7
Posted 21 October 2012 - 02:58 PM
I take it that you can't just
shell.run(<program name>)
to go back to the choice stage?
bloodless2010 #8
Posted 21 October 2012 - 03:00 PM
I'll give it a go now, will edit with results.
sjele #9
Posted 21 October 2012 - 03:00 PM
Allso indention in code would help, as that makes it much easyer to see what belongs where. witch does what and so on


http://pastebin.com/5xBwgT23 –That is a quickly made example.
bloodless2010 #10
Posted 21 October 2012 - 03:04 PM
I take it that you can't just
shell.run(<program name>)
to go back to the choice stage?
Worked! Thanks, didn't know why it didn't work the other way :S
casr144 #11
Posted 21 October 2012 - 03:08 PM
lol np :)/>/>
Lyqyd #12
Posted 21 October 2012 - 05:50 PM
I take it that you can't just
shell.run(<program name>)
to go back to the choice stage?

This is a terrible idea. Please don't ever recommend this again. Using this to loop a program to a sufficient extent will overflow the stack.
ChunLing #13
Posted 21 October 2012 - 09:44 PM
Yeah. Just loop the program and use return to exit it where you want.