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

Function giving weird output?

Started by Wouto1997, 24 November 2012 - 01:34 AM
Wouto1997 #1
Posted 24 November 2012 - 02:34 AM

function getindex(index, items)
i = index
  while i > items do i = i - items end
  while i < 1 do i = i + items end
  return i
end
function SelectionMenu(title, ...)
  DefaultMenu(title)
  index = 1
  items = {...}
  len = table.getn(items)
  CenterString("> "..items[getindex(index, len)].." <", 5)
  for i=1,12,1 do
    if items[getindex(index + i, len)] == nil then
     CenterString(getindex(index + i, len), 5 + i) -- debug
    else
     CenterString(items[getindex(index + i, len)], 5 + i)
    end
  end
end

I've got this code, and the idea is that it should not be possible to give out of range indexes of the array items[] by using getindex, but it seems to be giving weird outputs between 5-10, 11 or above seems to work better,

what it does now at 6-10 is
6 = 6
7 = 1
8 = 8
9 = 1
10 = 10

11 and 12 seem to be giving the correct options again.
I'm executing SelectionMenu with this command:

menu.SelectionMenu("Main menu", "Start game", "options", "settings", "tutorial", "Multiplayer")

Thanks for helping/reading
ChunLing #2
Posted 24 November 2012 - 02:57 AM
Use a modulo function, either % or math.fmod(), instead of that getindex thing you made.
Wouto1997 #3
Posted 24 November 2012 - 02:59 AM
Thanks i'll try it now
Wouto1997 #4
Posted 24 November 2012 - 03:03 AM

function SelectionMenu(title, ...)
  DefaultMenu(title)
  index = 1
  items = {...}
  len = table.getn(items)
  CenterString("> "..items[math.fmod(index, len)].." <", 5)
  for i=1,7,1 do
  if items[math.fmod(index, len)] == nil then
   CenterString(items[math.fmod(index+i, len)], 5 + i)
  else
   CenterString(items[math.fmod(index+i, len)], 5 + i)
  end
  term.write(" - "..len)
  end
end

using this, I already know the first option ( which is selected with > < ) has the actual table length (5), but all other options are showing "option name - option name length" and not "option name - table length(5)"
ChunLing #5
Posted 24 November 2012 - 04:15 AM
Place some debug prints, like

function SelectionMenu(title, ...)
  DefaultMenu(title)
  index = 1
  items = {...}
print(unpack(items))
  len = table.getn(items)
print(len)
  CenterString("> "..items[math.fmod(index, len)].." <", 5)
  for i=1,7,1 do
  if items[math.fmod(index, len)] == nil then
   CenterString(items[math.fmod(index+i, len)], 5 + i)
  else
   CenterString(items[math.fmod(index+i, len)], 5 + i)
  end
  term.write(" - "..len)
  end
end
and so on. I don't know what the function CenterString() does exactly, so can't speak to the output it's producing.

Also, why are you doing "for i=1,7,1" rather than "for i=1,len"?
Wouto1997 #6
Posted 24 November 2012 - 04:22 AM
I'm doing 7 to limit the amount of items, so I can use the menu function for different sized menu's with even 100 options if I ever wanted to, without getting the screen stuffed.. I also found out why it derped, I was modifying 'len' in CenterString.
ChunLing #7
Posted 24 November 2012 - 06:02 AM
Aha, the old local/global bug.