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

CheeseMyBaby
27 Sept 2018, 12:05I 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

CheeseMyBaby
27 Sept 2018, 12:21I solved it.
PickOneString
should've been PickOneObject
.
It's ok to point and laugh if you want to!

CheeseMyBaby
27 Sept 2018, 13:03Ok. 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?
The Pixie
27 Sept 2018, 13:29You 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)) {
mrangel
27 Sept 2018, 13:44I 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.

CheeseMyBaby
27 Sept 2018, 13:53This returned an error loot = PickOneObject ()
but I added l
to it and it's working like a charm!
Thank you so much!

Dcoder
27 Sept 2018, 13:53I 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?
The Pixie
27 Sept 2018, 13:57It 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.
mrangel
27 Sept 2018, 16:17I 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)

DarkLizerd
28 Sept 2018, 03:16I 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...

CheeseMyBaby
28 Sept 2018, 07:46@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