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

[Lua]Menu API. Includes submenus. Need help.

Started by Jazza, 12 July 2012 - 04:40 AM
Jazza #1
Posted 12 July 2012 - 06:40 AM
Alright, I've got, well most of the API done. But Uh, I'm not even sure if the code is legit.
If someone could tell me what doesn't work, and or fix it, I'd be happy to give you credit.

API:


-- Jazzas Menu API V 0.0_1

--functionList--
--menu.setMenu(subMenuName,subMenuParent,numOfOptions,option1,function1)
--menu.setPointer(point)
--menu.menu(name)

function setPointer(point)
pointer = point
end

function printCentered(str, ypos)
term.setCursorPos(w/2 - #str/2, ypos)
print(str)
end

function clear()
term.clear()
term.setCursorPos(1,1)
end

function Menu(name)
while true do
w,y = term.getSize()
menuSelected = 1
clear()
term.setCursorPos(15,9)
write(pointer)

startline = 9 - menuSelected + 8 -#tArgs/2-1
printCentered(menuOptions[1],startline)
for c = 1,#tArgs-1, 2 do
printCentered(MenuID[name .. c],startline])
startline = startline+1
end

a,keys = os.pullEvent("key")

if  keys == 200 and menuSelected > 1 then
menuSelected = menuSelected - 1
elseif keys == 208 and menuSelected < 8 then
menuSelected = menuSelected + 1
elseif keys == 28 and menuSelected == 1 then
MenuID[name .. 4]()
elseif keys == 208 and menuSelected == 2 then
MenuID[name .. 6]()
elseif keys == 208 and menuSelected == 3 then
MenuID[name .. 8]()
elseif keys == 208 and menuSelected == 4 then
MenuID[name .. 10]()
elseif keys == 208 and menuSelected == 5 then
MenuID[name .. 12]()
elseif keys == 208 and menuSelected == 6 then
MenuID[name .. 14]()
elseif keys == 208 and menuSelected == 7 then
MenuID[name .. 16]()
elseif keys == 208 and menuSelected == 8 then
MenuID[name .. 2]()
end
end
end

function setMenu(...)
tArgs = {...}
name = tArgs[1]
MenuID[name .. 1] = {tArgs[2]} --parent
MenuID[name .. 2] = {tArgs[3]} --num
MenuID[name .. 3] = {tArgs[4]}
MenuID[name .. 4] = {tArgs[5]}
MenuID[name .. 5] = {tArgs[6]}
MenuID[name .. 6] = {tArgs[7]}
MenuID[name .. 7] = {tArgs[8]}
MenuID[name .. 8] = {tArgs[9]}
MenuID[name .. 9] = {tArgs[10]}
MenuID[name .. 10] = {tArgs[11]}
MenuID[name .. 11] = {tArgs[12]}
MenuID[name .. 12] = {tArgs[13]}
MenuID[name .. 13] = {tArgs[14]}
MenuID[name .. 14] = {tArgs[15]}
MenuID[name .. 15] = {tArgs[16]}
MenuID[name .. 16] = {tArgs[17]}
table.insert(MenuID,"Back")
end

What's in the program:



menu.setMenu(main,"",6,"Hello","hello","Startup","startup","Goodbye","goodbye")
menu.setPointer("=>")
menu.menu(main)

function hello()
end

function startup()
end

function goodbye()
end

Thanks, Jazza
Teraminer #2
Posted 12 July 2012 - 11:20 AM
You get an error? If yes please list it :)/>/>
Jazza #3
Posted 12 July 2012 - 11:30 AM
1:Attempt to index a nil value o.O
Teraminer #4
Posted 12 July 2012 - 11:32 AM
You are trying to use a nill (0, null) vaule. But your code is a bit advanced for me (cause I don't get how args work) so I can't help more that this :)/>/>
MysticT #5
Posted 12 July 2012 - 03:17 PM
You haven't defined MenuID so it's nil, and then try to index it -> attempt to index a nil value
1lann #6
Posted 12 July 2012 - 04:49 PM
Alright, I've got, well most of the API done. But Uh, I'm not even sure if the code is legit.
If someone could tell me what doesn't work, and or fix it, I'd be happy to give you credit.

API:

SNIP

Thanks, Jazza

Not sure if this is a problem for you, but lua hates mixing numbers with strings

MenuID[name .. 1] = {tArgs[2]}
So if it does throw an error, use

MenuID[name .. "1"] = {tArgs[2]}
MysticT #7
Posted 12 July 2012 - 04:59 PM
Not sure if this is a problem for you, but lua hates mixing numbers with strings
Not at all, from the lua manual:
Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format. For complete control over how numbers are converted to strings, use the format function from the string library (see string.format).
Jazza #8
Posted 13 July 2012 - 03:22 AM
Hmm, I could just save a table of for each menu inside the main table, and then have the options inside that? That would make my life simpler, wouldn't it. I just don't exactly know how to call it?

Hello = {"1","2"}
Jazza[1] = "Goodbye"
Main = { Jazza, Hello }
Jazza = { Main[1] }
print("Jazza[1]")

Would that be right?
Graypup #9
Posted 13 July 2012 - 03:49 AM
I haz some time to read some code. You do

Main[1] = {}
to make a table within a table.
Graypup #10
Posted 13 July 2012 - 03:54 AM
You are trying to use a nill (0, null) vaule. But your code is a bit advanced for me (cause I don't get how args work) so I can't help more that this B)/>/>
Eat some info about args, then :)/>/>

tArgs = ...
tArgs is the array (/table, whatevs) that holds the arguments, the 1st arg is tArgs[1], the 2nd is tArgs[2]. … represents the arguments to a program. You can use … in an API while defining a function, too, to have a variable number of arguments, you use it like this:

function foo(...)
tArgs = ...
end
1lann #11
Posted 13 July 2012 - 03:33 PM
Not sure if this is a problem for you, but lua hates mixing numbers with strings
Not at all, from the lua manual:
Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format. For complete control over how numbers are converted to strings, use the format function from the string library (see string.format).
Huh, seems to complain for me so I have to use tostring() :-/