Twine Version: Twine 2
Story Format: Harlowe3.2.3
Hi all, sorry to be a bother again - I have gotten stuck again with some Twine coding in my story.
I am wanting to set things up so that the user selects a difficulty level, and based on that choice, they see a number of cases to solve, randomly selected from a larger set of cases. (For example, in “easy” mode they would get 4 cases out of a set of 8).
I have shamelessly plagiarized from greyelf’s code from here - thanks greyelf!!! - (Harlowe) How to set random passages to one link? - Twine Q&A - to set up an array and randomly select the 4 cases.
This is the code that I am using to set up a shuffled array of the cases:
(link-reveal-goto: "Play the game: level of difficulty - easy", "instructions")[(set: $choice to 4)(set: $list to (shuffled: "Case 1", "Case 3", "Case 4", "Case 5", "Case 7", "Case 8", "Case 11", "Case 14"))]
and similar for the other levels of difficulty.
Then this, below, is the code I am using to set up the links (in a footer passage, if that matters?) (The time counter $t stuff is tracking how much time the user spends in the game, Is it possible to measure the amount of time spent in the game (Twine, Harlowe 3.2.3)? - not actually super relevant to this post but I’ve left it in so you can see my actual code.)
(if: $choice is 4)[(link-reveal-goto: "1", $list's 1st)[(set:$t to it + time)]
(link-reveal-goto: "2", $list's 2nd)[(set:$t to it + time)]
(link-reveal-goto: "3", $list's 3rd)[(set:$t to it + time)]
(link-reveal-goto: "4", $list's 4th)[(set:$t to it + time)]]
Okay, and this works just fine, I can see when I test it that it shuffles randomly through the cases and the link-reveal-goto links correctly link to the cases, as intended.
But, the problem that I have is that I don’t actually want the link text to be “1” etc. - I’ve just put that in as a placeholder for now.
I need the link text to be a different variable depending on which case is chosen. Each case has a different variable ($Cn, a number which I’ve generated using random), for example:
(set: $C1 to (10 + (random: 1, 25)))
(set: $C2 to (40 + (random: 1, 25)))
So I want to somehow, from my shuffled list, associate the correct variable with the case number being selected in $list’s 1st, and display that variable $Cn as the link text (i.e. if Case 4 was 1st in $list, then the link text would display $C4). I suppose there could be a way to do this with some really gnarly if: and ifelse: statements but it kind of makes my brain hurt just thinking of it?
Do I need to set up a datamap with the cases and $Cn variables? This is going to sound daft but I’ve read the manual section on datamaps three times and I don’t really understand how I could apply it to this situation.
I had thought that alternatively to using the $Cn variables, I might try setting up a datamap of the shuffled list - and assigning a new number to each case, i.e.
(set: $dm to (dm: $list's 1st, 1, $list's 2nd, 2, $list's 3rd, 3, $list's 4th, 4))
so that now, if Case 4 was 1st in $list, it would be assigned to 1 - and this solution would work for what I’m trying to do with the story - but I can’t work out how, from there, to replace all of the instances of $C4 with 1 in the text of my story. Because it might actually be $C7 that I would want to replace with 1 - depending on the order of $list. So I am not sure how that could work really, in actual code.
(I have just focused on the code for “easy” mode in the above, but would need to apply the solution to slightly larger sets of cases for the harder game modes as well - with if statements based on $choice)
I am incredibly sorry if this is a dumb question - I’ve just worked my brain into knots to the point where nothing is making sense anymore. I think what I want to do should be quite simple really? I’m sure I must just be missing something - just no idea what. I would be really grateful if y’all could help unknot things!