Jump to content




[Help] with a menu


  • You cannot reply to this topic
5 replies to this topic

#1 cheekycharlie101

  • Members
  • 231 posts

Posted 23 September 2012 - 09: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

#2 Mtdj2

  • Members
  • 66 posts
  • LocationBehind you

Posted 23 September 2012 - 11:18 AM

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.

#3 cheekycharlie101

  • Members
  • 231 posts

Posted 23 September 2012 - 06:04 PM

View PostMtdj2, on 23 September 2012 - 11:18 AM, said:

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

#4 sjele

  • Members
  • 334 posts
  • LocationSomewhere on the planet called earth

Posted 23 September 2012 - 06: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


#5 Anonomit

  • Members
  • 67 posts

Posted 23 September 2012 - 06:14 PM

View Postcheekycharlie101, on 23 September 2012 - 06:04 PM, said:

View PostMtdj2, on 23 September 2012 - 11:18 AM, said:

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


#6 cheekycharlie101

  • Members
  • 231 posts

Posted 24 September 2012 - 05:17 PM

View PostAnonomit, on 23 September 2012 - 06:14 PM, said:

View Postcheekycharlie101, on 23 September 2012 - 06:04 PM, said:

View PostMtdj2, on 23 September 2012 - 11:18 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users