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

[Lua] Option Menu help

Started by Mattzz, 10 March 2012 - 11:39 PM
Mattzz #1
Posted 11 March 2012 - 12:39 AM
I know this code will be wrong, I was just testing stuff out to see if I could get it to work but anyway.
what i was trying to do was make an option menu but i dont know how to make it go back to the option menu
after you choose an option (bold is the menu). it can be changed (from functions to something else)
but the base stuff like names and what it does has to do the same thing.
also im not saying to rewrite my code im just saying that i would like the way to do it (like just say the commands and maybe a small example
and ill just rewrite my code)


Spoiler

local function loginScreen()
local password = "missile"
term.clear()
term.setCursorPos(11,2)
write("[Door Terminal]")
term.setCursorPos(6,3)
write("[Enter Security Password]")
term.setCursorPos(2,6)
write("Password: ")
status, input = pcall( read, "*" )
if input == password then
term.setCursorPos(2,7)
write("Password Correct")
sleep(2)
term.clear()
else
term.setCursorPos(2,7)
term.write("Password Incorrect")
pcall( sleep, 2 )
loginScreen()
end
end

local function clearScreen()
term.clear()
term.setCursorPos(2,1)
end

local function optionScreen()
[b]term.setCursorPos(5,1)
term.write("[Door Terminal]")
term.setCursorPos(2,2)
write("Please select an option")
term.setCursorPos(2,3)
write("[1] Open Door")
term.setCursorPos(2,4)
write("[2] Close Door")
term.setCursorPos(2,5)
write("[3] Lock System")
term.setCursorPos(2,6)
write("[4] Shutdown System")
term.setCursorPos(2,8)
write("Option: ")
status, option = pcall(read)
if option == "1" and redstone.getInput("back") == false then
clearScreen()
textutils.slowWrite("Opening Door")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
redstone.setOutput("left", true)
if redstone.getInput("back") == true then
redstone.setOutput("left", false)
redstone.setOutput("left", true)
end
write("Done")
optionScreen()
elseif option == "1" and redstone.getInput("back") == true then
clearScreen()
write("Door Already Open")
pcall( sleep, 2 )
optionScreen()
elseif option == "2" and redstone.getInput("back") == true then
clearScreen()
textutils.slowWrite("Closing Door")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
write(".")
pcall( sleep, 0.5 )
redstone.setOutput("left", false)
redstone.setOutput("left", true)
write("Done")
optionScreen()
elseif option == "2" and redstone.getInput("back") == false then
clearScreen()
write("Door Already Closed")
pcall( sleep, 2 )
clearScreen()
optionScreen()
elseif option == "3" then
os.reboot()
elseif option == "4" then
os.shutdown()
else
clearScreen()
write("Not a valid option")
pcall( sleep, 2 )
clearScreen()
optionScreen()[/b]
end
end

loginScreen()
optionScreen()
Liraal #2
Posted 11 March 2012 - 06:42 AM
use tables:

local options={"opt1","opt2"}
local functions={"print(1)", "print(2)"}
local n=1
for i=1,#options,1 do
write("["..i.."]  ")
if i==n then write("["..options[i].."]") else write(" "..options[i].." ") end
print("")
a,b=os.pullEvent()
if a == "key" and b==28 then
local tmp=loadstring(functions[n])
tmp()
end
end

The above uses two tables, i haven't coded the up/down arrows, you can do it by yourself. if you select 'opt1' it'll print 1 and if 'opt2', it'll print 2.
Advert #3
Posted 11 March 2012 - 07:13 AM
use tables:

local options={"opt1","opt2"}
local functions={"print(1)", "print(2)"}
local n=1
for i=1,#options,1 do
write("["..i.."]  ")
if i==n then write("["..options[i].."]") else write(" "..options[i].." ") end
print("")
a,b=os.pullEvent()
if a == "key" and b==28 then
local tmp=loadstring(functions[n])
tmp()
end
end

The above uses two tables, i haven't coded the up/down arrows, you can do it by yourself. if you select 'opt1' it'll print 1 and if 'opt2', it'll print 2.
Using loadstring like that is a really bad idea.
You can put functions in the table, instead.
Mattzz #4
Posted 11 March 2012 - 09:12 AM
Spoiler
use tables:

local options={"opt1","opt2"}
local functions={"print(1)", "print(2)"}
local n=1
for i=1,#options,1 do
write("["..i.."]  ")
if i==n then write("["..options[i].."]") else write(" "..options[i].." ") end
print("")
a,b=os.pullEvent()
if a == "key" and b==28 then
local tmp=loadstring(functions[n])
tmp()
end
end

The above uses two tables, i haven't coded the up/down arrows, you can do it by yourself. if you select 'opt1' it'll print 1 and if 'opt2', it'll print 2.
Using loadstring like that is a really bad idea.
You can put functions in the table, instead.

Well if you read the first post again you may notice I said
I want to know how to make it so that after it does the option it goes back to the option select menu
instead of it just calling the function over and over again inside of it. should i just make it a loop, will that do it
or is there something like labels(goto command in batch and foget the command in AHK)
thanks for any and all help

EDIT: Maybe i should have made the Opening Post clearer >.>

Mattzz
Liraal #5
Posted 11 March 2012 - 09:20 AM
Use a while true loop and a break somewhere inside. There's no goto that i know of in Lua.
Mattzz #6
Posted 11 March 2012 - 09:25 AM
my suspicions were correct thanks for answering so quick. I was right about what to do (damn why didn't i think of days ago. Derp)
also i didnt think there were any goto's in Lua but at least i know for sure there isn't
also can you explain how the code in your post works?
Liraal #7
Posted 11 March 2012 - 09:28 AM
sure

local options={"opt1","opt2"} –Table declaration
local functions={"print(1)", "print(2)"} –As above ;)/>/>
local n=1 –just a variable
for i=1,#options,1 do –for loop for all elements of 'options' table
write("["..i.."] ") –writes a number
if i==n then write("["..options[i].."]") else write(" "..options[i].." ") end –writes either 'option' or '[option]
print("") –next line because i don't like n
a,b=os.pullEvent() –waits for an event (most likely key press)
if a == "key" and b==28 then –if key is enter then
local tmp=loadstring(functions[n]) –load a function from the table to var tmp
tmp() –execute said function
end –well, end :mellow:/>/>
end
Mattzz #8
Posted 11 March 2012 - 09:42 AM
-Snip-
tmp() –execute said function
end –well, end :mellow:/>/>
-Snip-
Good observation Watson ;)/>/>
moving on ther are still a couple things i dont understand but i guess the best way to work it out
is to test it :)/>/>
oh and also, @Advert. What did you mean by replace with functions(example(1 line)?
Advert #9
Posted 11 March 2012 - 10:02 AM
-Snip-
tmp() –execute said function
end –well, end :mellow:/>/>
-Snip-
Good observation Watson ;)/>/>
moving on ther are still a couple things i dont understand but i guess the best way to work it out
is to test it :)/>/>
oh and also, @Advert. What did you mean by replace with functions(example(1 line)?
Using loadstring repeatedly is bad.
Lua is not interpreted; each time you call loadstring something like this will happen:
1) Compiler is loaded
2) The script being loaded is turned into bytecode
3) The bytecode will be loaded by the VM.

There are a few cases where you'll want to use loadstring, though:
1) User input
2) Doing something really complex, where loadstring may make it easier to code.
And even then, you'll probably want to cache the result of loadstring.

So, to give you an example of how you'd do this without loadstring:


local functions = {
 function() print(1) end,
 function() print(2) end
}
-- snip

if a == "key" and b==28 then
functions[n]()
end
--snip
Mattzz #10
Posted 11 March 2012 - 10:24 PM
Thanks Mate
Think i understand now
Thanks

Mattzz