Jump to content




Menu System


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

#1 makeme

  • Members
  • 60 posts

Posted 23 June 2013 - 02:25 PM

so for most of the day i have been trying to get a working menu for a spawner program i am working on but ive got kinda stuck and cant get it to work
(Sorry for the quality of my code i know its not pretty)
Spoiler
Thanks in advance

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 23 June 2013 - 02:46 PM

Whats the problem with it?

I had a quick overview and you can only call functions which you have defined:
function test()
  print("test")
end
test()
-- Doesnt work:
test()
function test()
  print("test")
end


#3 makeme

  • Members
  • 60 posts

Posted 23 June 2013 - 03:00 PM

wait sorry real tired atm just realised i was half way though coding a part of it :P

sorry yeah i hadn't finshed coding part of it its working now :/ here it is :P http://pastebin.com/7SdQ2BpC its still not finshed but its getting there .... slowly

#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 23 June 2013 - 04:17 PM

A little code improvement, so your codes is more readable simple to handle:
local function menu(startX,startY,bgcol,txtcol,markcol,options)
local currY = startY
local selected = 1
local function redraw()
term.setBackgroundColor(bgcol)
term.clear()
for i = 1, #options do
term.setCursorPos(startX, currY)
if i == selected then
term.setTextColor(markcol)
term.write("[")
term.setTextColor(txtcol)
term.write(options[i])
term.setTextColor(markcol)
term.write("]")
else
term.setTextColor(txtcol)
term.write(options[i])
end
end
end
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" then
if sKey == keys.up then
if selected == 1 then
selected = #options
else 
selected = selected - 1
end
elseif sKey == keys.down then
if selected == #options then
selected = 1
else
selected = selected + 1
end
elseif sKey == keys.enter then
return options[selected]
end
redraw()
sleep(0)
end
end

--Creating a new menu
local spawnMenu = menu(5,5,colors.brown,colors.gray,colors.lime,{"Mob1","Mob2","Mob3"})

--Interacting with the output
if spawnMenu == "Mob1" then
--Do something
elseif spawnMenu == "Mob2" then
--Do something
elseif spawnMenu == "Mob3" then
--Do something
end

I hope there aren't any typos ^_^/>

-Freack100 with his HTC-

#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 23 June 2013 - 04:38 PM

View PostFreack100, on 23 June 2013 - 04:17 PM, said:

A little code improvement, so your codes is more readable simple to handle:
local function menu(startX,startY,bgcol,txtcol,markcol,options)
local currY = startY
local selected = 1
local function redraw()
term.setBackgroundColor(bgcol)
term.clear()
for i = 1, #options do
term.setCursorPos(startX, currY)
if i == selected then
term.setTextColor(markcol)
term.write("[")
term.setTextColor(txtcol)
term.write(options[i])
term.setTextColor(markcol)
term.write("]")
else
term.setTextColor(txtcol)
term.write(options[i])
end
end
end
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" then
if sKey == keys.up then
if selected == 1 then
selected = #options
else
selected = selected - 1
end
elseif sKey == keys.down then
if selected == #options then
selected = 1
else
selected = selected + 1
end
elseif sKey == keys.enter then
return options[selected]
end
redraw()
sleep(0)
end
end

--Creating a new menu
local spawnMenu = menu(5,5,colors.brown,colors.gray,colors.lime,{"Mob1","Mob2","Mob3"})

--Interacting with the output
if spawnMenu == "Mob1" then
--Do something
elseif spawnMenu == "Mob2" then
--Do something
elseif spawnMenu == "Mob3" then
--Do something
end

I hope there aren't any typos ^_^

-Freack100 with his HTC-
You missed an end :P No worries, I know the phone trouble xD
Spoiler






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users