0 votes
by (160 points)
I can name passages as numbers, I suppose, at leave it at that.  But I would like to take the existing order of numbers and shuffle them.  But how to rename (renumber) passages before the story starts?  Or in the build?  That would require also globally renaming links, I realize.

I am adept at js, and I can easily create code that can shuffle items in an array.  But there doesn't seem to be a way to apply that to a story to dynamically rename passages.

Perhaps there is a way to do this with tags? Or variables?

2 Answers

+1 vote
by (63.1k points)
selected by
 
Best answer

Without knowing the story format, it's hard to give specific advice. This will be fairly easy to implement in SugarCube, harder (but possible) in Harlowe. 

I would implement it by: 

  • Tagging the passages you want to be randomized. Or the opposite. Assuming there are some passages that need to be fairly static, like starting and ending passages. 
  • Create an array of the appropriate passage names using the above information. Easy to do with Story.lookupWith() in SugarCube. 
  • Create a function that returns one of the passage names randomly (or by plucking it, if some / all passages should not be reused). 
  • Use that function in your links. 

I would not recommend altering passage names. Treat them as read only at run-time. 

SugarCube example code (goes in Story JavaScript: 

(function () {
    var passages = Story.lookupWith( function (passage) {
        return passage.tags.includes("random");
    }).map( function (passage) {
        return passage.title; 
    });
    
    function randomPassage () {
        var psg = passages.pluck();
        if (!tags(psg).includes("unique") {
            passages.push(psg);
        }
        return psg; 
    }
    setup.randomPassage = randomPassage; 
    
}());

Usage: 

[[link|setup.randomPassage()]]

In the Twine 2 app, you'll need to delete the passage named "setup.randomPassage()" that is auto-created before the link will work correctly. 

The above code will return passages randomly if they have the "random" tag. Passages tagged "unique" will be removed from the array after being selected. 

Note: this code was written from memory and may contain syntax errors, but the gist should be correct. 

If you're using Harlowe: if you're comfortable with JavaScript, consider switching, especially if you're early on. This is possible in Harlowe but only through hacks and such. 

by (160 points)
Ah, that is not exactly what I was looking for, but I believe it gives me a way to get there.

Writing a forward-moving story adventure, not like Zork at all; it branches, but does not loop, does not simulate an "explorable space."  

Visually, what I am looking to do is have a # display before the passage name.  First passage of the story will always be 1, last passage will always be the last number assigned (say #500 out of a total of 500 numbered passages).  In between, I would like to randomize the number associated with the passage so that no discernable progress sequence.

So I think the way to go is to store the passage names in a dict {passageName: "", passageNumber} with an ascending value in passageNumber.  Then use a Fisher–Yates shuffle items 2 through n-1.

Need a function to make links display as the corresponding #s, too.  If I get it working, will post code!
by (810 points)
If you want the passage numbers to be consistent from play to play, then I’d write a special sort function on the passage names to generate it—say, alphabetical order on the reversed string.  Or process the source (outside of Twine) to make the passage ids match what you need, then use them.
0 votes
by (2.7k points)
What do you want to achieve by changing the passage names?

Perhaps you will change room connections, which might be changed by links with random targets,  or which could be expressed by a data array?
...