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

Menu selection not working, I've tested everything.

Started by SadProgrammer, 03 September 2012 - 05:48 PM
SadProgrammer #1
Posted 03 September 2012 - 07:48 PM
Hello There!

And thanks for checking this out!



Ok, ok… I'll cut the crap, dive straight into it! :D/>/>

So there is this larger project I've been working on, but I cannot seem to get it working!
Now I've used computercraft for a very long time, and I once wrote an UI with build in APIs and alot of handy utilities on a server.
But that server shut down, I don't know what the owner was thinking.

So I lost ~2000 lines of almost releasable code! (Well at least for the first version :P/>/> )

Now I've decided to rewrite the whole thing, but I wanted to change the layout for the choosing of options in menus.
I decided to do this kind of thingy that you can scroll with the arrow keys, up and down, and in bigger menus, to the sides.

But I got stuck very early when facing a problem in my loop. Here is the code, I'll explain the problem!

Spoiler

--
-- Part of a project, made by Cedric.
--
--
-- In Minecraft, using computercraft.
--
--

os.loadAPI("/ui/uiBase") -- Loads an api I made for frames for the menus and such
local choice = 1 -- what choice you made in the menu
function menu() -- a function drawing the menu
  uiBase.frame() -- a function from uiBase
  term.setCursorPos(20, 7)  -- !
  write "Login"			 --Just
  term.setCursorPos(20, 8)  --Some
  write "Register"		  --Options
  term.setCursorPos(20, 9)  --Over
  write "About"			 --Here
  term.setCursorPos(20, 10) -- !
  write "Shutdown"
  return
end
while true do -- A loop for updating the screen and for inputting new inputs
  shell.run("clear") -- clears the screen

  menu() -- my function

  if choice == 1 then -- draws a pointer for the selected choice
	term.setCursorPos(19, 7) --
	write ">"		   -- same here
  elseif choice == 2 then
	term.setCursorPos(19, 8)
	write ">"			   -- I think you get my point here
  elseif choice == 3 then
	term.setCursorPos(19, 9)
	write ">"
  elseif choice == 4 then
	term.setCursorPos(19, 10)
	 write ">"
  end


  event, param1 = os.pullEvent("key") -- Waiting for a key to be pressed
	if event == "key" and param1 == "200" then -- If the key pressed was "up"
	  choice = choice - 1					  -- Scrolls up the list
	elseif event == "key" and param1 == "208" then --If the key pressed was "down"
	  choice = choice + 1					 -- scrolls down the list
	elseif event == "key" and param1 == "28" then -- if the key pressed was "ENTER"
	  if choice == 1 then					   -- Going into the...
		shell.run("loginMenu")				  -- login menu
	  elseif choice == 2 then				  
		shell.run("registerMenu")			   --registering menu
	  elseif choice == 3 then
		shell.run("aboutText")				  --"about" screen
	  elseif choice == 4 then
		shell.run("shutdownCustom")			 -- shuts down the computer
  end
  if choice == 5 then -- you cant go outside the menu
	choice = 4
  elseif choice == 0 then
	choice = 1
  end

	
end
end -- It just wanted that. wth?

Now the problem is that when the menu pops up, the selection marker shows up, but I can't get it to react! So the os.pullEvent() most have frozen there somehow, nothing reacts, you have to terminate the program to get out. I've scratched my head for a time now, but then I found this forum!

Help me ComputerCraft forum, you're my only hope… *Star wars reference*
Kolpa #2
Posted 03 September 2012 - 08:06 PM
and that's why you don't code on servers kids servers are just to show off the shit that you got
SadProgrammer #3
Posted 03 September 2012 - 09:55 PM
and that's why you don't code on servers kids servers are just to show off the shit that you got
This was a project I made with another guy.

I think the server owner died btw :D/>/>
Sammich Lord #4
Posted 03 September 2012 - 11:14 PM
I think I may of found the problem.
You use os.pullEvent("key") but you also checks if the event is a key.
It should be:

p1, p2 = os.pullEvent("key")
while true do
  if p1 == keycodehere then
	--do shit
  elseif p1 = keycodehere
	--etc
  end
end
Basically a noob mistake. It should work however, I have not tested.
SadProgrammer #5
Posted 04 September 2012 - 06:47 AM
Exellent! I'll use this, hope it works!
SadProgrammer #6
Posted 13 September 2012 - 08:08 PM
I think I may of found the problem.

Noo, that wasn't the problem :)/>/>
I couldn't reply, because I didn't have access to my computer in days. But now that I've tested, it turned out kinda sad.


What now? :P/>/>
Cranium #7
Posted 13 September 2012 - 08:49 PM
I have a function that sounds similar to what you are talking about. With some modifications, I think it can do what you need.

--requires the use of a table to add the menu options
local options={
"option1",
"option2",
"option3",
"option4",
"option5",
"option6"
}
local function menuOpt(menu)
local btn1 = {1,1,menu[1]}
local btn2 = {2,1,menu[2]}
local btn3 = {1,2,menu[3]}
local btn4 = {2,2,menu[4]}
local btn5 = {1,3,menu[5]}
local btn6 = {2,3,menu[6]}
local a1 = string.len("   ["..btn1[3].."]   "..btn2[3].." ")/2
local a2 = string.len("    "..btn1[3].."   ["..btn2[3].."]")/2
local a3 = string.len("    "..btn1[3].."    "..btn2[3].." ")/2
local b1 = string.len("   ["..btn3[3].."]   "..btn4[3].." ")/2
local b2 = string.len("    "..btn3[3].."   ["..btn4[3].."]")/2
local b3 = string.len("    "..btn3[3].."    "..btn4[3].." ")/2
local c1 = string.len("   ["..btn5[3].."]   "..btn6[3].." ")/2
local c2 = string.len("    "..btn5[3].."   ["..btn6[3].."]")/2
local c3 = string.len("    "..btn5[3].."    "..btn6[3].." ")/2
local x,y = term.getSize()
local curx=btn1[1]
local cury=btn1[2]
while true do
--test for looping
if curx<btn1[1] and cury==btn1[2] then curx=btn2[1] cury=btn2[2]
elseif curx>btn2[1] and cury==btn2[2] then curx=btn1[1] cury=btn1[2]
elseif curx<btn3[1] and cury==btn3[2] then curx=btn4[1] cury=btn4[2]
elseif curx>btn4[1] and cury==btn4[2] then curx=btn3[1] cury=btn3[2]
elseif curx<btn5[1] and cury==btn5[2] then curx=btn6[1] cury=btn6[2]
elseif curx>btn6[1] and cury==btn6[2] then curx=btn5[1] cury=btn5[2]
elseif cury<btn1[2] and curx==btn1[1] then cury=btn5[2] curx=btn5[1]
elseif cury>btn5[2] and curx==btn5[1] then cury=btn1[2] curx=btn1[1]
elseif cury<btn2[2] and curx==btn2[1] then cury=btn6[2] curx=btn6[1]
elseif cury>btn6[2] and curx==btn6[1] then cury=btn2[2] curx=btn2[1]
end
-- line 1 of options
if curx==btn1[1] and cury==btn1[2] then
local x = (x/2)-a1
term.setCursorPos(x,8)
print("   ["..btn1[3].."]   "..btn2[3].." ")
elseif curx==btn2[1] and cury==btn2[2] then
local x = (x/2)-a2
term.setCursorPos(x,8)
print("    "..btn1[3].."   ["..btn2[3].."]")
else
local x = (x/2)-a3
term.setCursorPos(x,8)
print("    "..btn1[3].."    "..btn2[3].." ")
end
-- line 2 of options
if curx==btn3[1] and cury==btn3[2] then
local x = (x/2)-b1
term.setCursorPos(x,10)
print("   ["..btn3[3].."]   "..btn4[3].." ")
elseif curx==btn4[1] and cury==btn4[2] then
local x = (x/2)-b2
term.setCursorPos(x,10)
print("    "..btn3[3].."   ["..btn4[3].."]")
else
local x = (x/2)-b3
term.setCursorPos(x,10)
print("    "..btn3[3].."    "..btn4[3].." ")
end
-- line 3 of options
if curx==btn5[1] and cury==btn5[2] then
local x = (x/2)-c1
term.setCursorPos(x,12)
print("   ["..btn5[3].."]   "..btn6[3].." ")
elseif curx==btn6[1] and cury==btn6[2] then
local x = (x/2)-c2
term.setCursorPos(x,12)
print("    "..btn5[3].."   ["..btn6[3].."]")
else
local x = (x/2)-c3
term.setCursorPos(x,12)
print("    "..btn5[3].."    "..btn6[3].." ")
end
e, k = os.pullEvent("key")
if k==203 then curx = curx - 1 --left
elseif k==205 then curx = curx + 1 --right
elseif k==200 then cury = cury - 1 --up
elseif k==208 then cury = cury + 1 --down
elseif k==28 then break --enter
end
end
if curx == 1 and cury == 1 then return 1 end
if curx == 2 and cury == 1 then return 2 end
if curx == 1 and cury == 2 then return 3 end
if curx == 2 and cury == 2 then return 4 end
if curx == 1 and cury == 3 then return 5 end
if curx == 2 and cury == 3 then return 6 end
end
Right now, it centers everything, and scrolls up and down, left and right, and even loops back to the other side if you go too far. Hope this helps!