I am trying to randomize my choices in a passage. I am aware of either() but I realize that if I use either(), the correct choice would appear twice or it won't appear at all. How do you make it so that the choices won't repeat itself?
Ex. He went left! I better follow him!
When you say randomized do you mean:
a. You want all the options to appear but in a random order.
b. You want one or more of the incorrect options to not appear but the options that do appear are always in the same order.
c. A combination of points A and B.
d. Something else....
And as greyelf says, knowing the story format you're using is kind of important.
What I am trying to do is make sure that the correct choice is not repeated and that the order of the choices are randomized if that make sense. And I'm not sure what you mean by story format. I'm using Twine 1.4.2 if that's what you mean.
Maybe I should start a different thread, but really the subject is the same : how to randomize choices given to a player so they can't memorize a pattern.
On the dev side the problem becomes : how to randomize Twine links in a passage?
When you feed a dummy example to that function with 4 choices labeled option 1, 2, 3, and 4, like this :
logging the value of the content variable above in the console, I get the following :
and of course different order each time I run it.
Now that's all good and it works but that does not interface with Twine's passages, which require an extra mile of coding.
How can I integrate such typical Twine player's choices :
to translate into the resulting HTML, knowing that the above Twine's passages transforms eventually to :
I am not sure this is the right way to go, but to be useful for devs, the macro would look something like this:
and use it in a passage like that :
Is there a better way to do this ?
First. You don't need getRandomInt(), SugarCube has the built-in random() function (among many functions and methods which deal with randomness in some way). I'd suggest reading the documentation before deciding to reinvent the wheel.
Second. I'm unsure why you're manually recreating <a> links in exactly the same way as SugarCube does automatically. You probably don't want to do that.
Anyway. Let's say you initialize your array something like the following: You can destructively print out the randomized links like so: That does destroy the $choices array, however, so if you need it intact afterwards, you can print it non-destructively like so:
If you wanted them wrapped in additional markup, for example an unordered list, you might do something like the following: You should be able to figure the destructive version out from there, if you'd prefer that.
You can use a widget to implement a similar macro to your <<randomizeChoices>> one.
eg. The following widget is based on TheMadExile's pluck based solution: ... you use the following to call it: note: I added indents and line-breaks to the above two examples for readability, they can be safely removed.
The widget / macro wrapper was definitely my intention because I want all my passages to present randomized choices to the player.
Thanks a lot again
I just hit another (stupid and easy) problem, but can't seem to solve it!
Following this thread, say I want some of my choices-to-be-randomized to depend on a gender-like widget, as defined in the documentation :
Now of course this won't work with :
I tried to isolate the use of the widget in a variable but it all boils down to not being able to capture the widget's value and encapsulate that result in quotes, in order to then insert it as a choice in the array of choices to be randomized.
It must be obvious but I don't see it. Can anyone help ?
1. A widget does not return a value, it can on the other hand output content to the page.
2. A Markup link does not support embedding a macro call / variable the way you are trying to do. The following test show this:
@TheMadExile would be a better person to answer this but the only ways I could think of to resolve the problem all revolved around different ways to generate the string values that are to be converted into markup links. The following is one such method:
a. Change your he widget to assign a value to a $pronoun variable: b. Use the $pronoun variable to build your possible choices:
If you're using the widgets, you could then refactor them to use the functions via the <<print>> macro, so that you'd only have one set of pronoun code to maintain. For example: (I'm using the <<=>> alias here) Of course, in that case, you could just use the <<print>> macro directly, like so:
I'll use a mix of both solutions, namely the framing of @greyelf and the form of @TheMadExile Because ultimately here, I want as little code as possible in the passages, I'll go something along those lines (avoiding the many push() statements):
the good thing is, those variables don't change as long as genders don't change in the game. So far I am setting them to masculin by default, and updating them once and only once as soon the genders are known, before the story even starts.
For my own curiosity (since the documentation isn't that verbose on the subject), would it be a good place to use <<remember>> on those variables ? Or does it serve a totally different purpose?
Thanks again guys, great help!
Within a single playthrough (incl. saves) all $variables are automatically "remembered". So, unless the gender choice is one that the player should only ever make once (even across multiple playthroughs/restarts), then you definitely do not want to use <<remember>>. It is only useful when you want a $variable to persist across different playthroughs/restarts.
On the flip side, an achievement system would be an example of a reasonable use of <<remember>>.
Thanks for the added details