Jump to content




Function giving weird output?


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

#1 Wouto1997

  • Members
  • 15 posts

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

#2 ChunLing

  • Members
  • 2,027 posts

Posted 24 November 2012 - 02:57 AM

Use a modulo function, either % or math.fmod(), instead of that getindex thing you made.

#3 Wouto1997

  • Members
  • 15 posts

Posted 24 November 2012 - 02:59 AM

Thanks i'll try it now

#4 Wouto1997

  • Members
  • 15 posts

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)"

#5 ChunLing

  • Members
  • 2,027 posts

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"?

#6 Wouto1997

  • Members
  • 15 posts

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.

#7 ChunLing

  • Members
  • 2,027 posts

Posted 24 November 2012 - 06:02 AM

Aha, the old local/global bug.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users