Using Temporary Variables & Parameters To Share Info Between Knots

This is a tutorial to help authors understand how they can share information between Knots in their Literotica Interactive Stories using Temporary Variables and Parameters. To get the most out of this tutorial, you should already be familiar with Choices, Conditionals, and Variables.

What are Temporary Variables?

Variables are words (strength) or phrases (hit_points) that contain values. Variables can be used to make Interactive Stories more dynamic, as discussed in our Introduction To Variables Tutorial. The previous article mostly covered “Global Variables”, those which are declared at the start of a story and remain available to be accessed and updated anywhere until the story ends.

If a “Global Variable” is one that can be accessed everywhere (globally), then a simple definition of a “Temporary Variable” is one that is local, or available in the place where it was created. “Temporary Variable” makes it sound like your Variable may disappear when you least expect it, but “Temporary” just means that it’s not created at the start of the story and does not stay with you until the end. Utilizing the power of Parameters, you can control when Temporary Variables appear and disappear, making them a superior alternative to Global Variables for many uses.

Why Would I Use a Temporary Variable Instead of a Global Variable?

Global Variables require careful management throughout your story. After declaring Global Variables at the start of a story, authors need to remain aware of the current value of each Global Variable at every point in the story. Global Variables are great for keeping track of important elements like statistics, inventory, characters, and other information that will be needed repeatedly throughout your story. But managing too many Global Variables can become overwhelming.

Temporary Variables can be created anywhere you need them, be used for just as long as you need them, and then forgotten about once you no longer need them.

How do I create a Temporary Variable?

Temporary Variables are easy to create at the top of any Knot.

=== knot_name ===
~temp variable_name = "text string"

Here’s a real example:

=== arrive_at_beach ===
~temp swimsuit = "{~bikinis|thongs|shorts|one-pieces}"
You arrive at the beach. What a beautiful view.
In addition to the water, you notice several women in {swimsuit}!

In the above example, we create the Temporary Variable “swimsuit” using a tilde symbol followed by the word “temp” followed by the variable name “swimsuit” followed by an equals sign “~temp swimsuit =” and then the value we want the Temporary Variable to contain (in this case, a single text string from a shuffled list of text strings).

Once the Temporary Variable is declared, it can be accessed and used inside of this one Knot only. If you try to access a Temporary Variable outside of the Knot where it was created, it won’t work. That doesn’t mean you can’t pass the value of Temporary Variables to other Knots, though. There’s a simple way to send any information you need from one Knot to another using Temporary Variables, Choices/Diverts, and Knot Parameters.

What are Parameters?

Parameters are a list of values that can be sent from any Divert to any Knot using named placeholders in the receiving Knot’s header. That might sound complicated, but you might think of Parameters as Temporary Variables that can be shared between Knots. Here’s a simple example of what a list of Parameters looks like in the header of a Knot:

=== look_around_at_beach(direction, swimsuit) ===

The Parameters in the above example are “direction” and “swimsuit”. They’re listed in the Knot header, after the Knot name, inside of parenthesis, separated by commas:

knot_name(parameter_1, parameter_2, parameter_3, parameter_4)

If you have experience in computer programming, you’ll recognize this pattern as the one used for passing Parameters to Functions. For those who’ve never seen this method of sharing data before, we’ll go through it step by step with several working examples.

How do I use Parameters to Share Information Between Knots?

Parameters work exactly the same as Temporary Variables. In our previous example:

=== look_around_at_beach(direction, swimsuit) ===

You can use “direction” and “swimsuit” just as you would any other Variable inside of the “look_around_at_beach” Knot. Here’s one way they could be used:

=== look_around_at_beach(direction, swimsuit) ===
You look {direction} and see people wearing {swimsuit}.
-> END

A Parameter can be used just like using any other Temporary Variable. But where does the value for the Parameters “direction” and “swimsuit” come from? Parameter values come from Diverts, which are usually (but not always) attached to Choices. Here’s an example:

+ Look right
-> look_around_at_beach("right","bikini")

The above is a Choice - with text “Look right” and a Divert “-> look_around_at_beach” - as you’ve seen many times before. What’s new is that we’ve added two Parameters to the Divert “("right","bikini")”.

When you attach Parameters to a Divert, those Parameters get passed to the Knot named in the Divert. The value of the Parameters in the receiving Knot is determined by the value of the Parameters in the sending Knot’s Divert.

Here’s a full story example:

On a sunny day like today, you want to go swimming.
+ Go to the beach
-> arrive_at_beach

=== arrive_at_beach ===
~temp swimsuit = "{~bikinis|thongs|shorts|one-pieces}"
You arrive at the beach. What a beautiful view.
In addition to the water, you notice several women in {swimsuit}!
What do you want to do next?
+ Go Swimming
-> swimming
+ Look left
-> look_around_at_beach("left",swimsuit)
+ Look right
-> look_around_at_beach("right",swimsuit)

=== swimming ===
You have a nice swim{look_around_at_beach.saw_many_women_on_beach: while thinking about all of the pretty people on the beach| while thinking about all of the pretty fishes in the sea}.
-> END

=== look_around_at_beach(direction, swimsuit_type) ===
{direction == "left": You look to the left and see beautiful women in tiny {swimsuit_type} everywhere.}
{direction == "right": You look to the right and see so many beautiful women in {swimsuit_type} everywhere.}
This is the life!
+ (saw_many_women_on_beach) Probably a good time for a swim to cool off!
-> swimming

In this story, we create a Temporary Variable called “swimsuit” in the “arrive_at_beach” Knot. The value of “swimsuit” is randomly selected (shuffled) by the script from our list of text strings “{~bikinis|thongs|shorts|one-pieces}”. The author of the story knows that the value of “swimsuit” Temporary Variable will be one of those four values, but the specific one selected is unknown to the author.

Since the author doesn’t know what type of “swimsuit” the people on the beach are wearing, and we want to write about the swimsuits in the “look_around_at_beach” Knot, we need a way to share the value of the Temporary Variable “swimsuit” between the Knot where it was created and the Knot where we want to use it. The information on what type of swimsuits people on the beach are wearing will only be used in two Knots in the entire story, and it’s not data important to the central narrative, so it’s not a good candidate for a Global Variable. This is a case for Parameters.

We have two Choices with Diverts connecting the “arrive_at_beach” Knot to the “look_around_at_beach” Knot. We can simply add the “swimsuit” Temporary Variable to the Diverts, add a Parameter to the receiving Knot, and continue to use our Temporary Variable in the second Knot.

Here’s the code where we transfer the “swimsuit” value from the “arrive_at_beach” Knot to the “swimsuit_type” Parameter in the “look_around_at_beach” Knot:

+ Look left
-> look_around_at_beach("left",swimsuit)
+ Look right
-> look_around_at_beach("right",swimsuit)

=== look_around_at_beach(direction, swimsuit_type) ===

We add the Parameters we want to send from the sending Knot to the Divert:

-> receiving_knot_name(parameter_1, parameter_2, parameter_3, parameter_4)

Then we add the Parameters, in the exact same order and with the same code formatting, to the receiving Knot header:

=== receiving_knot_name(parameter_1, parameter_2, parameter_3, parameter_4) ===

It’s important to understand that the values are being passed based on the order of the comma separated items in the list, and not based on the Parameter names. You can use any valid Variable name you want for the Parameter names. Here’s another example:

Sending Knot:
-> receiving_knot_name(variable_a, variable_b, variable_c, variable_d)

Receiving Knot:
=== receiving_knot_name(new_variable_name_1, new_variable_name_2, new_variable_name_3, new_variable_name_4) ===

In the above example, the value of the Parameters in “receiving_knot_name” will be:

new_variable_name_1 = variable_a
new_variable_name_2 = variable_b
new_variable_name_3 = variable_c
new_variable_name_4 = variable_d

Parameters are sent to the receiving Knot in the exact order of the sending Divert’s comma separated Parameters list.

Parameters can be chained from Knot to Knot to keep a Temporary Variable’s value alive in as many Knots as you need.

=== knot_name_1 ===
~ temp variable_a = "variable a value here"
-> knot_name_2(variable_a)

=== knot_name_2(parameter_a) ===
-> knot_name_3(parameter_a)

=== knot_name_3(parameter_b) ===
-> knot_name_4(parameter_b)

=== knot_name_4(parameter_c) ===
-> knot_name_5(parameter_c)

=== knot_name_5(variable_a) ===
{variable_a}
-> END

The value of the Temporary Variable “variable_a” that we created in “knot_name_1” is available for use in every Knot in the above example (although the Parameter name changes), and the original value “variable a value here” is outputted in “knot_name_5”.

You could theoretically make a Temporary Variable available in every Knot throughout an entire story by simply using Parameters to send its value to one Knot after another. In cases like that where you want to use a Variable in every Knot, using a Global Variable is the best option, but Parameters offer another powerful tool to help you keep track information throughout your Interactive Story.

Additional Notes on Parameters

You do not need to create a Temporary Variable to use a Parameter. Any value you add to a Divert can be sent to a receiving Knot as a Parameter. This code is valid:

Start text.
-> knot_name_1

=== knot_name_1 ===
This is a simple text Knot with no Variables.
+ Go to Knot 2
-> knot_name_2("Some Random Value", "Another Random Value")

=== knot_name_2(parameter_1, parameter_2) ===
{parameter_1} {parameter_2}
-> END

You do not need to use Choices with Parameters. Diverts are usually attached to Choices, but there are times when a Divert on its own may be preferable. You can use a Divert with Parameters on its own like this:

Start text.
-> knot_name_1

=== knot_name_1 ===
This is a simple text Knot with no Choices.
-> knot_name_2("Some Random Value", "Another Random Value")

=== knot_name_2(parameter_1, parameter_2) ===
{parameter_1} {parameter_2}
-> END

Like other Variables, Parameters can be text strings (“text here”), boolean (true/false), numbers (400), or Variables (variable_name):

-> knot_name_2("Some Random Value", "Another Random Value")
-> knot_name_2(false, true, false, true)
-> knot_name_2(4, 29, 25, 40)
-> knot_name_2(variable_1, variable_2)

One other thing to keep in mind is that Parameter names, as with Temporary Variable names and Knot names, cannot be identical to any of your Global Variable names. Global Variable names are reserved story-wide after being declared at the start of a story.

Parameters are a de facto type of Temporary Variable that simplify sharing data between Knots without having to worry about management issues that come with using Global Variables. Once you get started using Parameters in your stories, their utility will quickly become apparent.

Further Reading on Temporary Variables and Parameters

The creators of the Ink language have written a detailed help document explaining how to use every advanced feature of the language. If you’re interested in becoming a power user, please see their “Writing With Ink” tutorial for more information. We also have our own growing library of Interactive Story Writing Tutorials and FAQs that you might want to read.

If you’re interested in helping test and give feedback on the new Literotica Interactive Story format, either as an author or a reader, please read this thread in the Literotica Forum: Interactive Adult Story Testers Needed.