Using Lists
Lists are made up of any number of strings (for a stringlist) or objects (for an objectlist).
Lists can be created by the NewStringList or NewObjectList functions, or can be returned by functions such as the scope functions.
Example of generating an objectlist called myList and an empty stringlist called MyStringList:
myList = ScopeVisibleNotHeld() myStringList = NewStringList()
An object or string can be retrieved from a list by its index using ObjectListItem or StringListItem. For example, to retrieve the second item from the above list (remembering that they count from zero):
StringListItem(myStringList, 1)
A string list can be converted to a string using the Join function. Conversely, a string can be broken up into a string list using the Split function.
Iterating through list elements
You can read a list in several ways. Note that lists are indexed from zero, so the item at index 2 will actually be the 3rd element.
- foreach, the easiest method, will just iterate through the entire list.
foreach (item, ScopeVisible()) {
msg ("The item is "+item.name)
}
- for is a general loop method which will need more details about the length of your list so that it will not exceed the bounds (go past the end) of the list.
for (myItem, 0, ListCount(myList) - 1) {
msg ("Current item is " + ObjectListItem(myList,myItem).name)
}
Adding and removing items
To manipulate the list use add and remove commands.
- list remove removes a named item from the list. Note: space between the word list and add/remove
if (ListContains(myList, player) {
list remove(myList, player)
}
- list add adds strings or objects to a list.
list add(myStringList,"Text item 1") list add(myStringList,"Text item 2")
Combining lists
To combine two lists into one, use the ListCombine function.
To return a copy of a list with one element removed, use the ListExclude function.