Passing dice rolling scores to the next passage

Twine Version: 2.7.1.0
Hi all,
I have written a Javascript function that rolls 2d6 for a skill, luck and strategy scores and it saves them in a scores box within the same passage.
But whatever I try to do, I just can’t get these scores to be stored and passed through to the next passage (same scorebox is setup on that passage, it just will not populate)

Does anyone have any experience of passing rolled scores through to the next Twine passage (I’m using Sugarcube) or could suggest a simple solution?

Thanks in advance.

What’s a “scores box” and how are you saving the roll values into it? Showing your code would be easiest.

Sure. And thanks for your response.
The scores box is an area (a banner at the top of the screen) where the dice roll scores are stored.
Below is the full JS code for rolling the dice and storing the scores in the banner section.
This works fine, in that it rolls 2d6 scores for each attribute and stores them in the scores section of my passage.

// Roll the dice
luckScore = Math.floor(Math.random() * 6 + 1) + Math.floor(Math.random() * 6 + 1);
skillScore = Math.floor(Math.random() * 6 + 1) + Math.floor(Math.random() * 6 + 1);
staminaScore = Math.floor(Math.random() * 6 + 1) + Math.floor(Math.random() * 6 + 1);

    // Update the scores
    setupBanner();

Are you running pure JavaScript in your passage? If so, how? If it’s via a <script> tag, then you probably don’t want to do that.

I’m asking because I’m getting the feeling you’re fighting the story format by treating it like a web page, rather than the single-page [web] application (SPA) that it is.

Anyway. Story variables are how you do persistent state in a story format. Story formats also provide utility macros/functions/methods to handle various things, like random numbers, for you.

As one example:

/* Roll the dice */
<<set $luck = random(1, 6) + random(1, 6)>>
<<set $skill = random(1, 6) + random(1, 6)>>
<<set $stamina = random(1, 6) + random(1, 6)>>

/* Update the scores */
<<run setupBanner()>>

Now, I doubt that setupBanner() will work as-is in the above example, but that’s the kind of thing you’d most often see.

Here’s another example that’s probably closer to what you’re doing now, but using the <<script>> macro—note the double angle brackets:

<<script>>
/* Roll the dice */
const sv = State.variables;
sv.luck = random(1, 6) + random(1, 6);
sv.skill = random(1, 6) + random(1, 6);
sv.stamina = random(1, 6) + random(1, 6);

/* Update the scores */
setupBanner();
<</script>>

Again, setupBanner() will likely need adjustment.

Excellent advice thanks, yes I was using my previous js skills and tags treating it like a webpage.
However, I have now set my score variables to 0 at the start of my first Twine passage:

/* Initialize the scores with initial values */
<<set $luckScore = 0>>
<<set $skillScore = 0>>
<<set $staminaScore = 0>>

But when I display this webpage, <> displays three times on my page:

This might be a noob question, but is it about how I’m setting the variables and trying to initialize them at the same time?

cheers

You must be in Test/Debug mode. When you “play” a project, this won’t appear on the page.
If you hover your mouse on the <<set>> you should see the relevant code, where it sets a variable.

You’re spot on, thx Manon. I just knew it was a noob question…

Don’t worry :slight_smile: happens to all of us

You should really do your startup initializations within the StoryInit special passage—the name is case-sensitive, like most everything in SugarCube.

The primary reason being that if you test—via Play or Test—from any passage other than your starting passage, your initializations won’t be run. The StoryInit special passage, on the other hand, always runs during startup.