Printing object names as a list

Hello everybody. Sorry for a probably stupid question, but how can I print the names of array’s objects as a list?
I’ve tried to use the script below, but it didn’t help:

<<set $LChestItems = [ $RoSS, $WolfSkin, $SkullCarved, $LockedBook]>>
<<print $LChestItems.join (",")>>

It just simply doesn’t appear when rendered.
P.S.: learning the Twine and SugarCube2 for a couple of days, so don’t know lots of things. Never tried any scripting in my life before (as you can see).

Twine Version: Twine 2
Story Format: SugarCube2

I’m not quite sure what you mean by “the names of array’s objects” in this context.

First, assuming that all of the four variables in the array below are objects:

<<set $LChestItems = [$RoSS, $WolfSkin, $SkullCarved, $LockedBook]>>

then you should be aware that, after a passage transition occurs (where all story variables get cloned), the array elements will no longer refer to the same objects as those object variables, thus changing the properties of one of those object variables will no longer change the previously corresponding element in the array or vice versa.

As for what you wanted, if those four variables are all generic objects and each have a “name” property, then you could add this to your JavaScript section:

/* Add Array.getNameList() method - Start */
Object.defineProperty(Array.prototype, "getNameList", {
	value: function () {
		// Collect the value of the "name" property on each element in the array.
		var list = "", len = this.length - 1;
		this.forEach(function (val, idx) {
			if (len === 0) {
				list += val.name;  // Lone element
			} else if ((len === 1) && (idx === 0)) {
				list += val.name + " ";  // First of two elements
			} else if (idx === len) {
				list += "and " + val.name;  // Last element
			} else {
				list += val.name + ", ";  // Any other element
			}
		});
		return list;
	}
});
/* Add Array.getNameList() method - End */

and that would create a new Array.getNameList() method, which you can use to display the values in the “name” properties of all of the objects within any array, like this:

<<print $LChestItems.getNameList()>>

If that wasn’t what you were trying to do, then please clarify what those four variables are and what “the names of array’s objects” means here.

I used kind of an advanced trick in the above code, so let me know if you want me to explain any of it as well.

Hope that helps! :grinning:

Sorry for my explanation of the problem (eng isn’t my mothertongue), but you’ve got the exact point of my question: yes, names are properties of generic (don’t know what does it mean) objects, and this macro you’ve written is a nice decision, thank you a lot, HiEv.

Also i didn’t understand the moments about passage transition and clonning of variables. And i have no idea how to write my own macro (even in case i have watched video tutorial of “VegeterianZombi” on youtube). I suppose, i need to know how to write with Java for it.
If it is not hard for you to explain this moments and share some information about nice tutorials, where its possible to learn macro writing and other useful possibilities, i would be deeply grateful to you.
Actually, I am already grateful for your detailed answers.

FYI - It’s not Java, it’s JavaScript. They aren’t the same thing.

If you’re like me, examples help. I have a bunch of examples of macros and widgets in my Twine/SugarCube sample code collection.

The “Class” Macro sample code is a good introduction to how to create a SugarCube macro. See also the <<ScrollTo>> Macro, <<hovertip>> Macro, <<seen>> Macro, and <<selectRange>> Macro examples.

And for examples of SugarCube widgets, see the <<SetPronouns>> Widget, <<HoverTxt>> Widget, <<checkboxPlus>> Widget, and <<textboxPlus>> Widget examples.

I wouldn’t recommend adding your own methods to JavaScript objects until you’re a bit more familiar with JavaScript, but here’s the breakdown of the above Array.getNameList() code:

Object.defineProperty(Array.prototype, "getNameList", {

The Object.defineProperty() method is the way you add a new property or method to an existing object, in this case the Array object. The method name added in this case is “getNameList”.

	value: function () {

You’d use “value:” like that for adding a method, and this tells you that the following function will return the value returned when you use the method you’re adding.

		var list = "", len = this.length - 1;
		this.forEach(function (val, idx) {

The “this” object will refer to the array that this method is being used on. It sets the “list” variable up as a string to contain the list of array elements, and sets “len” variable as the length of the array - 1. The forEach() method then loops through all of the elements in the array, executing a function that passes the value of each element as “val” and the current index in the array as “idx”.

			if (len === 0) {
				list += val.name;  // Lone element

If there’s only one element in the array, this sets list to be the value of that one array element’s name property.

			} else if ((len === 1) && (idx === 0)) {
				list += val.name + " ";  // First of two elements

If there’s only two elements, and it’s currently on the first element, then just add the value of the element’s name property followed by a space to the list variable.

			} else if (idx === len) {
				list += "and " + val.name;  // Last element

If you’re on the last element of the array, then add "and " followed by the value of the last element’s name property.

			} else {
				list += val.name + ", ";  // Any other element
			}
		});

For any other case, it adds the value of the element’s name property followed by ", " to the list variable. It then loops back up to look at the next element in the array.

		return list;
	}
});

Once it’s all done, then it returns the value of the list variable as the value output by this method.

I didn’t do any error checking in that, so if any of the elements of the array don’t have a name property, then that will cause the method to throw an error.

Hopefully you now understand how that code works.
 

Glad to be of assistance. :grinning:

2 Likes

Wow… your collection is so extremely rich with great examples! That will definately help a lot.
Thank you!
Wouldn’t you mind if i’ll ask you some possible future questions (hope they won’t be many) in private messages or here (just visited this forum for the first time and not quite sure how to use all features of it)?

I generally prefer that you ask any questions publicly. That way other people can add their input and/or learn from what was discussed.

I’m also available at the r/twinegames subreddit where I’m one of the moderators.

Have fun! :grinning: