Hello everyone,
I'm currently trying to add NPCs to my game nothing to difficult right? but I don't want the NPCs to be the same on each game so I did something like that (keep in mind that it's a shortened version)
<<set $NPC to {
name: either("Lara", "Amelia"),
eye: either("blue", "green", "brown"),
hair: either("red", "blonde", "brown", "black"),
}>>
Now I don't want to have to rewrite all this code every time since all my NPC are not created at the start of the game.
Do you have suggestions for me? I tried a couple of things but beside setting them all at the start ( I fear it would make the launch unnecessarily long) or write the code at the start of the passage the npc is created I have no idea. Idea that works at least.
Comments
1. A Javascript Object Constructor to create each of your NPCs:
Add the following to your script passage, it creates an NPC object constructor which you can use to create NPC objects with random attributes.
2. By using SugarCube's built in clone method
The following passage shows how to use both of the above techniques:
I would like to see that done. is it just as simple as
<<widget "NPC">>
<</widget>>
Then you just write <<NPC>> in passages where you want it.
What I have to realize is that to learn SugarCube I should go back to Twine 1/SugarCube tutorials and that they still work over here in Twine 2 (with a few exceptions of course).
I'll go with Claretta's suggestion for this one since i'm on SugarCube and the widget is a bit more intuitive to use but i'll remember your suggestion greyelf if i use something else than sugarcube.
Once again both of you thank you.
My NPC Widget returns nothing.
Do I need to <<print Args[]>> or something?
If your NPC widget is similar, then….
For example, continuing with Claretta's example: To use it to setup a new random NPC and then output details about that NPC, you could do something like the following:
Though, you probably wouldn't want to use the widget as-is, since it offers no protection against making NPCs with the exact same name, but differing in other details (i.e. it would be odd if Lara had brown eyes the first time you met her, but green the next).
I agree with TME that the example code is incomplete in this instance.
So is there a way to have it remove an option from that list of choices once it has been selected? Or is that non-trivial?
That will reroll the <<NPC>> widget if you get a Lara. If you set the widget up right, you could set it up in an <<elseif>> loop, so that it constantly rerolls until it gets a valid name.
Though you'd also need a way to record chosen NPC names as they are chosen, so you could do something like
No guarantees that this will be the best solution, as I'm prone to inventive but meandering code. For small arrays though, like only 3 name choices, it would work well enough.
note: the following example uses the Javascript code from this post to handle the finding and removing of an array element from the $nameList array.
Try the following StoryInit and Start passages:
AH..... yes. That DOES work.
Thank you for being amazing!
::Start::
::StoryInit::
Javascript
It will output:
1. You do not need to polyfill ES5 (or below) native methods in any SugarCube version. You do not need to polyfill ES6 (or below) native methods in SugarCube 2.x. TL;DR: It's beyond pointless to polyfill Array.prototype.indexOf() in SugarCube because there's already a polyfill for it (for, you know, the one browser that needs it).
2. Rather than using the either() function and your Array.prototype.removeElement() method, why not simply use SugarCube's Array.prototype.pluck() method (2.x and 1.x)? For example:
Why? ha! Because until you mentioned it I had NO IDEA what it was!
Let me give it a try right now.
So is there a way to save off each person as a new entity?
So then, like this:
Regardless of how you're doing it, if you are removing entries from the list of names, then you'll need to ensure that you do not call for a new random NPC more times than you have names or things will get weird (because there will be no names left to use). Simply being vigilant works, or you could pre-generate your NPCs, or you could employ an NPC cache, or….
Just something to keep in mind.
(and yes, carp was intentional.... seemed funnier that way)
It was my own laziness that I did not search to see if the story format in question has implemented its own version of a method for array element remove.
Also, if by "they" you mean MDN (on its page there), then no, they do not suggest the use of the polyfill. They simply provide it and note that some browsers may need it. It may seem like I'm splitting hairs here, but the distinction is real and important. Beyond that, AFAIK, Harlowe, like SugarCube 2.x, includes polyfills for ES5 and ES6*, so it shouldn't need it either.
* Native object methods only. The new syntax in ES6 would require a transpiler, which SugarCube 2.x does not include, and I don't think Harlowe does either (though, admittedly, I could be wrong about that; I don't claim to be an expert on Harlowe).
Tut tut, there's no need for that. It was not my intention to lambaste anyone. I was just pointing out that polyfills are unnecessary with SugarCube.
Yes, I probably showed my exasperation with the situation more than I needed to (which is to say, at all), but it just begins to wear on you when no one bothers to read the docs.
Thanks.
I didn't realize that SugarCube accepted the double-pipe. I was trying to help someone on the forum with "or" earlier and just didn't know. Thanks.