Listing objects inside a container

psymann
04 Apr 2013, 16:37
Hi all,

Tell me if you're getting bored of my daily questions! I'm stuck again :oops:

This time I have a box (container), that I might have put some items in.

When I open it, or examine it, if there's anything inside, I want to be able to run an IF, that checks whether there are any objects in the bin, and if there are, then print a message that says:

".... You can see the following inside: " + [object1] + ", " + [object2] + ", " + [object 3] + "." "


Problem is, I don't know how many objects there will be, nor what they will be. I am guessing I need a For...Each bit of code to cycle round and list all objects currently inside the box.

And that's where once again my coding skills fail me.

I know there's usually an auto thing that lists all the items for me with hyperlinks but I have it turned off the rest of the time and don't really want to turn it on temporarily whenever I look in a container.

psymann

jaynabonne
04 Apr 2013, 20:27
You can do something like this (leveraging what's in Quest):

s = FormatObjectList("You can see the following inside: ", container, "and", ".", false)
if (s <> "") {
msg(s)
}


Or if you want to iterate them yourself:

children = GetDirectChildren(container)

if (ListCount(children) <> 0) {
foreach (item, children) {
// Handle the next child object.
}
}


Remember that these will be objects, so you'll need to use item.alias (or better: GetDisplayAlias(item)).

Hope that helps!

HegemonKhan
04 Apr 2013, 22:05
each of the player objects (such as "player") inventories, is an objectlist: http://quest5.net/wiki/ScopeInventory

so, you can use all these list functions:

http://quest5.net/wiki/Category:All_Fun ... t_Commands
ScopeInventory ()
(see "L" category and its "List" links)

http://quest5.net/wiki/ListCount
ListCount (ScopeInventory ())

http://quest5.net/wiki/ListContains
http://quest5.net/wiki/ListCombine
http://quest5.net/wiki/List_remove
http://quest5.net/wiki/List_add
http://quest5.net/wiki/ListExclude

http://quest5.net/wiki/ObjectListItem
http://quest5.net/wiki/Objectlist

and then there's this, which talks about much of what you are interested in doing:

viewtopic.php?f=18&t=3515

ask sora for more help, or I can try to help you too, if I can do so.

------------

P.S.

err... you're talking about making a (container) object's objectlist (and not the player objects' inventory objectlists), but the same applies, regardless.

this is a bit complicated, I myself may not even be able to figure it out in how to code it corectty for you. so maybe sora or jaynnebonne or whoever can help you, as they know code well.

jaynabonne
04 Apr 2013, 23:34
FYI, the Scope* functions return object lists, but those lists are all generated, based on the current state of object parents. For instance, to add an object to the player's inventory, you just set the object's parent attribute to the player. Similarly, all the objects in a room are those objects which happen to have a parent of that room. When you call a Scope function, like ScopeInventory, the code actually finds all the objects whose parent is the player and adds them into a new list, which is returned.

So the player doesn't have an object list as such and rooms don't have an object list as such. The lists are generated on the fly in the Scope functions.

The core function for all this is GetDirectChildren, which is in the core C# files (and hence quicker than trying to do it brute force in the ASL code).

HegemonKhan
04 Apr 2013, 23:53
"gtk" (good to know) !!! :D

I used GetDirectChildren before, but I didn't realize that all the scopes where returning object lists using this function as their base.

my apologies for the mistaken comments about the object lists. I've still a lot to learn at understanding all this code stuff, heh.

psymann
05 Apr 2013, 01:29
jaynabonne wrote:
children = GetDirectChildren(container)

if (ListCount(children) <> 0) {
foreach (item, children) {
// Handle the next child object.
}
}


That works almost perfectly thanks :-D Whoo! I can now "look in" my container and it spits out a lovely list of what's in there :) Though I did cheat a bit and have two permanent non-takeable items in there so I can start my list with one of them, end with the other one, and put all the others in the middle so I can just simply put a comma after each one without worrying about how many of them there are ;)

(I say "almost" perfectly because I think I should probably include some sort of prefix, so for example "...a letter...", not just "...letter...". But that's a small thing and since I tried adding OutputTextNoBr(item.prefix) and it didn't like it, I'm going to quit while I'm ahead!)

Thanks! :D

psy

psymann
05 Apr 2013, 01:31
And no worries, HK - sometimes it'll help and be very useful, and sometimes it won't but may end up with me learning something else as a side-effect ;-)

jaynabonne
05 Apr 2013, 08:36
HegemonKhan wrote:"gtk" (good to know) !!! :D

I used GetDirectChildren before, but I didn't realize that all the scopes where returning object lists using this function as their base.

my apologies for the mistaken comments about the object lists. I've still a lot to learn at understanding all this code stuff, heh.


No need to apologize. I hope you take my comments not as criticism but as trying to help bring some understanding. I've been diving deep into the Quest code, and I still have quite a bit to explore. (Code spelunker me.) lol

Sora574
07 Apr 2013, 02:51
So... I've read through this thread and I like the suggestions and all... but if you're interested, I found a way that I think is simple, although it looks complicated, and says things like:
The container includes a box, 3 keys, 2 cookies, and a piece of chicken.

Of course, it wouldn't work right with things ending with 's' but... You can change that by adding the EndsWith function if you have to. I'm lazy though so...

Anyways, here's a tiny little demo I made. Just look at the box, and it will tell you what it has in it.
If you need me to explain anything, just ask.

EDIT: Oops, forgot to make it work if there's only one or no objects in the box lol. That's fixed now, so you can make it work with any object that has something in it.