List Indexing
homeeman
05 May 2013, 07:12I'm having a little trouble understanding what Quest is trying to tell me, I think. I have a somewhat complicated function which uses a lot of object lists and the like, but for some reason the first time I try to access one of them I get the following error message:
Error running script: Error evaluating expression 'ObjectListItem(empty, GetRandomInt(1, ListCount(empty)) - 1)': ObjectListItem: index 0 is out of range for this list (0 items, last index is -1)
From that last bit, I gathered that the list was empty (despite its name, it shouldn't be). So just to double check, just after the list is created I decided to print its ListCount() return.
Just before I get this error, it prints the number 4.
So I'm not entirely certain what this error is trying to tell me at this point. Any ideas? Is there something I'm missing?
Additional Info: The list is created using the GetDirectChildren() function on a "room" which serves to contain other rooms like a folder.
I always get this error message. So either for some reason I'm always getting '0' from the GetRandomInt() function, or this error is trying to tell me something I don't at all grasp.
If I remove the "- 1" from the ObjectListItem() function, replaces the 0 in "0 items" with a 1 and the -1 in "last index is -1" with a 0.
Error running script: Error evaluating expression 'ObjectListItem(empty, GetRandomInt(1, ListCount(empty)) - 1)': ObjectListItem: index 0 is out of range for this list (0 items, last index is -1)
From that last bit, I gathered that the list was empty (despite its name, it shouldn't be). So just to double check, just after the list is created I decided to print its ListCount() return.
Just before I get this error, it prints the number 4.
So I'm not entirely certain what this error is trying to tell me at this point. Any ideas? Is there something I'm missing?
Additional Info: The list is created using the GetDirectChildren() function on a "room" which serves to contain other rooms like a folder.
I always get this error message. So either for some reason I'm always getting '0' from the GetRandomInt() function, or this error is trying to tell me something I don't at all grasp.
If I remove the "- 1" from the ObjectListItem() function, replaces the 0 in "0 items" with a 1 and the -1 in "last index is -1" with a 0.

jaynabonne
05 May 2013, 09:17You need to give more code, if possible. If I do this, it works:
The only time I get an error is if the list is, indeed, empty.
L = NewObjectList()
list add(L, game)
list add(L, player)
o = ObjectListItem(L, GetRandomInt(1, ListCount(L)) - 1)
msg(o)
The only time I get an error is if the list is, indeed, empty.
homeeman
05 May 2013, 18:16Unfortunately, there's not much to add for the code:
It doesn't get very far into the function before it stops completely (the sixth line above). The parameters are put together just before the function is called, in the game's start script (the game doesn't react well to this function at all when it's called outside the start script, unfortunately).
<function name="populateDungeon" parameters="enemyList, weaponsList, suppliesList, area, max"><![CDATA[
empty = GetDirectChildren(area)
enemies = enemyList
weapons = weaponsList
while (ListCount(enemies) > 0) {
room = ObjectListItem(empty, GetRandomInt(1, ListCount(empty)) - 1) //This line is where the function hangs
for (i, 1, GetRandomInt(2, max), 1) {
enemy = CloneObject (ObjectListItem(enemies, GetRandomInt(1, ListCount(enemies)) - 1))
MoveObject (enemy, room)
enemy.wequip = ObjectListItem(weapons, GetRandomInt(1, ListCount(weapons)) - 1)
supply = GetRandomInt(0, (2 * ListCount(suppliesList)) - 1)
count = 0
while (supply < ListCount(suppliesList) and count <> 3) {
CloneObjectAndMove (ObjectListItem(suppliesList, supply), enemy)
supply = GetRandomInt(0, (2 * ListCount(suppliesList)) - 1)
count = count + 1
}
list remove (enemies, enemy.cloneof)
room.bads = room.bads + 1
}
if (ListCount(enemies) = 1) {
enemy = CloneObject (ObjectListItem(enemies, 0))
MoveObject (enemy, room)
enemy.wequip = ObjectListItem(weapons, GetRandomInt(1, ListCount(weapons)) - 1)
supply = GetRandomInt(0, (2 * ListCount(suppliesList)) - 1)
count = 0
while (supply < ListCount(suppliesList) and count <> 3) {
CloneObjectAndMove (ObjectListItem(suppliesList, supply), enemy)
supply = GetRandomInt(0, (2 * ListCount(suppliesList)) - 1)
count = count + 1
}
list remove (enemies, enemy.cloneof)
room.bads = room.bads + 1
}
room.battle = true
room.prubble = GetRandomInt(0, 4)
room.erubble = GetRandomInt(0, max)
list remove (empty, room)
}
]]></function>
It doesn't get very far into the function before it stops completely (the sixth line above). The parameters are put together just before the function is called, in the game's start script (the game doesn't react well to this function at all when it's called outside the start script, unfortunately).
list add (game.enemyList, Raider)
list add (game.enemyList, Raider)
list add (game.enemyList, Raider)
list add (game.enemyList, Brute)
list add (game.enemyList, Brute)
list add (game.enemyList, Brute)
list add (game.enemyList, Sickly)
list add (game.enemyList, Sickly)
list add (game.enemyList, Sickly)
list add (game.weaponslist, combat knife)
list add (game.weaponslist, crowbar)
list add (game.weaponslist, baton)
list add (game.weaponslist, pistol)
list add (game.weaponslist, shotgun)
list add (game.weaponslist, machete)
list add (game.weaponslist, baseball bat)
list add (game.supplieslist, leather jacket)
list add (game.supplieslist, bulletproof vest)
list add (game.supplieslist, pistol ammo)
list add (game.supplieslist, shotgun shells)
populateDungeon (game.enemyList, game.weaponslist, game.supplieslist, Combat Rooms, 3)

jaynabonne
05 May 2013, 21:30What I see: you have a loop that loops on enemies and for each enemy picks and removes a room from "empty". What happens if you have more enemies than rooms? Eventually "empty" will be... empty.
homeeman
05 May 2013, 23:30I searched through this function forever and somehow I didn't notice that the while loop hinges on the number of enemies, not the number of remaining rooms. Thanks a ton.