Init data and such

Hello there.

Im a new user, background mostly in OOP developement. So i think too much how to optimize it instead of making it just work.

Everything here is just wierd for me.

However, i love this stuff. Makes my life easier and gives time and efforts for the important thing, story telling.

Im used to set up a init file, where i declare all variables and so. Now im bit confused. What is the best way to initialise all necessary variables etc. in this case?

External file, or add them to part of first scene of story?

Im using the online version of Twine.

Thanks, EK

If you’re using harlowe, you make an init passage (the equivalent of an init file) and tag it with “startup”, and it will be run once upon initialization. You can have multiple passages tagged startup, and they will be run in alphabetical order.

1 Like

Ok, so its simple as that.

Thanks, EK

There are different story formats within Twine, so the answer depends on what story format you’re using.

Harlowe is, unfortunately, the default story format. However, I’d strongly recommend using the SugarCube story format instead. If you want to know the specifics of why, see the Reddit thread “2020, which do you prefer more - Harlowe vs Sugarcube?”.

If you’re using SugarCube you’d normally initialize variables within the StoryInit special passage. However, you can also do some initialization within the JavaScript section, any “widget” tagged passages, or when triggered by the :storyready event. See the “Refreshing and Restarting” section of the SugarCube documentation for details.

That said, I’d strongly urge you to use temporary variables (the ones that start with an “_”) whenever possible, and only use story variables (the ones that start with a “$”) as needed, and <<unset>> any story variables when you’re done with them. The reason why is that, unlike temporary variables, story variables are stored in the game’s history, and that bloating up the game’s history slows down saves, loads, and passage transitions. Thus, for optimal gameplay speeds, it’s best to minimize the use of story variables as much as possible.

Hope that helps! :grinning:

Hi HIEv and thanks for this extra information.

I use datamaps, since i store characters in them. Characters haves properties and using datamaps allowed me to create a single template page where i can show information about character no matter which one it is.

(set: $charName to (datamap:
	"name", "abc",
	"origin", "def",
	"description", ".jkl",
	"friendship", 0

Now i can use that as datamap template what i can use in a single page to show information about any character on my game.

''Name:'' (print: $tpl_character's name).
''Origin:'' (print: $tpl_character's origin).
''Charasteristics: '' (print: $tpl_character's charasteristic).

''Description:'' (print: $tpl_character's description)


As far i have had time to study Twine, this is best way i have found.

Datamaps are just generic javascript objects (AFAIK).

To do the exact same thing in sugarcube, you’d do this:

<<set $charName to {
	"name": "abc",
	"origin": "def",
	"charasteristic": "ghi",
	"description": ".jkl",
	"friendship": 0

Name: <<print $>>
Origin: <<print $charName.origin>>

You can also make it JS-ier by using <<set $x = 1>> instead of to, and <<= $value_to_print>> instead of print.

Yeah, looks cool to me. Shame i have allready written so much, i dont want to restart anymore. But i will definately remember your advices for next project.

Thanks for you help.

  • EK