0 votes
by (1.3k points)

So, I have this kind of thing:

<<piercingsRandom random(2, setup.pcPiercingCount())>>

Can I do something like this? Or do I need to pull it out, assign it to a variable and then use it in the widget call?

I'm expecting setup.pcPiercingCount() to come up with a value. piercingsRandom expects a simple integer argument.

Let me show this other thing I cobbled together:

/* Returns the number of parts that are pierced. */
setup.pcPiercingCount = function () {
	var sv = State.variables;
	var cnt = 0;
	Object.keys(sv.pcPierced).some(function (part) {
		if (sv.pcPierced[part].scale > 0)
			cnt++;
	});
	return cnt;
};

It works, but I really don't understand if it's essential to have it like this. Maybe there's a simpler way?

With the piercing stuff, I decided to limit the print out. So, it'll present a randomized listing between this and that number, to give it variety. It works, except for this bit. So... what do?

1 Answer

0 votes
by (68.6k points)
selected by
 
Best answer

Yes, you can do that.  No, not as you're attempting it.  RTFM.

<<piercingsRandom `random(2, setup.pcPiercingCount())`>>

 

Also, you're using the wrong array method.  The <Array>.some() method has a specific use, and this isn't it.  You want <Array>.forEach().  For example:

/* Returns the number of parts that are pierced. */
setup.pcPiercingCount = function () {
	var sv  = State.variables;
	var cnt = 0;
	Object.keys(sv.pcPierced).forEach(function (part) {
		if (sv.pcPierced[part].scale > 0) {
			cnt++;
		}
	});
	return cnt;
};

 

by (1.3k points)

Ah cool. Saved again.

If you're curious about the random widget, I based it on the previous one:

<<widget "piercingsRandom">>
	<<set _piercingList = setup.pcGetPiercingDescs()>>
	<<if $args[0] > _piercingList.length>>
		<<set $args[0] = _piercingList.length>>
	<</if>>
	<<set _piercingList = _piercingList.randomMany($args[0])>>
	<<set $pcPiercings[0] = _piercingList.length>>
	<<if _piercingList.length == 2>>
		<<set $pcPiercings[1] = [_piercingList.slice(0, -1)
		.join(', '), _piercingList.slice(-1)[0]]
		.join(_piercingList.length < 2 ? '' : ', and ');>>
	<<else>>
		<<set $pcPiercings[1] = [_piercingList.slice(0, -1)
		.join('; '), _piercingList.slice(-1)[0]]
		.join(_piercingList.length < 2 ? '' : '; and then... ');>>
	<</if>>
<</widget>>

But, I took a lot of the preformatting text back into the main passage, only keeping fragments. But, I do turn it into a string, since the user might want to save the layout, if they happen across some randomized mix they like. It works. Works pretty well, so thanks a lot for all that help from before. And then again. :P

...