Best way to a randomize a passage that is linked to a template (using SugarCube)

Twine Version: 2.4.1
Story Format: SugarCube 2

I am looking to create a scenario where a person goes to a pet store and when looking at a pet a random pet is chosen. Next time they go to the pet store they can select that pet, after x interactions they have the ability to purchase the pet, after another set of x interactions they have the ability to buy an item for the pet. I need to then repeat this when they go back to the pet store for another pet to be randomly selected and follow the same path.

OR if there is a way when they select a specific pet like Brown Dog, they have to interact x times before they unlock another action, so on and so forth without me duplicating passages.

1 Like

Interaction counter. Here’s an example for creating a dog interaction counter.

In a passage titled StoryInit create a default value
<<set $dog_interactions to 0>>

In each interaction passage you can add
<<set $dog_interactions += 1>>

Or, if you want to only increase the counter the first time a player visits a passage
<<if visited() is 1>><<set $dog_interactions += 1>><<endif>>

Then on the passage where you want the player to buy the dog, after eg. five interactions, insert this code:
<<if $dog_interactions gte 5>> <<link "Buy Dog">> <<goto "You bought a dog">> <</link>> <<endif>>

The easiest way to repeat this would to be to create similar counters for other animals, and include all of the links on the same page in sequence.


Randomizer. If you want to randomize the animal that players see, there are various ways to do that. I assume you would want to animal to be randomly chosen as soon as the game begins without giving a player a choice.

Say you have three animals. In your StoryInit passage write

<<set $animal to random(0,2)>>

Then throughout your game, let’s say dog is 0, cat is 1, and fish is 2

<<if $animal eq 0>> Insert whatever dog text and code is necessary here <<endif>>
<<if $animal eq 1>> Insert whatever cat text and code is necessary here<<endif>>
<<if $animal eq 2>> Insert whatever fish text and code is necessary here <<endif>>

To combine it with the “Buy dog” link in the interaction example

<<if $animal eq 0>><<if $dog_interactions gte 5>> <<link "Buy Dog">> <<goto "You bought a dog">> <</link>> <<endif>><<endif>>

1 Like

This is perfect! Thank you!

Absolutely! I overlooked something last night, the last section of the code should use elseif

<<if $animal eq 0>> Insert whatever dog text and code is necessary here
<<elseif $animal eq 1>> Insert whatever cat text and code is necessary here
<<elseif $animal eq 2>> Insert whatever fish text and code is necessary here
<<endif>>

Ace, going to add this in when I get home. Do you know if it’s possible to display specific text in another Passage B depending on the amount of times a bit of text in Passage A is shown? So instead of counting passage visits you’re counting text block reveals?

Thanks!

You could create a counter similar to the dog_interaction counter in the earlier code.

If you insert <<set $counter += 1>> alongside any conditionally shown text, (ie. within an if statement) it will count upwards by one every time the text is shown.

Then you can use <<if $counter eq 3>>...<<endif>> elsewhere to show the text after the counter reaches 3.

Basically it is a matter of nesting conditions. You might want to look at using widgets if your code becomes repetitive.