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

[Help] with a menu

Started by cheekycharlie101, 23 September 2012 - 08:33 AM
cheekycharlie101 #1
Posted 23 September 2012 - 10:33 AM
ok so i have this menu set out. <– thanks to craniumkid for helping me code it.
anyway i have a problem. everytime i hit enter over a option on the menu it goes to the top of the menu and executes the right code. my problem is that i dont want it to go to the top. i want to hit enter over an option, then it executes the code AND it stays on the option i chose. heres my code :

options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
}
local function opt(m,mY)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
while true do
term.clear()
rednet.open("top")
local input = opt(options,0)
if input == 1 then
rednet.send(id,"payload,1")
elseif input == 2 then
rednet.send(id,"up,1")
elseif input == 3 then
rednet.send(id,"down,1")
end
end
all help would be appreciated. thanks -Cheeky
Mtdj2 #2
Posted 23 September 2012 - 12:18 PM
Well, instead of defining "n" in the function, define it in the very top of the code. This will make the program remember "n" and let the screen stay at the function you choose.
Hope this helped.
cheekycharlie101 #3
Posted 23 September 2012 - 07:04 PM
Well, instead of defining "n" in the function, define it in the very top of the code. This will make the program remember "n" and let the screen stay at the function you choose.
Hope this helped.
err, i dont really understand. sorry someone helped me write this so im not really sure what you mean
sjele #4
Posted 23 September 2012 - 07:06 PM

local function opt(m,mY)
n=1
l=#m
while true do
for i=1, l, 1 do
if i==n then
move the n = 1 to the top of code like this:
n = 1
--More vars
--more code
Anonomit #5
Posted 23 September 2012 - 07:14 PM
Well, instead of defining "n" in the function, define it in the very top of the code. This will make the program remember "n" and let the screen stay at the function you choose.
Hope this helped.
err, i dont really understand. sorry someone helped me write this so im not really sure what you mean

Make sure you code in the id of the turtle at the top.

Spoiler


id = 1 --change to turtle id #

n=1

options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
}

local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
while true do
term.clear()
rednet.open("top")
local input = opt(options,0)
if input == 1 then
rednet.send(id,"payload,1")
elseif input == 2 then
rednet.send(id,"up,1")
elseif input == 3 then
rednet.send(id,"down,1")
end
end

cheekycharlie101 #6
Posted 24 September 2012 - 06:17 PM
Well, instead of defining "n" in the function, define it in the very top of the code. This will make the program remember "n" and let the screen stay at the function you choose.
Hope this helped.
err, i dont really understand. sorry someone helped me write this so im not really sure what you mean

Make sure you code in the id of the turtle at the top.



id = 1 --change to turtle id #

n=1

options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
}

local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do
if i==n then
local x, y = term.getSize()
local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(">"..m[i].."<")
else
local x, y = term.getSize()
b = string.len(m[i])/2
x = (x/2)-b
term.setCursorPos(x,i+mY)
term.clearLine()
print(m[i]) end
end
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<l then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
while true do
term.clear()
rednet.open("top")
local input = opt(options,0)
if input == 1 then
rednet.send(id,"payload,1")
elseif input == 2 then
rednet.send(id,"up,1")
elseif input == 3 then
rednet.send(id,"down,1")
end
end

Thanks sooo much. it worked