I’m not exactly a programming genius, so forgive if my questions are ill informed…
I am starting to concept out a text based adventure game, being inspired by ones like Master of Flesh and Forest & Shack. I’d rather use an engine like Twine 2 for such a project. After getting started, I’m anticipating a few issues as things get more involved.
Does anyone know if it’s possible to create a function every page runs? Ideally something I would build in another page so it doesn’t clunk down the existing page. Something that would read the variables (weight, hunger) and determine if something has happened (fat1 is false and weight > 180) [display fat1 growth text, set fat1 to true].
What I’m envisioning is something that I can simply call on at the end of every page to check variables and display flavor text, when conditions are met. I’d rather not build this big function I have to paste on every single page. Thoughts?
Moved to the Project Help/Game Engines category as this is a question about the twine engine and not general game design.
Specific solutions will depend a bit on which format you end up using within Twine 2. I have some experience with the Harlow story format.
You can find the full documentation here: https://twine2.neocities.org
Of particular note would be the “display” macro, which lets you display the contents of a specified passage on demand. This can be used like a primitive function call (but without any real “return” values. It’s a bit clunky in that you’re still likely to use global variables), but can be called/displayed at end of your page as you mentioned.
(display: "Cellar") prints the contents of the passage named “Cellar”.
There are also special “header” and “footer” tags that can be applied to special-purpose passages that you want to run with every other passage in your story. This is useful for a blanket set of macros to be run each time.
Considering this is the main Twine 2 question topic, I am getting confused on how (or where) to apply variables in the game. I understand the difference of values that exist everywhere (those with $) while there are values that are temp. (those with _) but I don’t know where to place the $ value in the story (could be anywhere or at the beginning or something) and be able to tell the system when a change has occurred in the entire story by changing the same value.
I understand the temp value is for the passage it appears in only but still I don’t know how to apply as well to at least work.
Sorry ahead of time, I never had the time to do coding so even something simple is kind of making it hard for me to use it even though I understand the limits of it. ( I am looking at the cookbook and the Harlowe home page but still not finding what I am looking for).
The each $ variable is created in the first passage encountered that it has a value assigned to it.
This can lead to trouble if you are trying to test a $ variable before the passage that sets it up has been encountered. A useful defensive technique is to set up all the variables you need right at the start of the game. You can do this in the first passage (not ideal), or better still in a separate initialisation passage/script. It ensures everything is initialised, and also gives you a handy list of the variable names you are using to refer back to.
All the $ variables are part of the “story” and their values are stored in the history, and in saves. If you are planning to have very many variables and a deep history this can become a problem as the save files can get quite large.
Alright, now how do I apply a value to a certain object if the player does a certain action to it. An example could be the player eats a hot dog and it adds 2 to their value while not eating changes nothing. I do have an idea on how to set up the possible outcome depending on the choice but again I don’t know. Also how can I do an action to ensure it does happen, I know the link reveal thing though I don’t know if it is what I need.
Sorry again, I’m trying to use some of the functions but there are a few that are still vague in how they fully operate.
The twine stuff I do is mainly in Sugarcube, so I’m not an expert art Harlowe, but…
The simplest way is to do this is to have a separate passage to describe eating the hot dog. The basic contents of this (or indeed any new passage) would be:
- Update the variables for this choice
- The descriptive text
- A list of choices for what to do next
You can mix the choices up in the descriptive text, but the expectation of readers is usually to find them at the end, as they would in a print book.
In the passage where eating the hot dog was a choice, then you simply offer a link to this eating-hotdog passage.
You can do fancy things with link reveal, but there are some gotchas around variable handling in Twine due to the way it works. In very broad strokes the passage is “evaluated” including all the conditionals and substitutions to generate HTML markup, and then this markup is shown to the player. It doesn’t evaluate again until the player navigates to a new passage. Reveal and its kin are just messing with the visibility of already generated HTML elements in the markup. I don’t think you can’t (easily) conditionally change a variable based on whether an element has been made visible or invisible. (I’m welcome to be corrected on this!)
This is why it mostly makes sense to put all the assignments at the start of the passage. You certainly don’t need to do something like:
- Ask vendor for hot dog
- Pay vendor
- Take hot dog
- Eat hot dog
- offer next options
- Describe everything that happens.
- offer options
Suppose one of the options is to buy another hot dog and eat it as well, but you might no longer have enough money, or be too full to consume it. You might be tempted to handle all three in the same eating hot dog passage, putting conditionals around the chunks of assignments and text. That would work, but it’s often easier in Twine to break it into separate passages for each case (as having long rambling conditionals often leads to human error, either with an unclosed condition or messing up which options to offer next), so create:
hotdog-eating (the normal case, described above),
hotdog-no-money (that doesn’t change any variables, but the only option offered is to return home), and
hotdog-full (where you buy the hot dog, but can’t eat it, and throw it away).
You can then create a
hotdog passage that tests the current money and fullness variables and uses goto to take the player to the appropriate one of these first three. It doesn’t generate any text itself. There are other options than goto here (like display, and Harlowe specific things), but let’s keep it simple.
I hope that’s kind of clear.
Yes it does make sense, thank you!
Is there a way to have a system where a player can type in a code to traverse a portion of the story or is it impossible?