Moving random object from one room to inventory and then removing it from a list?

I have a room called LOOTROOM where I keep all lootable objects.
When the player types "loot soldier" I want one lootable object to be moved to the inventory.
I can't get it to work, probably 'cause I suck terribly at lists.

Here's what I've done so far:

In the start script I have: game.lootlist = NewObjectList()

When the player use the loot command I have a switch witch has a 25% chance to lead to actual loot. I scripted it like this:

foreach (obj, GetDirectChildren(LOOTROOM)) {
  list add (game.lootlist, obj)
}
if (ListCount(game.lootlist) > 0) {
  loot = PickOneString (game.lootlist)
  AddToInventory (loot)
  msg ("You find "+ loot+ ".")
  list remove (game.lootlist, loot)
  object.looted = true
}
else {
  msg ("You find nothing.")
  object.looted = true
}

This is not working. Again: I suck at lists.... badly!
The error message I get is this:

  • loot corpse
    Error running script: Error compiling expression 'object': RootExpressionElement: Cannot convert type 'Object' to expression result of 'Element'
    You find .

If I do it again I get this (as it should be)

g
Been there, looted that

Can any of you smart people give me a hand with this... it's driving me crazy!
dsfsdfsdffdfsdfdsfsdklllllllllllllllllllllllllllllllllllllllllllllllllllffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffs dssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll


I solved it.

PickOneString should've been PickOneObject.

It's ok to point and laugh if you want to!


Ok. I didn't solve it after all.
Everything seems to be working just fine except the fact that I can loot the same object twice.

I want the object (located in LOOTROOM) to be unlootable after the player picks it up... for some reason the same object can be looted over and over now.

So... I guess I still need help :)
Anyone?


You are making it much harder than it is. Use GetDirectChildren to get a list of objects. If there is at least one item there, pick one, move it to the inventory, and tell the player about it.

l = GetDirectChildren(LOOTROOM)
if (ListCount(l) > 0) {
  loot = PickOneObject ()
  AddToInventory (loot)
  msg ("You find " + GetDisplayName(loot) + ".")
}
else {
  msg ("You find nothing.")
}
object.looted = true

That second line could be changed to give a random chance (30% here) of finding something.

if (ListCount > 0 and RandomChance(30)) {

I would probably have made it:

loot = PickOneObject (GetDirectChildren (LOOTROOM))
if (loot = null) {
  msg ("You find nothing.")
}
else {
  msg ("You find " + GetDisplayName(loot) + ".")
  AddToInventory (loot)
}
object.looted = true

but that's just a matter of personal preference.


This returned an error loot = PickOneObject ()but I added l to it and it's working like a charm!

Thank you so much!


I want the object (located in LOOTROOM) to be unlootable after the player picks it up... for some reason the same object can be looted over and over now.

It would seem that the looted object is not being removed from LOOTROOM, but only from game.lootlist. Can you verify this in-game with the debugger tool?


It would seem that the looted object is not being removed from LOOTROOM, but only from game.lootlist. Can you verify this in-game with the debugger tool?

I think the reverse. Adding to the inventory removes it from the room, but it is still on the list so still available to be picked again.


I think the reverse. Adding to the inventory removes it from the room, but it is still on the list so still available to be picked again.

Yep. With the original code, if LOOTROOM contained an Apple, Bell, Candle, and Duck, then the list is initially blank.

Call the script the first time:

  • lootlist is blank
  • LOOTROOM contains Apple, Bell, Candle, Duck
  • Add those to the list; lootlist is now [Apple, Bell, Candle, Duck]
  • Pick one (Apple) and remove it from the list
  • lootlist is now [Bell, Candle, Duck]

Then loot a second enemy.

  • lootlist is [Bell, Candle, Duck]
  • LOOTROOM contains Bell, Candle, Duck
  • Add those to the list; lootlist is now [Bell, Candle, Duck, Bell, Candle, Duck]
  • Pick one (Bell) and remove it from the list
  • lootlist is now [Candle, Duck, Bell, Candle, Duck]

Then loot a third enemy.

  • lootlist is [Candle, Duck, Bell, Candle, Duck]
  • LOOTROOM contains Candle, Duck
  • Add those to the list; lootlist is now [Candle, Duck, Bell, Candle, Duck, Candle, Duck]
  • Pick one (Duck) and remove it from the list
  • lootlist is now [Candle, Bell, Candle, Duck, Candle, Duck]

Then loot a fourth enemy.

  • lootlist is [Candle, Bell, Candle, Duck, Candle, Duck]
  • LOOTROOM contains Candle
  • Add those to the list; lootlist is now [Candle, Bell, Candle, Duck, Candle, Duck, Candle]
  • Pick one (Duck again) and remove it from the list
  • lootlist is now [Candle, Bell, Candle, Candle, Duck, Candle]

Objects that are still in the loot room are more likely to be chosen, because every time the script runs they're added to the list again. But if it takes you 6 attempts to get the apple, then you'll get 6 apples before it admits that the list is empty.
(Well, not actually 6 apples. But the same apple moved into your inventory 6 times)


I would have hit this backwards...
If loot chance<25 then (you found something)
Random pick of object from the loot room
else
"You don't find anything useful"

You could also have a second list of useless stuff to find...
Like: pocket lent, a washer, a picture of his girlfriend,... A picture of his boyfriend...


@DarkLizerd
No lists needed at all. Like Pix said; I was doing it way more difficult than needed.
His code (below) worked like a charm. :)

l = GetDirectChildren(LOOTROOM)
if (ListCount(l) > 0) {
  loot = PickOneObject(l)
  AddToInventory (loot)
  msg ("You find " + GetDisplayName(loot) + ".")
}
else {
  msg ("You find nothing.")
}
object.looted = true

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums