[lua] Can't figure out why...
#1
Posted 04 January 2013 - 12:15 AM
http://pastebin.com/yKxec9R9
#2
Posted 04 January 2013 - 01:09 AM
#3
Posted 04 January 2013 - 01:34 AM
#4
Posted 04 January 2013 - 02:33 AM
remiX, on 04 January 2013 - 01:34 AM, said:
You can, try it....
function test12345()
print("You're gona be surprised")
end
current = test12345
current()
#5
Posted 04 January 2013 - 02:59 AM
But the way you're making the menu systems is weird, you should use a table with the options and then print the options.
Example:
local function menu(_table)
local sel = 1
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
if sel > #_table then sel = 1 end
if sel < 1 then sel = #_table end
for i = 1,#_table do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print("[".._table[i].."]")
else
print(" ".._table[i].." ")
end
end
local e, key = os.pullEvent("key")
if key == 200 then -- up key
sel = sel-1
elseif key == 208 then -- down key
sel = sel+1
elseif key == 28 then
term.setCursorPos(curX,curY)
return _table[sel], sel
end
end
end
-- Table with options
tab = {"Option 1", "Option 2"}
-- Call it like this
selection = menu(tab)
if selection == tab[1] then
-- Option 1 was selected
elseif selection == tab[2] then
-- Option 2 was selected
end
#6
Posted 04 January 2013 - 03:12 AM
#7
Posted 04 January 2013 - 05:19 AM
remiX, on 04 January 2013 - 02:59 AM, said:
But the way you're making the menu systems is weird, you should use a table with the options and then print the options.
Example:
local function menu(_table)
local sel = 1
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
if sel > #_table then sel = 1 end
if sel < 1 then sel = #_table end
for i = 1,#_table do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print("[".._table[i].."]")
else
print(" ".._table[i].." ")
end
end
local e, key = os.pullEvent("key")
if key == 200 then -- up key
sel = sel-1
elseif key == 208 then -- down key
sel = sel+1
elseif key == 28 then
term.setCursorPos(curX,curY)
return _table[sel], sel
end
end
end
-- Table with options
tab = {"Option 1", "Option 2"}
-- Call it like this
selection = menu(tab)
if selection == tab[1] then
-- Option 1 was selected
elseif selection == tab[2] then
-- Option 2 was selected
end
I was gona use table but i don't understand them, tried looking at tutorials but didn't get it...
Even still should my code not work? even if it is werid?!?
#8
Posted 04 January 2013 - 05:50 AM
WIth the code I gave you, you don't even need to tables:
local function menu(...)
local sel = 1
list = {...}
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
if sel > #list then sel = 1 end
if sel < 1 then sel = #list end
for i = 1,#list do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print("["..list[i].."]")
else
print(" "..list[i].." ")
end
end
local e, key = os.pullEvent("key")
if key == 200 then -- up key
sel = sel-1
elseif key == 208 then -- down key
sel = sel+1
elseif key == 28 then
term.setCursorPos(curX,curY)
return list[sel], sel
end
end
end
-- Call it like this
selection, number = menu("Option 1", "Option 2")
if selection == "Option 1" then -- You can also use the 'number' variable, here it would be 1
-- Option 1 was selected
elseif selection == "Option 2" then -- number will be 2
-- Option 2 was selected
end
Edit: Remove these lines from each of the last 3 functions with os.pullEvent
if event == "key" then
else
current()
end
if key ~= 28 or 200 or 208 then
current()
end
And then when you make mMselection = 2, there is no if statement for if it is 2.
Also, you can use if elseif else end, instead of each block being an if statement.
#9
Posted 04 January 2013 - 06:16 AM
remiX, on 04 January 2013 - 05:50 AM, said:
WIth the code I gave you, you don't even need to tables:
local function menu(...)
local sel = 1
list = {...}
local offX,offY = term.getCursorPos()
local curX,curY = term.getCursorPos()
while true do
if sel > #list then sel = 1 end
if sel < 1 then sel = #list end
for i = 1,#list do
term.setCursorPos(offX,offY+i-1)
if sel == i then
print("["..list[i].."]")
else
print(" "..list[i].." ")
end
end
local e, key = os.pullEvent("key")
if key == 200 then -- up key
sel = sel-1
elseif key == 208 then -- down key
sel = sel+1
elseif key == 28 then
term.setCursorPos(curX,curY)
return list[sel], sel
end
end
end
-- Call it like this
selection, number = menu("Option 1", "Option 2")
if selection == "Option 1" then -- You can also use the 'number' variable, here it would be 1
-- Option 1 was selected
elseif selection == "Option 2" then -- number will be 2
-- Option 2 was selected
end
Edit: Remove these lines from each of the last 3 functions with os.pullEvent
if event == "key" then else current() end if key ~= 28 or 200 or 208 then current() end
And then when you make mMselection = 2, there is no if statement for if it is 2.
Also, you can use if elseif else end, instead of each block being an if statement.
I've just realised all the 0 should be 2 in the if statements...
Also why remove these functions? They are needed to ensure other event aren't interfering and that they entered valid keys...
#10
Posted 04 January 2013 - 06:27 AM
Also, I used elseif's here so you can see how it's used:
function mMenuKI()
event , key = os.pullEvent()
-- Surround the code within an if statement
if event == "key" then
if key == 28 then--ENTER KEY
if mMselction == 1 then --SELECTED E-Mail Network
EMailNetworkMenu()
elseif mMselction == 2 then --SELECTED Exit
exitP()
end
elseif key == 200 and mMselction == 1 then --ARROW KEY
mMenu()
elseif key == 200 and mMselction == 2 then --ARROW KEY
mMenu()
elseif key == 208 and mMselction == 1 then --ARROW KEY
mvMenu()
elseif key == 208 and mMselction == 2 then --ARROW KEY
mvMenu()
end
end
end
#11
Posted 04 January 2013 - 07:09 AM
remiX, on 04 January 2013 - 06:27 AM, said:
Also, I used elseif's here so you can see how it's used:
function mMenuKI() event , key = os.pullEvent() -- Surround the code within an if statement if event == "key" then if key == 28 then--ENTER KEY if mMselction == 1 then --SELECTED E-Mail Network EMailNetworkMenu() elseif mMselction == 2 then --SELECTED Exit exitP() end elseif key == 200 and mMselction == 1 then --ARROW KEY mMenu() elseif key == 200 and mMselction == 2 then --ARROW KEY mMenu() elseif key == 208 and mMselction == 1 then --ARROW KEY mvMenu() elseif key == 208 and mMselction == 2 then --ARROW KEY mvMenu() end end end
No idea what on earth you have done but it works
Im now just changing the code a bit to get what i want ill post if i don't understand something or have hit a brick wall...
#12
Posted 04 January 2013 - 07:21 AM
---------------------------------------
--MAIN MENU KEY INTERACTION
function mMenuKI()
event , key = os.pullEvent()
-- Surround the code within an if statement
if event == "key" then
if key ~= 28 or 200 or 208 then
current()
elseif key == 28 then--ENTER KEY
if mMselction == 1 then --SELECTED E-Mail Network
EMailNetworkMenu()
elseif mMselction == 2 then --SELECTED Exit
exitP()
end
elseif key == 200 and mMselction == 1 then --ARROW KEY
mMenu()
elseif key == 200 and mMselction == 2 then --ARROW KEY
mMenu()
elseif key == 208 and mMselction == 1 then --ARROW KEY
mvMenu()
elseif key == 208 and mMselction == 2 then --ARROW KEY
mvMenu()
end
end
end
----------------------------------------
When adding in the if statement so if they hit like f it does nothing (Which doesn't stop them i just tried...) it now also means you can't use the arrow keys i tried implimenting it diffrent ways but it just doesn't work...
#13
Posted 04 January 2013 - 07:43 AM
if key ~= 28 or key ~= 200 or key ~= 208 then
But you don't need that! The one I posted should work fine.
#15
Posted 04 January 2013 - 08:04 AM
function mMenuKI()
while true do
event , key = os.pullEvent()
-- Surround the code within an if statement
if event == "key" then
if key == 28 then--ENTER KEY
if mMselction == 1 then --SELECTED E-Mail Network
EMailNetworkMenu()
elseif mMselction == 2 then --SELECTED Exit
exitP()
end
elseif key == 200 and mMselction == 1 then --ARROW KEY
mMenu()
elseif key == 200 and mMselction == 2 then --ARROW KEY
mMenu()
elseif key == 208 and mMselction == 1 then --ARROW KEY
mvMenu()
elseif key == 208 and mMselction == 2 then --ARROW KEY
mvMenu()
end
end
end
end
Problem solved
#16
Posted 04 January 2013 - 08:12 AM
#17
Posted 04 January 2013 - 08:27 AM
you used
if key ~= 29 or 200 or ...but it should be
if key ~= 29 or key ~= 200 ...
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











