Dynamic List Box Contents?

Started by (deleted), Mon 11/08/2008 11:07:53

Previous topic - Next topic

(deleted)

(After studying previous forum posts regarding this dilemma, it seems to become apparent that this question merits a new topic)

What I'm attempting to accomplish is a function which searches through the contents of a list box to see if a string of text exists there, or not. The ideal function would accommodate the list box's total item number changing.

For example, if the player has Goo in his inventory, the function (located in repeatedly_execute) would update the list box with an entry called "Goo Listbox Text." I've used the term contents in the code below:

Code: ags

function repeatedly_execute() 
{

   if (player.InventoryQuantity[Goo.ID] != 0){         // PLAYER'S GOT THE GOO.
       if (listbox.contents != ("Goo Listbox Text"))  // NO GOO ENTRY IN LIST BOX?
       listbox.AddItem("Goo Listbox Text");}         // ADD LISTBOX ENTRY
   }

}


...so right now, uhh... I'm stumped.

I've tried a few approaches which result in the "INVALID INDEX NUMBER" error, and others which depend on the  listbox.ItemCount remaining the same (so I can check each slot, numerically) - but as I mentioned earlier, the total number of items in the list box changes, so checking via "listbox.items#" won't work, since the index number needs to accommodate a variable total.

Is there, perhaps, a way to convert a list box's contents to a series of strings, which can be checked? ...this is all so daunting.

Here's another example, which I've tried. It results in the game hanging - eternal loop style - and I get the error: "Script appears to be hung in GlobalScript etc"

Code: ags


int TotalNumber;

while (TotalNumber == Listbox.ItemCount -1) {
 if ((Listbox.Items[TotalNumber] != "Goo Listbox Text")  &&  (player.InventoryQuantity[Goo.ID] !=0 ))
   {Menu.AddItem("Goo Listbox Text");}
 else {}
}



I'm absolutely sure there's a head-slappingly simple way of reaching a solution, so come on, you dogs. You have full permission to make me look like an incapable dunce.

zabnat

From the top of my head, untested:
Code: ags

if(player.InventoryQuantity[Goo.ID] > 0){
  int i = 0;
  bool found = false;
  while(i < MyListbox.ItemCount){
    if(MyListbox.Items[i] == "Goo Listbox Text")
      found = true;
    i++;
  }
  if(!found)
    MyListbox.AddItem("Goo Listbox Text");
}

Of course you will be wanting to do that as a function that takes a inventory item as parameter, the check agains that items ID and get that items description or custom property for listbox text. I'm also sure there are better are more sophisticated ways to do this.

Khris

I'd rewrite that a bit:
Code: ags
bool Contains(this Listbox*, String s) {
  int i = 0;
  bool found = false;
  while(i < this.ItemCount){
    if(this.Items[i] == s) found = true;
    i++;
  }
  return found;
}


Now you can do
Code: ags
  if (!listbox.Contains("Goo Listbox Text")) listbox.AddItem("Goo Listbox Text");

Laukku

So you are trying to make a text inventory? Maybe this thread would be useful: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=32644.0
You are standing in an open field west of a white house, with a boarded front door.
>WIN GAME
Congratulations! You just won! You got 0 out of 500 points.

(deleted)

#4
Thanks for your help, fellas.

Laukku: No, not exactly. The idea is to create a single listbox which uses text to reflect details about the player's inventory.

The function in question serves two purposes:

1. Prevent duplicate entries in the list box.

2. Update constantly to reflect changes in player inventory.

Zabnat: Good idea with retrieving custom properties to supply the string text. (I'm using it, and you'll never see a red cent.)

KhrisMUC: Ahhh! It works! I may have omitted one or two elements, since (in the case of my project) there's only one List Box to be concerned with.

However, when calling this function in repeatedly_execute, it fills the list with the specified string, instead of adding only one. I'm using two functions (to disambiguate):

Code: ags


bool Contains(this ListBox*, String s) {
  int i = 0;
  bool found = false;
  while(i < this.ItemCount){
    if(this.Items[i] == s) found = true;
    i++;
  }
  return found;
}

bool Doesntcontain(this ListBox*, String s) {
  int i = 0;
  bool found = false;
  while(i < this.ItemCount){
    if(this.Items[i] != s) found = true;
    i++;
  }
  return found;
}

///--------Later, in Repeatedly_Execute...///--------------

if ((player.InventoryQuantity[Goo.ID] >0) && (Boxedlist.Doesntcontain("Goo Description"))) // -- List Doesn't Show Goo Description, yet.
{Boxedlist.AddItem("Goo Description");} // Add it.

if ((player.InventoryQuantity[Goo.ID] >0) &&  (Boxedlist.Contains("Goo Description"))) // -- List Does Show Goo Description
{} // -- Do nothing.


...so if it only adds to the list when it doesn't already contain the text, why is it filling up with duplicates?

Khris

You don't need a second function, just use it like I did, put a "!" before listbox.Contains to get "DoesntContain". (The "!" is "logical not".)

(deleted)

I, uhh.. Heh, heh. I knew that.

Works now, btw. Thanks for your charitable assistance.

SMF spam blocked by CleanTalk