Jump to content




[Programming][Help] Making a menu


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

#1 EmTeaKay

  • Members
  • 115 posts

Posted 14 September 2012 - 03:25 AM

So, I need help making a menu. Nothing complex, just a three option menu. I know there's a tutorial here: http://www.computerc...gh-menu-making/ but I can't get that to work.
All I need is the code and an explanation and I'll be able to figure it out.

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 14 September 2012 - 03:50 AM

Menus can be be simple or complex depending on how you want the menu to behave. Here is a simple one though.

local menuOptions = {"Option1", "Option2", "Option3"} --Put your menu items into this table
local termX, termY = term.getSize() --Get the terminal size
local selected = 1 --The selected menu item
function centerText(text, termY) --Write text to the center of the x axis
term.setCursorPos(termX/2-#text/2,termY)
term.write(text)
return true
end
function start()
while true do
  term.clear() --Rewrite the menu each time the user does something
  for i,v in ipairs(menuOptions) do --For each of the items in menuOptions do this
   if i == selected then --If the index of the menuItem is selected, draw it with "[ ]" around it
	centerText("[ "..v.." ]", i)
   else
	centerText(v,i)
   end
  end
  x,y = os.pullEvent() --Get user input
  if y == keys.down and selected < #menuOptions then --If selected is smaller than the number in menuOptions
   selected = selected + 1
  elseif y == keys.up and selected > 1 then
   selected = selected - 1
  elseif y == keys.enter then --Returns the selected item
   return selected
  end
end
end
x = start()
print()
print("You selected option ".. x)

Hope this helped :)/>

#3 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 14 September 2012 - 04:19 AM

Here's a nice little script I wrote for you:
http://pastebin.com/wVkzaqF7

I'll post the code here too, but I strongly suggest that you download it onto a computer in your world to read. Or better yet, read it in NotePad++.
Spoiler

Hope I helped! :)/>

#4 EmTeaKay

  • Members
  • 115 posts

Posted 14 September 2012 - 04:24 AM

Current code:
local menuOptions = {"1", "2", "3"}
local termX, termY = term.getSize()
local selected = 1
function centerText(text, termY)
term.setCursorPos(termX/2-#text/2,termY)
term.write(text)
return true
end
function start()
while true do
 term.clear()
 for i,v in ipairs(menuOptions) do
  if i == selected then
	   centerText("[ "..v.."] ", i)
 else
    centerText(v,i)
 end
   end
   x,y = os.pullEvent()
   if y == keys.down and selected < #menuOptions then
    selected = selected + 1
   elseif y == keys.up and selected > 1 then
    selected = selected - 1
   elseif y == keys.enter then
    return selected
   end
end
end
x = start()
print()
print("You have selected option: "..x)  
Also, it shows up like
[1]
[2]
[3]

#5 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 14 September 2012 - 01:25 PM

Hmm. I'm on my phone right now so I can't test it but the code looks right to me. I don't know why it would be showing up wth the brackets around each number. It was working fine earlier. Ill try to test this out when I get home.

#6 sjele

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

Posted 14 September 2012 - 03:14 PM

   if y == keys.down and selected < #menuOptions then 
it errors with atemting to call nill
That line error, i belive you can fix it by changing keys.down to key code for down arrow.

Scroll down here to find the number for it:
http://computercraft...=Raw_key_events

#7 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 14 September 2012 - 07:02 PM

View Postsjele, on 14 September 2012 - 03:14 PM, said:

   if y == keys.down and selected < #menuOptions then 
it errors with atemting to call nill
That line error, i belive you can fix it by changing keys.down to key code for down arrow.

Scroll down here to find the number for it:
http://computercraft...=Raw_key_events

That should only be the case if you are not using the latest version of CC (perhaps tekkit?) or if for some reason you deleted the keys api.If you aren't using the latest version of CC though, then yes that would fix the problem.

#8 EmTeaKay

  • Members
  • 115 posts

Posted 14 September 2012 - 07:42 PM

Great, I got it working, but how do I make it so it goes sideways instead of up and down?

#9 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 14 September 2012 - 08:19 PM

Did you take a look at my tutorial? It's a little better documented, but I can show you how to do what you're looking for. Instead of using the up and down arrow key codes, we'll use the left and right.

203 = left
205 = right

In the print options method you can change how to options are drawn from being vertically stacked, to horizontal.

#10 EmTeaKay

  • Members
  • 115 posts

Posted 15 September 2012 - 04:50 AM

Yes, I do agree it is a lot better explained, but searching through that much code without knowing what you're looking for is daunting. So, do you think I could give you my current code and you change it? Because I am that that great at Lua tables and all that jazz.
http://pastebin.com/3zEBxREP
Also, thank you in advance.

#11 EmTeaKay

  • Members
  • 115 posts

Posted 16 September 2012 - 02:26 AM

Grim Reaper, I used your code and I stripped away all the '--''s. Now it says:
startup:28:attempt to call nil
And here is my current code, in the form of Pastebin:
http://pastebin.com/8RCmSMpE

#12 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

Posted 16 September 2012 - 03:35 AM

View PostEmTeaKay, on 16 September 2012 - 02:26 AM, said:

Grim Reaper, I used your code and I stripped away all the '--''s. Now it says:
startup:28:attempt to call nil
And here is my current code, in the form of Pastebin:
http://pastebin.com/8RCmSMpE

I didn't look through all of it as it would take forever to compare both pieces of code, but you probably deleted a important line of code, or didn't completely get rid of a comment. Just try running the code as he posted it first, before getting rid of all of his comments.

#13 EmTeaKay

  • Members
  • 115 posts

Posted 16 September 2012 - 03:56 AM

It seems that the problem is that I can't have it as a startup.

#14 EmTeaKay

  • Members
  • 115 posts

Posted 16 September 2012 - 04:59 AM

http://pastebin.com/AsXYxYYi All right, I got a different menu code. But this one prints vertical when I want it to print horizontal. Could someone please help?
And, sorry for the double post.

#15 sapient.fool

  • New Members
  • 2 posts

Posted 17 September 2012 - 05:28 AM

Here, I fixed up your code for you.
I personally don't care for doing menus this way, but hey, whatever gets the job done.

Spoiler






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users