Randomizing X passages across Y domains

Twine Version: 2
[also choose a Story Format tag above]

Hi there,

I am trying to create a quiz. It will have ~100 multiple choice questions, each in its own passage, across 5 domains designated by Tags.

When the user initializes the game, I want to select 5 random passages from each domain. I then want the the user to move through the randomized passages, domain by domain.

As proof of concept I have set up one domain with passages that should simply link to the next passage. I have a start page that works correctly, getting the user to a random question.

However I get Error: cannot execute macro <<goto>>: Story.has title parameter cannot be undefined when I click to the next passage.

Here is my Start passage:

:: Start
<<set $geographyQuestions = Story.lookup("tags", "Geography")>>
<<set $selectedGeography = $geographyQuestions.randomMany(5)>>
<<set $allQuestions = [].concat($selectedGeography)>>
<<set $currentQuestionIndex = 0>>
<<set _firstQuestionTitle = $allQuestions[$currentQuestionIndex].title>>

Begin the quiz!

[[Start Quiz->_firstQuestionTitle]]

This works fine. I go to a random passage tagged [geography]`.

Here is what the passages currently look like:

:: Geo_Q1 [Geography]

This is test passage 01 . [[Next Question->NextQuestion]]

Here is my nextQuestion.tw passage. I suspect this is where my problem lies:

:: NextQuestion
<<set $currentQuestionIndex+1>>

<<if $currentQuestionIndex < $allQuestions.length>>
    <<goto $allQuestions[$currentQuestionIndex]>>
    You've completed the quiz! [[Back to Start->Start]]

I have confirmed all my passages exist. They are tagged properly (just 6 passages with the same content as above, just different numerals for example :: Geo_Q2 [Geography]).

It looks like for whatever reason I am not getting the next passage’s title param and throwing an error. Any ideas?

Thank you!

Well, since this worked:

<<set _firstQuestionTitle = $allQuestions[$currentQuestionIndex].title>>

have you tried?

<<goto $allQuestions[$currentQuestionIndex].title>>

~ ~ ~
Found a lil issue:

Should be, according to the docs

=> <<set $currentQuestionIndex +=1>>

You could also do in the previous passage: {Docs}

[[Next Question->NextQuestion][$currentQuestionIndex +=1]]

Also, I would avoid usigng <<goto>> outside an interactive macro (like <<link>> or <<button>>)


Hmm, those didn’t quite work but you got me thinking. I believe I have it now working using a simpler concept: splitting each domain into “chapters” and making a little start page for each. This seems a little simpler than trying to do it all at once on the Twine Start page:

:: Start
<<set $geographyQuestions = Story.lookup("tags", "Geography")>>
<<set $selectedGeography to $geographyQuestions.shuffle().slice(0,5).map(p => p.title)>>
<<link "Start Chapter 1">>
    <<goto $selectedGeography.pop()>>

Then at the end of each the passages:

<<if $selectedGeography.length > 0>>
    <<link "Next Question">>
        <<goto $selectedGeography.pop()>>
    <<link "Next Chapter">>
        <<goto "next_domain_start_page_title">>

Repeat the `::Start`` page pattern for all other domains.

It’s not quite as elegant as I’d like but it’s enough to get me going. Thank you!

1 Like

It’s worth suggesting that you would be better doing this in the special StoryInit passage than in your Start passage. Otherwise, if your game is loaded from a different passage (e.g. when testing) those variables will not be set.

1 Like