Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Narrative Randomisation

Hello friends!

I am trying to create a story that simply randomises a series of passages I have written.

Currently I am using the either function to link to random passages: [[>|either("P2","P3")]]

This is working well, however, a problem arises when the reader revisits the same passage several times and I only want them to view each passage once.

I played around with the visited() function but it became a little too confusing.

There is probably a very simple answer, and I would enjoy it thoroughly if you could share it with me!

Thanks,
Ben

Comments

  • You need to state which story format you are using as answers can be different for each one.

    If you are using SugarCube then you could store the list of possible passage names within an array $variable and then use the pluck function (2.x or 1.x) which will randomly give you the next passage name while at the same time removing that passage name from the list.
    <<set $list to ["Passage 1", "Passage 2", "Passage 3", "Passage 4"]>>
    <<set $next to $list.pluck()>>
    <<link "Next" $next>>
    
  • You noted that you want the player to visit each passage at most once, however, you failed to note what should happen once they have visited them all.

    A couple of notes on greyelf's SugarCube solution:
    1. It wasn't mentioned, however, you'll need to set the array $list before the passage in question. Say, either in some previous passage which the player can only visit once or within the StoryInit special passage. If you do it within the same passage, as is seemingly shown, then it will perform exactly the same as using either($list) or $list.random(), since you'll be resetting the list every time.
    2. The <Array>.pluck() method returns the undefined value when the array is empty. So, if it's possible for the player to visit the passage more times than the number of passages within $list, then you'll need to handle that. Three easy things which could be done once the array is exhausted are: disable the link, link to a default passage, or reuse the last available passage title.
    So, keeping the above notes in mind, example solutions to the array exhaustion problem follow.

    Example Setup (should be done only once):
    <<set $list to [ "Passage 1", "Passage 2", "Passage 3", "Passage 4" ]>>
    

    Example: Disabling the link
    This will link to the plucked passage if $next is defined, otherwise the link will be disabled.
    <<set $next to $list.pluck()>>\
    <<if def $next>>[[>|$next]]<</if>>
    

    Example: Linking to a default passage
    This will link to the plucked passage if $list.pluck() returns one, otherwise to the specified default passage.
    <<set $next to $list.pluck() or "Passage 5">>\
    [[>|$next]]
    

    Example: Reusing the last passage title
    This will link to the plucked passage if $plucked is defined, otherwise to the last successfully plucked passage.
    <<set $plucked to $list.pluck()>>\
    <<if def $plucked>><<set $next to $plucked>><</if>>\
    [[>|$next]]
    
  • Wow. Thanks guys. I will have a play with this over the weekend and let you know if there are any issues. I am using 1.42 and Sugarcane is the format.

    Thanks again,
    Ben
  • Sorry guys. I'm still very new to the coding side of Twine. Most of this is still very confusing.

    I tried defining $list in the StoryInit passage and then plugging in the following code into the Start passage:

    <<set $next to $list.pluck()>>
    <<link "Next" $next>>

    But it returns an error saying that link is not a macro or passage.

    Capture.jpg

    Help,
    Ben
  • As noted, by both greyelf and I, the examples we gave were for SugarCube. You are using Sugarcane.

    To make what you tried work in Sugarcane, use the following:
    <<set $next to $list.splice(random(0, $list.length - 1), 1)[0]>>
    [[Next|$next]]
    
  • Thank you!
  • What is the Sugarcane equivalent to
    def
    
    ? I've been trying to implement the array exhaustion solutions you suggested, but I can't figure it out.
  • For the "Disabling the link" example:
    <<if typeof $next !== "undefined">>[[>|$next]]<</if>>
    

    For the "Reusing the last passage title" example:
    <<if typeof $plucked !== "undefined">><<set $next to $plucked>><</if>>\
    
  • Thanks guys, your Sugarcube version will be a huge help. I wanted to randomize phone calls or police dispatches- no answer, booted to voice mail, etc. It was a long term want, not a need, but this should make it easy. I should be able to do an 'If Visited' to keep from having the same actual calls occur.

    Thanks a ton for handling a future question.
  • edited July 2015
    For the "Disabling the link" example:
    <<if typeof $next !== "undefined">>[[>|$next]]<</if>>
    

    For the "Reusing the last passage title" example:
    <<if typeof $plucked !== "undefined">><<set $next to $plucked>><</if>>\
    

    I tried the example you gave relating to disabling the link. It works the best of anything so far, however there is a click-able next link highlighted in red on the final passage. I think I understand what is going on, I just don't know how to fix it.

    Thanks again for all of your help.

    - Ben
  • A link that is red highlighted indicates that the link's target passage does not exist or can't be found.

    If your are sure that the passage does exist, then check that the target passage of the link is spelt exactly the same as the title of the actually passage, the case of each letter is import.
  • greyelf wrote: »
    A link that is red highlighted indicates that the link's target passage does not exist or can't be found.

    If your are sure that the passage does exist, then check that the target passage of the link is spelt exactly the same as the title of the actually passage, the case of each letter is import.

    Thanks Greyelf, the problem is that the passages are defined in the array at the start of the story and then used throughout, but once the array is exhausted the red highlighted text appears. I just want to know how to remove that instance.

    Thanks,
    Ben
  • We have no idea what you've cobbled together from these examples, so please show your code.
  • My Story Init passage has this code:
    <<set $list to ["P1", "P2", "P3"]>>
    <<set $next to $list.splice(random(0, $list.length - 1), 1)[0]>>
    

    The start passage:
    [[Next|$next]]
    

    Finally, at the end of each passage:
    <<set $next to $list.splice(random(0, $list.length - 1), 1)[0]>>
    <<if typeof $next !== "undefined">>[[Next|$next]]<<endif>>
    

    Everything works, except at the end there is the false red link.

    Thanks,
    Ben
  • Ah, you're probably running afoul of the daft auto-initialization code in the vanilla story formats (which, in this case, is clearly doing the wrong thing).

    Changing your end of passage code to something like the following should work:
    <<if $list.length gt 0>>\
    <<set $next to $list.splice(random(0, $list.length - 1), 1)[0]>>\
    [[Next|$next]]\
    <<endif>>
    

    Though, if you're going to be appending that to most passages, then you'd probably be better served by making a pseudo-macro out of it. And, perhaps, calling that from a postrender task.
  • That worked beautifully! Thank you so much!! I will have a play with the pseudo-macro as well.

    Thanks again for putting up with my antics :)

    - Ben
Sign In or Register to comment.