Index Numbers and lists

Talon
23 Aug 2017, 20:16

Hey there trying to figure out why this isn't working, my aim is to make a couple lists the player can add too, that was easy enough to do, the way I have it set up the player will be populating the lists so the indexes should line up (Say Red Blue Black with Dog Cat Mouse) and am trying to get the code right so I randomly select an index number and can use that number pick the proper entry from each list. Only testing it out on one list right now because it'll be easy to expand after
(list populates to player.customcolors)

m = Listcount(player.customcolors)
n = GetRandomInt (0,m)
msg {eval:StringListItem (player.customcolor, n)}

This throws up errors and i'm not sure why


The Pixie
23 Aug 2017, 20:45

The second line should be

n = GetRandomInt (0,m - 1)

hegemonkhan
24 Aug 2017, 02:52

List's start from/at '0', so, the last item's index number, is: ListCount (LIST) - 1

the index number range: '0' to 'ListCount (LIST) - 1'

for example:

color_list = split ("red;blue;yellow", ";")

3 items = ListCount (color_list)
red (1/3)
blue (2/3)
yellow (3/3)

index numbers:

0 = red (1/3)
1 = blue (2/3)
2 = yellow (3/3) // ListCount (color_list) = 3, and: 3-1 = 2 as the last (3rd) item's index number
3 = ERROR (4/3), there is no 4th item !!!!

color = StringListItem (color_list, 2)
// color = "yellow"

color = StringListItem (color_list, 0)
// color = "red"

color = StringListItem (color_list, 1)
// color = "blue"

color = StringListItem (color_list, 3)
// color = ERROR!!!! there is no 4th item/color !!!!!