Removing list box items by name

Started by Jasmine, Fri 02/10/2009 05:05:59

Previous topic - Next topic

Jasmine

I'm working on a game where the player will have a bunch of tasks to complete before they can move on to the next section of the game, so I want to give them a list of what needs to be done.  I've got a list box called TaskList that works fine to display the tasks, but not so well to remove them when completed.

For example, if the player has to make a romantic dinner, they might have to cook the dinner, set the table, and pick flowers.
Code: ags
TaskList.AddItem("Cook Dinner");
TaskList.AddItem("Set table");
TaskList.AddItem("Pick flowers");

works perfectly.

However, because I don't know what order the player will choose to complete the tasks in, I can't use TaskList.RemoveTask to remove tasks that the player has completed.  I'd like to be able to type something like
Code: ags
TaskList.RemoveTask("Set table");
when the player has set the table.

If something other than a list box would work better, I'm fine with that.  If it would be easier to cross off or change the color of completed tasks, I'm fine with that as well (although I'd prefer to delete them).

I'm using AGS 3.1 and am really new to scripting, so if it's more complicated than what I've already got I'll need plenty of explanation ;D 

Thanks in advance!

Gilbert

You can search through the list for the string to delete it. You may make a function (untested) like:
Code: ags

function RemoveTask(String sss){
  int ind=TaskList.ItemCount;
  while (ind>0){    
    ind--;
    if (TaskList.Items[ind]==sss){ //Found text.
      TaskList.RemoveItem(ind);
      return 0;
    }
  }
  return 1; //Meaning the string was not found and nothing was removed.
}


Alternatively, you may define an array holding the indices of the texts when they're added to the list and find from the array which item to remove.

Jasmine

It works, but I have to put it at the beginning of all the room scripts.  Am I doing something wrong?  Is it possible to change it so that I can put it in the global script?

Thanks!

RickJ

Put Gilbet's function in the Global Script.  Then if you put the following line in the Script Header you should be able to call the function from any room script.

import function RemoveTask(String sss);

SMF spam blocked by CleanTalk