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

[Lua][Help][Menu] Not showing change in selection

Started by rats3g, 30 September 2012 - 12:38 AM
rats3g #1
Posted 30 September 2012 - 02:38 AM
Hello everyone, and thanks for any help you can provide.

I want to start off by saying that I am using ComputerCraft 1.4.2 for Minecraft 1.3.2. Not sure if it makes any difference but I try to be thorough.

I am attempting to create a menu system to control my inventory/crafting system, reactors, and turtles in my upcoming 1.3.2 world. I am using the system shown here http://pastebin.com/wVkzaqF7 by Grim Reaper, to identify which option I am selecting. This system surrounds the selected option name with brackets and uses os.pullEvent("key") to change the selection with the up and down arrow keys.

My problem is only visual. While the code does work and changes the selection, thereby allowing me to move through menus and selections within them; it does not show this. When I test by printing only a single menu ie: boot menu drawBoot() it works, showing me moving through the selections, but when I use tMenus[menustate].draw() so that I can select other menus to display it displays the new menus when I select them (enter key) but it does not show the changing of selections within a menu.

Sorry if this isn't entirely clear but it's easier if you just try it yourself. You can see the problem by changing between drawBoot() and tMenus[menustate].draw()

Here's the pastebin to try my code yourself: http://pastebin.com/j85k7eQN

Code:
Spoiler

-- -- TechOS -- --

-- Local Variables --

local nScreenWidth, nScreenHeight = term.getSize()
local nSelection = 1
local menustate = "Boot"

-- End Local Variables --

-- Local Tables --

local tMenus = {
  [ "Boot" ] = {
	options = { "Inventory", "Turtles", "Reactor" },
	draw = drawBoot
  },
  [ "Inventory" ] = {
	options = { "Raw Materials", "Refined Materials" },
	draw = drawInventory
  },
  [ "Turtles" ] = {
	options = { "Mining" },
	draw = drawTurtles },
  [ "Reactor" ] = {
	options = { "Control" },
	draw = drawReactor
  }
}

-- End Local Tables --

-- -- Draw Functions -- --

-- Base Draw Functios --

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

function PrintCentered( nHeight, sString )
  term.setCursorPos( nScreenWidth/2 - #sString/2, nHeight )
  term.write( sString )
end

function PrintLeft( nHeight, sString )
  term.setCursorPos( 1, nHeight )
  term.write( sString )
end

function PrintRight( nHeight, sString )
  term.setCursorPos( ( nScreenWidth + 1 ) - #sString, nHeight )
  term.write( sString )
end

function PrintTitle( sName )
  PrintCentered( 1, string.rep( "-", ( nScreenWidth + 1 ) ) )
  PrintCentered( 2, string.rep( "-", ( nScreenWidth + 1 ) ) )
  PrintLeft( 3, "-" )
  PrintCentered( 3, sName )
  PrintRight( 3, "-" )
  PrintCentered( 4, string.rep( "-", ( nScreenWidth + 1 ) ) )
  PrintCentered( 5, string.rep( "-", ( nScreenWidth + 1 ) ) )
  for i = 6, ( nScreenHeight - 1 ) do
	PrintLeft( i, "-" )
	PrintRight( i, "-" )
  PrintCentered( nScreenHeight, string.rep( "--", ( nScreenWidth + 1 ) ) )
  end
end

-- End Base Draw Functions

-- Menu Draw Functions --

function drawBoot()
  PrintTitle( "Boot Menu" )
  for j = 1, 3 do
	if j == nSelection then
	 PrintCentered( 11 + ( j - 1 ), "[" .. tMenus["Boot"].options[j] .. "]" )
	else
	 PrintCentered( 11 + ( j - 1 ), " " .. tMenus["Boot"].options[j] .. " " )
	end
  end
end

function drawInventory()
  PrintTitle( "Inventory Menu" )
  for k = 1, 2 do
	if k == nSelection then
	 PrintCentered( 11 + ( k - 1 ), "[" .. tMenus["Inventory"].options[k] .. "]" )
	else
	 PrintCentered( 11 + ( k - 1 ), " " .. tMenus["Inventory"].options[k] .. " " )
	end
  end
end

-- End Menu Draw Functions --

-- -- End Draw Functions -- --

while true do
  ClearScreen()

  -- -- Problem lies here -- --
  -- drawBoot() -- shows changing of selections
  tMenus[menustate].draw() -- doesn't show changing of selections

  local id, nKey = os.pullEvent("key")
	  if nKey == 200 and nSelection > 1 then
	nSelection = nSelection - 1
	  elseif nKey == 208 and nSelection < #tMenus[menustate].options then
	nSelection = nSelection + 1
	  elseif nKey == 28 then
	menustate = tMenus[menustate].options[nSelection]

	  end
  end
Pharap #2
Posted 30 September 2012 - 03:02 AM
I think the issue might be that tMenus["Boot"].draw() is assigned as drawBoot() before drawBoot() has been implemented.
Try moving the local tables to after the draw functions.

I might be wrong, but I know lua is like C++ and throws a hissy fit if you don't state what a function does before trying to use it.
rats3g #3
Posted 30 September 2012 - 03:18 AM
Tried what you suggested but putting the tables below the base draw functions didn't change anything, while putting the tables below all the draw functions gave me an error: TechOS: 60: attempt to index ? (a nil value). So, unfortunately, that doesn't seem to be the solution. Thanks for the idea though.
Pharap #4
Posted 30 September 2012 - 03:41 AM
For the record, I'm assuming this was intended to be an actual operating system replacing CraftOS and not just a pseudo-OS run on top of the existing one, in which case are you using it as such? (eg replaced the CraftOS bios file with the TechOS)

Also, have you tried running the menustate as something other than boot?
You might be better off asking the creator. People tend to be better at knowing how their own programs work than other people's.
My next suggestion would involve rearranging a rather large chunk of the code to make the order more logical (since at the moment it is slightly paradoxical/recursive) which would be a large change and I'm still not sure if it would fix the issue.
rats3g #5
Posted 30 September 2012 - 03:56 AM
Oh, sorry. It's just a psuedo OS run on top. I just named the program TechOS because it sounds cool. Thanks for the tip. I'll try that.
Pharap #6
Posted 30 September 2012 - 04:10 AM
Oh, sorry. It's just a psuedo OS run on top. I just named the program TechOS because it sounds cool. Thanks for the tip. I'll try that.
Oh right, so it's based off of Reaper's system. I had got the impression that you were trying to use someone else's OS and were having troubles.

If this is something you are creating yourself, that puts a whole different spin on things (I was thinking why it wasn't working for you as opposed to looking for design issues).
I'm starting to think those overhaul suggestions might be in order, but I will see what your findings with the other options are first.

Out of interest, how much do you know about object orientation?