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

Menu System

Started by makeme, 23 June 2013 - 01:25 PM
makeme #1
Posted 23 June 2013 - 03: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

--Default Mobs Zombie - White & ZombiePig - Yellow & Creeper - Black & Skell - Red & slime - Oranage --

-------Menu------------
function Menu()
local n=1
term.clearLine()
if n==1 then
ZombieS()
elseif n==2 then
ZombiePigS()
elseif n==3 then
a, b=os.pullEvent("key")
while a~="key" do a, b=os.pullEvent() end
if b==203 then n = n + 1 end
if b==205 then n= n -1 end
if b==28 then print("Exit") end
end
end
------Menu Options-------
function ZombieS()
print(">Zombie<") -- White
print("Zombie PigMan") -- Yellow
print("Creeper") -- Black
print("Skeleton") -- Red
print("Slime") -- orange
end
function ZombiePigS()
print("Zombie") -- White
print(">Zombie PigMan<") -- Yellow
print("Creeper") -- Black
print("Skeleton") -- Red
print("Slime") -- orange
end
function CreeperS()
print("Zombie") -- White
print("Zombie PigMan") -- Yellow
print(">Creeper<") -- Black
print("Skeleton") -- Red
print("Slime") -- orange
end
function SkeletonS()
print("Zombie") -- White
print("Zombie PigMan") -- Yellow
print("Creeper") -- Black
print(">Skeleton<") -- Red
print("Slime") -- orange
end
function SlimeS()
print("Zombie") -- White
print("Zombie PigMan") -- Yellow
print("Creeper") -- Black
print("Skeleton") -- Red
print(">Slime<") -- orange
end

Menu()
Thanks in advance
Engineer #2
Posted 23 June 2013 - 03: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
makeme #3
Posted 23 June 2013 - 04: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
H4X0RZ #4
Posted 23 June 2013 - 05: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-
Engineer #5
Posted 23 June 2013 - 05:38 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-
You missed an end :P/> No worries, I know the phone trouble xD
Spoiler

local function menu(startX,startY,bgcol,txtcol,markcol,options)
	local currY = startY
	local selected = 1
	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
			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
			sleep(0)
		end
	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