Where to put my <<set>> variables

Twine Version: 2.3.16
Story Format: 2.36.1

This is a general question about the <> variables.

In my StoryInit, I have these types of varibales

<<set $Gold to 100>>
<<set $Bank to 50>>
<<set $Health to 100>>

and so forth.

I want to have many locations in my game that the player can travel to, and I want to set it up so that there are variables for:

  • whether the player has been there before or not,
  • whether or not they have fulfilled a quest,
  • whether or not their reputation affects their relationship with a person at a location etc.

I understand that the variables would be along the lines of

<<set $beenToShop to 'false'>>

…or words to that effect. Also, shops’ merchandise seems like it would be established this way as well.

<<set $wine to false>>

So that the wine shop would have:

<<if $wine is false>>[[A nice bottle of wine|wine]]

My question is: Do I need to put all of into these <> variables into the StoryInit, or can they go at the head of the appropriate passage, eg “The Shop”? It seems that if I can put new variables in specific passages, it might make it easier to check what has and hasn’t been set up yet. But I suspect that maybe StoryInit is the proper location for them.

I haven’t written any of these variables yet, as I am still figuring these out, but I’m sure it would be easier to do it properly from the beginning than have to change everything after I’ve messed it up.

Once again, thanks in advance for your time.

To know where players have been, you might wonsider the hasVisited() function ([SugarCube v2 Documentation]). It will tell if the player has already reached the passage, though it does not count a passage having been visited if you’ve used the <<include>> macro to , to insert the passage in another one.

If you plan to use <<include>> in this case you’ll like to use a specific variable indeed.

About where to declare variables, it will depend on how you code. For instance, you can use the def or ndef operator in a <<if>><</if>> macro. It’s quite powerful because it tests if the variable exists rather than testing if the variable is true. So if the variable is non-bolean it will still work. But in this case you mustn’t declare your variable in advance. Also you need in this case to <<unset>> the variable if no longer needed.
However, my advice is to declare all your story variables in StoryInit at first.When you’lle be more confident with Sugacube’s options you’ll might want to use more complex ways.

Regarding the Boolean like variables (eg. $beenToShop and $wine) mentioned in your examples…

1: Don’t use String values (like ‘false’) when a variable represents a Yes/No or On/Off like state, use the Boolean type values true and false.

<<set $beenToShop to false>>

2: Don’t use the is or eq comparison operators (or the mathematical equivalents === and ==) when evaluating a Boolean variable, doing so just adds an extra unnecessary step in that evaluation. Do the following instead…

/* Checking for true ... */
<<if $beenToShop>>You have been to the shop<</if>>

/* Checking for false ... */
<<if not $beenToShop>>You haven't been to the shop yet<</if>>
<<if ! $beenToShop>>You haven't been to the shop yet<</if>>

/* Checking for both true and false ... */
You <<if $beenToShop>>have<<else>>haven't<</if>> been to the shop.

Regarding inventory, both the shop’s and the player’s.

There are many methods you can use to implement ‘inventory’ in a project, which you use depends on on what functionally and complexity you want it to have.

1: You can use Boolean variables to represent each item that can be present at a “location”, or in the player’s “backpack”. eg. <<set $hasWine to false>>

2: You can use Array type variables to represent each “container” in your project (the shop’s “stock”, or the player’s “back pack”).

/* Pre-define the player's empty backpack, generally done in StoryInit */
<<set $backpack to []>>

/* Allow the player to buy the wine if they don't already have it... */
<<if not $backpack.includes("wine")>>
	<<link "Buy Wine">>
		<<run $backpack.push("wine")>>

see: Array Methods in the SugarCube documentation, or Array in JavaScript documentation at MDN, for information about the Array functions used above.