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

Need serious help with menu's

Started by cheekycharlie101, 03 November 2012 - 12:21 PM
cheekycharlie101 #1
Posted 03 November 2012 - 01:21 PM
pleaseeee help me
i need to print a title at the top of my menu. this is a menu for a program i coded called turtle control. it wirelessly controls turtles using a menu. here is my code:


id = 15 --change to turtle id #
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
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") --Change to the side of your modem
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")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end

please note i only coded some of the code. some i got from a tutorial so im not 100% sure what ends end what part of the code.
all help would be grateful.
thanks -Cheeky
diegodan1893 #2
Posted 03 November 2012 - 11:05 PM
You clear the screen in the for bucle, so you need to write the title in the same for bucle that prints the options. This should work:


id = 15 --change to turtle id #
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do


--Print the title here
term.setCursorPos(1,1) --set the cursor where you want your title
print("Title") --And print it

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") --Change to the side of your modem
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")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end
cheekycharlie101 #3
Posted 04 November 2012 - 03:33 AM
You clear the screen in the for bucle, so you need to write the title in the same for bucle that prints the options. This should work:


id = 15 --change to turtle id #
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do


--Print the title here
term.setCursorPos(1,1) --set the cursor where you want your title
print("Title") --And print it

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") --Change to the side of your modem
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")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end
thanks, just one problem. if you print it in the center it prints over the first option.
is there any way you can help me make the options 1 line down?
thanks -Cheeky
diegodan1893 #4
Posted 04 November 2012 - 09:26 AM
thanks, just one problem. if you print it in the center it prints over the first option.
is there any way you can help me make the options 1 line down?
thanks -Cheeky

Yes, this should work well:

id = 15 --change to turtle id #
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do

--Print the title here
term.setCursorPos(1,1) --set the cursor where you want your title
print("Title") --And print it
if i==n then
local x, y = term.getSize()

y = y - 1 --I made this because the first line has the title

local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b

term.setCursorPos(x,i+mY + 1) --add 1 to the Y value to set the cursor one line down

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 + 1) -- same here

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") --Change to the side of your modem
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")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end

I added 1 to the Y value to make the options appear 1 line down.

I didn't tested the code, so tell me if there is a problem.
cheekycharlie101 #5
Posted 04 November 2012 - 10:06 AM
thanks, just one problem. if you print it in the center it prints over the first option.
is there any way you can help me make the options 1 line down?
thanks -Cheeky

Yes, this should work well:

id = 15 --change to turtle id #
n=1
options = {
"Drop Payload",
"Go Up",
"Go Down",
"Go Left",
"Go Right",
"Go Forward",
"Go Backward",
"Dig",
"Turn Left",
"Turn Right",
}
local function opt(m,mY)
l=#m
while true do
for i=1, l, 1 do

--Print the title here
term.setCursorPos(1,1) --set the cursor where you want your title
print("Title") --And print it
if i==n then
local x, y = term.getSize()

y = y - 1 --I made this because the first line has the title

local b = string.len(">"..m[i].."<")/2
local x = (x/2)-b

term.setCursorPos(x,i+mY + 1) --add 1 to the Y value to set the cursor one line down

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 + 1) -- same here

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") --Change to the side of your modem
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")
elseif input == 4 then
rednet.send(id,"left,1")
elseif input == 5 then
rednet.send(id,"right,1")
elseif input == 6 then
rednet.send(id,"forward,1")
elseif input == 7 then
rednet.send(id,"back,1")
elseif input == 8 then
rednet.send(id,"dig,1")
elseif input == 9 then
rednet.send(id,"lLeft,1")
elseif input == 10 then
rednet.send(id,"lRight,1")
end
end

I added 1 to the Y value to make the options appear 1 line down.

I didn't tested the code, so tell me if there is a problem.
Il test it now see if it works. thanks so much for the help
Edit
Works great! :DDDD thanks soooo much man