Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: KodiakBehr on Sat 08/09/2012 16:52:16

Title: Multiple possible additions to a listbox.
Post by: KodiakBehr on Sat 08/09/2012 16:52:16
This should be straightforward but I must have been at this too long.  I've got a listbox that starts mostly unpopulated.

The game is open-ended enough that the player can add entries to that listbox is any order.  How do I ensure that the correct actions for selecting a listbox item are linked to the right item added.

This is probably a clumsy way of explaining.  Let me clarify.

A player gets a phone number, so if I state:

listPhoneNumbers.InsertItemAt(2, "867-5309");

It won't show up in the listbox until there is a 0 and 1 entry.  That's no good to me.

If I instead state:

listPhoneNumbers.AddItem("867-5309");

It could end up in any spot, and I have no idea how to ensure that listPhoneNumbers.SelectedIndex==2 goes to THAT entry.

What do you think I should do?  (Besides get some sleep.)
Title: Re: Multiple possible additions to a listbox.
Post by: Khris on Sat 08/09/2012 17:03:57
Keep the numbers in an array, then iterate through its elements to find out which one was selected.

Code (ags) Select
// header

enum Contact {
  ePNContact1,
  ePNContact2,
  ePNContact3,
  ...
};

// main script

String phone_number[15];

// in game_start

  phone_number[ePNContact1] = "867-5309";
  phone_number[ePNContact2] = "867-7834";
  ...

int GetContact() {
  int i;
  while (i < 15) {
    if (phone_number[i] == listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex]) return i;
    i++;
  }
  return -1;
}

// call event
  Contact c = GetContact();
  if (c == ePNContact1) {
    ...
  }
  if (c == ePNContact2) {
    ...
  }


Edit: thanks geork, code corrected
Title: Re: Multiple possible additions to a listbox.
Post by: KodiakBehr on Sat 08/09/2012 18:33:06
Okay.  I'm finding this conceptually difficult to follow.  So instead of using if "(listPhoneNumbers.SelectedIndex==0){" I should use "if (c == ePNContact1){" instead?

Also, upon using that code it gives me a parse error at phone_number (line 16)?


Title: Re: Multiple possible additions to a listbox.
Post by: geork on Sat 08/09/2012 21:04:17
About the error: It should be
Code (AGS) Select
phone_number[ePNContact1] = "867-5309"; not
Code (AGS) Select
phone_number[ePNContact1] == "867-5309"; (the single and double equals signs are a programming ****  :P)

Conceptually speaking: Khris is linking the item's name to an "index" he has created, so when the list box is checked, it will be the item's name, not index, which matters. He creates his own "index" here:
Code (AGS) Select
enum Contact {
  ePNContact1,
  ePNContact2,
  ePNContact3,
  ...
};

(which is the same as:)
Code (AGS) Select
enum Contact {
  ePNContact1 = 1, //Wierdly the first one is 1, not 0
  ePNContact2 = 2,
  ePNContact3 = 3,
  ...
};

Then he gives everyone of his phone_number[] variables a value:
Code (AGS) Select
phone_number[ePNContact1] = "867-5309";
(again, same as:)
Code (AGS) Select
phone_number[1] = "867-5309";]
Hence, when you want to check which phone number has been selected, you call GetContact() which iterates through all the phone_number variables (from phone_number[0] to phone_number[14]) and tries to find whether the string phone_number[number] = the string (i.e. name) of the currently selected list box item: If so it returns the current number
Code (AGS) Select
if (phone_number[i] == listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex]) return i; //return terminates the function too
So when Khris calls next:
Code (AGS) Select
Contact c = GetContact(); he is checking whether the number returned corresponds with any of the items in Contact (e.g. if 1 is returned, c = ePNContact1 as ePNContact1 = 1)

and so, instead of checking 
Code (AGS) Select
if (listPhoneNumbers.SelectedIndex==0]) you check
Code (AGS) Select
if (c == ePNContact1){

Sorry if this is a bit long winded, or if you knew most of that already. But I hope this clears up any conceptual things you found difficult to follow!
Title: Re: Multiple possible additions to a listbox.
Post by: Khris on Sat 08/09/2012 21:53:54
Thanks for explaining it :)

If should be noted, in case that isn't clear, that the enum members can be named anything you want.

Also, all that code is maybe almost too elaborate for the situation.
To address your original question directly: just test for the number, not the index, as in:
Code (ags) Select
  String n = listPhoneNumbers.Items[listPhoneNumbers.SelectedIndex];
  if (n == "867-5309") ...
Title: Re: Multiple possible additions to a listbox.
Post by: KodiakBehr on Sat 08/09/2012 22:00:29
Aw man, that's a much simpler solution.  Thank you both for taking the time to spell it out for me.