GetDirectChildren is yanking my chain!

Hey there...

I want to use

 GetDirectChildren(atticusstock)

Where "atticusstock" is a stockroom (I'm setting up a book store).

When printing this

"Available titles are: " + GetDirectChildren(atticusstock)

I get this:
List: Object: Pippi Longstocking;
(yes, I've been using Pippi as tryout book, I'm Swedish, there.)

I do do not want "List: Object:" to display. Same with the semicolon at the end.
I just want each book title to get printed, seperated by a comma.

And, of course, I can't get it to work.


(filler for getting my edited post, updated/posted)
(again, filler for getting my edited post, updated/posted)
(again, filler for getting my edited post, updated/posted)


the 'GetDirectChildren' Function creates and returns an Object List, so you either got to do:

(examples below using 'Variable' VARIABLES, but you should use 'Attribute' VARIABLES instead)

  1. (see code box below)
create ("parent_object")

object_variable = create ("child_1_object")
object_variable.parent = parent_object

object_variable = create ("child_2_object")
object_variable.parent = parent_object

object_variable = create ("child_3_object")
object_variable.parent = parent_object

// the 'GetDirectChildren' will NOT include this item (but if you used 'GetAllChildObjects' instead, it WOULD include this item):

object_variable = create ("child_of_child_1_object")
object_variable.parent = child_1_object

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

// the above creates this heirarchy:

parent_object
-> child_1_object
->-> child_of_child_1_object
-> child_2_object
-> child_3_object

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

objectlist_variable = GetDirectChildren (parent_object)

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

msg ("Available titles are:")

msg ("")

DisplayList (objectlist_variable, true)

// output:

Available titles are:

1. child_1_object
2. child_2_object
3. child_3_object

or (this will put the items on same single line, so, this is generally what you're asking for in your post)...

  1. (see code box below, this is actually quite advanced/comfusing, you asking to do something quite difficult, so don't feel bad about having trouble with trying to do it yourself, lol)
create ("parent_object")

object_variable = create ("child_1_object")
object_variable.parent = parent_object

object_variable = create ("child_2_object")
object_variable.parent = parent_object

object_variable = create ("child_3_object")
object_variable.parent = parent_object

object_variable = create ("child_child_1_object")
object_variable.parent = child_1_object

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

// the above creates this heirarchy:

parent_object
-> child_1_object
->-> child_of_child_1_object
-> child_2_object
-> child_3_object

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

objectlist_variable = GetDirectChildren (parent_object)

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

concatenation_string_variable = "Available titles are: "

numbering_integer_variable = 0

list_count_integer_variable = ListCount (objectlist_variable)

last_index_number_integer_variable = list_count_integer_variable - 1

second_last_index_number_integer_variable = list_count_integer_variable - 2

for (index_number_increment_integer_variable, 0, second_last_index_number_integer_variable, 1) {

  numbering_integer_variable = index_number_increment_integer_variable + 1

  concatenation_string_variable = concatenation_string_variable + "(" + numbering_integer_variable + ")" + ObjectListItem (objectlist_variable, index_number_increment_integer_variable) + ", "

}

concatenation_string_variable = concatenation_string_variable + ", and (" + list_count_integer_variable  + ") " + ObjectListItem (objectlist_variable, last_index_number_integer_variable)

msg (concatenation_string_variable)

// output:

Available titles are: (1) child_1_object, (2) child_2_object, and (3) child_3_object

In this case, there's already a function to do what you want.

msg (FormatObjectList ("Available titles are:", atticusstock, "and", "nothing"))

This turns an object list into a string, putting "and" between the last two, or "nothing" if there's nothing in it.

Is 'Atticus' a common name for bookshops? I've only seen one, and that's a quirky independent place that gave up being a bookshop to sell and repair accordions, then went back to being a bookshop a couple of years later, without changing its name.


An English analogy, in case it helps :p

John said “Grace” - John is saying one word, five letters.
Dave said grace - Dave is reciting a prayer that the word refers to.

It's the same in Quest:
someVariable = "Pippi Longstocking" - the variable is a string containing 17 letters and a space
anotherVariable = Moby Dick - the variable is an object (probably a book), which has a title, a description, a location within the game world, a set of scripts to govern whether it can be picked up, and a whole bunch of other data.

If you pass an object to msg, it doesn't really know what to do with it. Because msg is supposed to print simple textual messages. There isn't really a sensible way to display all the data contained in an object as a line of text, because Quest doesn't know which of the pieces of data within that object you actually want to show. You need to tell it something like msg(GetDisplayName(some object)) or msg (some book.description). If you don't, it just guesses, and prints "Object: Some object".

GetDirectChildren() returns a list of objects. So you need to go over that list one at a time, telling Quest that it's just the names you want. This is something that Quest already knows how to do when printing a room description, so FormatObjectList() does the job for you. It makes a list of the objects in a room, and gives you that list as a string, ready for sending to the user. In other circumstances, you might find FormatList() and Join() useful for turning lists into strings.


There's not enough coffee in my system at the moment. I'll re-re-read this again later and try to make sense of it.
Thanks a lot for answering!


K.V.

If we could vote, I'd vote for this as the best possible answer (that's right; I said "possible"):

msg (FormatObjectList ("Available titles are:", atticusstock, "and", "nothing"))


K.V.

By the way, that's an excellent analogy, mrangel!

...and a nice breakdown, HK!


Indeed! I'm very grateful. I fell asleep early yesterday and still haven't tried it. Will try to sort it out today!


HK not too smart, doing that code, when there's already the 'FormatObjectList', meh... oh well... maybe it can be use for someone, at least people can study it for concept with learning to code, meh... lol


Still haven't tried it out. Haven't had much time this week and when I've spent time in Quest I've mostly been working on other stuff.


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

Support

Forums