Audience: Beginner (no HTML/CSS/JS experience needed)
What this shows
Where to set up the starting values of your story’s variables so they’re ready before the player sees the first passage.
How it works
- A passage named
UserScriptand taggedscript(written as:: UserScript[script]in a.tweefile, or tagged “script” in the Twine editor) runs its JavaScript exactly once, when the story loads — before the first passage is shown. window.story.stateis the same object as the shorthandsyou use inside<% %>template tags in passages, so anything you set on it here (likewindow.story.state.gold = 10;) is immediately available ass.goldeverywhere else in the story.
Using this in your own story
- Create a passage named
UserScriptand tag itscript. - Set your starting variables there:
window.story.state.someVariable = ...; - Reference them anywhere else with
s.someVariableinside<% %>/<%= %>tags.
Full source
:: StoryData
{
"ifid":"A610836B-1798-438B-8042-5631E4D323D3"
}
:: StoryTitle
Initializing Story Variables in Snowman
:: UserScript[script]
/*
Code in a passage tagged "script" only runs once, when the story starts.
This makes it a good place to set up the starting values of your story variables.
*/
window.story.state.playerName = "Adventurer";
window.story.state.gold = 10;
window.story.state.hasMap = false;
:: Start
Welcome, <%= s.playerName %>! You start with <%= s.gold %> gold.
<% if (s.hasMap) { %>
You already have a map.
<% } else { %>
You do not have a map yet.
<% } %>
← Previous: Basic Text Formatting · All examples · Next: Simple Score Counter →