HTML Twine/Sugarcube - Best Practices help

CruzCoda

New Member
Aug 15, 2019
9
7
I'm working on my first Twine/Sugarcube based game, and looking at some tips for best practices. One of my major questions, is where the best place to store runtime variables would be?

I know there's the StoryInit passage, but that only runs when the story is first run, which means a new game must be started each time an update happens (as far as I can tell, at least.)

I'm looking at the gameTitle passage, though, as a possible solution, seeing as it runs everytime the page is first loaded. Would this be a good idea, or is there a better place for it?

Thanks. :)

Update:

I've done some testing, and it looks like 'Set it and forget it' variables can be placed in the gameTitle passage without any issue. Hoever, anything that needs to be run routinely, or has special values, needs to go into StoryInit.

Example - I have a simple array for the names of days, which I had moved to gameTitle. That wound up breaking my side panel. Once I moved the array back to storyInit, it was fixed.
 
Last edited:

guest1492

Member
Apr 28, 2018
350
288
Actually, StoryInit only runs on start-up, not when saved games are loaded.* For example, maybe you have a variable $day that you set to 1 in StoryInit. If a player loads a save from day 15, the variable $day does not suddenly get set back to 1.

In Twine, you do not have to declare variables before you use them (though it will throw an error if you try to access information stored by a non-existent variable). Therefore it's not necessary to find somewhere to initialize them before you use them. However, if you want to keep old saves up to date then you should look into .

Here's an example of what you can do:
You don't have permission to view the spoiler content. Log in or register now.

* I realize that the seems to say differently, but it's either wrong or worded incredibly badly. Yes, the StoryInit passage is run every time the browser loads the html file (so when you first open the file or when you somehow refresh the browser). However, loading saves does not refresh the browser, which means it does not reload the html file. Here's a slight modification of the sample code in the documentation:
You don't have permission to view the spoiler content. Log in or register now.
Yes, the value for setup.y is set when you load up the game and it's still there when you load a save because it's stored separately from the regular variables. However, if StoryInit really did run whenever a save is loaded, then setup.y would change every time you load a save but that is not the case. Also, data stored in the setup object does not persist between sessions unless you them.
 
Last edited: