How do I view another object's inventory

I have a point at which I give a character an item. When I look at them, I want to see that they are holding the item. So I'd like to print an expression that says, "This character is holding the following: " + object.inventory

Or something to that effect. How can I achieve that?


There's no difference, as far as Quest is concerned, between the contents of a container, the contents of an NPC, the contents of a room, or the contents of the player.

Inventory is just a fancy name for the contents of the player object.

There are a few ways to get the contents of any object:

GetDirectChildren (object) - gets an objectlist of objects directly inside the given object
GetAllChildObjects (object) - gets an objectlist of all objects inside the given object, including those which are inside containers and similar

But if you're looking to print the result, you probably want a function that will give you a nicely formatted string.
Here's one way to do it:

FormatObjectList("This character is carrying", object, Template("And"), ".")
That's exactly how the inventory command does it for the player. Note that this will give you an empty string if they're not holding anything. If you want to make it say something for that case, you would probably want something like:

list = FormatObjectList("This character is carrying", object, Template("And"), ".")
if (list = "") {
  list = "This character isn't carrying anything."
}
// then you can do something with `list`

Or even:

list = FormatObjectList(" carrying", object, Template("And"), ".")
if (list = "") {
  list = " not carrying anything."
}
msg (CapFirst(WriteVerb (object, "be")) + list)

(the function WriteVerb (object, "be") will give you "He is", "She is", "It is", or "They are" depending on the gender of the character. It will also give "you are" if used on the player, or "I am" if the gender is set to "I" - but those last two probably don't make a difference in your case)


Thank you, I think I'm starting to get it. However, I don't think I have the whole picture. I set up a function called: inventory list

inv_list = FormatObjectList("This character is carrying", object, Template("And"), ".")
if (inv_list = "") {
  inv_list = "This character isn't carrying anything."
}

So from my interpretation, this function will create the list, based on the object. Then I'd need to do a print expression to print the list, yes?

But I'm not sure about calling the function properly. My instinct is to call it in the character's "look at" description. But do I need to set any parameters during the call, or will Quest assume that the object in the function is the object being looked at? I tried it without any parameters, but I get the following error:

Error running script: Error compiling expression 'FormatObjectList("This character is carrying", object, Template("And"), ".")': Unknown object or variable 'object'
Error running script: Error compiling expression 'inv_list': Unknown object or variable 'inv_list'

Edit: one more question: is FormatObjectList() a built in function? Not finding it in the documentation so far, but I'm still searching.


Yeah, you asked about object.inventory so I assumed you had a variable called object.

If you're making it a function, you should give it a single parameter named object.
I would recommend making the function's type "string", and then adding a line at the end:

return (inv_list)

That way, if a character's 'look at' is a script, you could do:

msg (WhateverYourFunctionIsCalled (this))

and if it's a string you could use:

{=WhateverYourFunctionIsCalled (name_of_the_object)}

However, if you're using it in the "look at" script, there's an easier way.

Give the NPC a couple of attributes:
listchildren with value true (so his visible contents are shown when looked at)
transparent with value true (so his contents are visible even if he's not an open container)
listchildrenprefix is a string which could be something like "He is carrying"

Now what he's carrying will appear after his 'look' description without having to do anything else.
(these attributes are normally set on the "Container" tab, but the look command doesn't care if the NPC is actually a container or not)


Thank you. I'm going to try it each way, as this project is mostly to teach myself how to do things before I get to the actual game I want to create.


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

Support

Forums