Game Development Community

Get ID of option in dropdown?

by Kevin Epps · in Torque Game Builder · 03/07/2007 (8:26 am) · 2 replies

How can you get the get the ID of any option added to the dropdown list?

Like if I wanted to get the id of Text that wasn't selected, how can I do that?

#1
03/07/2007 (11:57 am)
Figured it out.

function GuiPopUpMenuCtrl::getID(%this, %string)
{
   for(%count = 0; %count < %highestNumberofIDs; %count++) // In my case %highestNumberofIDs = 96
   {
      $strng = strlwr(%this.getTextById(%count));
      if(strcmp($strng,%string) == 0)
      {
         return %count;
         break;
      }
      else
      {
         
      }
   }
}
#2
03/09/2007 (1:24 am)
Alternatively (although not equivalently)
Quote:
GuiPopupMenuCtrl::findText( string text ) -> integer ID - Returns the position of the first entry containing the specified text.
Or, using another class method:
Quote:
GuiPopupMenuCtrl::size() -> integer size - Get the size of the menu: the number of entries in it.
you can simplify your function, avoiding a magic number, the use of a global variable, and a call to strcmp(), by doing something like:
function GuiPopUpMenuCtrl::getIDByText(%this, %string)
{
   %size   = %this.size() ; 
   %string = strlwr( %string ) ; 
   for( %count = 0; %count < %size; %count++ ) 
   {
      if ( strlwr( %this.getTextById( %count ) ) $= %string ) 
         return
            %count ; 
   }
   return
      -1 ; 
}
where we have added an explicit return value in the case of no hits.