Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Variables & Changing Options/Text

I'm creating a prototype game to learn Twine 2 (I learn better by doing) and I'm trying to get the hang of how to best use variables with it. I've got a few questions so far from a day or so playing around in it I'm hoping this awesome community can help me with:
1) An easy one, how do I keep a huge blank space from showing up where I initialize them?

2) Since I get use commas when initializing something like a monetary amount, is there a way when I print the value to have it displayed in a more readable way? ex : (set: $money to 100000) prints out 100000 which is kind of hard to read as $100,000

3) What is the easiest way to go about using the variables to determine what options or text show up? ex : If the player has enough money I want to show them the option of being able to buy their way out of a situation, if not I want that hidden and maybe only show them the option to try and fight their way out.

Also any pointers on places that discuss or go over things you can use variables for in Twine 2 would be amazing.

Comments

  • You need to state which Story Format (name and possible version) you are using when you ask a question, as answers can be different for each one. I will assume you are using Harlowe as it is the default one for Twine 2.

    1. I will assume you are initialising your story variables within a startup tagged passage, you can hide any output generated by that passage by place the following CSS within your story's Story Stylesheet area:
    tw-include[type="startup"] {
    	display: none;
    }
    


    2. Harlowe does not have a built-in macro for doing this.

    Your two basic choices are to either use Harlowe macros like (text:) and (substring:) to break apart your amount into each of the relevant parts so you can add currency related characters (like the separators and the unit symbol) where they are needed, or you can use Javascript code/features like Number.toLocaleString and Intl.NumberFormat but these features may not be available in older web-browsers.

    2a. Example using Number.toLocaleString:
    (set: $money to 5000000)
    
    money: (print: "$" + $money.toLocaleString('en-US'))
    

    2b. Example using Intl.NumberFormat, this example consists of two sub parts:

    i. The Javascript code used to define a custom Javascript global My namespace and a currency function you can use within your story, this code goes within your story's Story Javascript area.
    if (! window.My) {
    	window.My = {
    		currency: function(amount){
    			var f = new Intl.NumberFormat("en-US", {
    					style: "currency",
    					currency: "USD",
    					currencyDisplay: "symbol",
    					maximumFractionDigits: 0
    			});
    			return f.format(amount);
    		}
    	};
    }
    

    ii. The code used to display an amount as currency.
    (set: $money to 5000000)
    
    money: (print: My.currency($money))
    


    3. You can use an (if: ) macro cpmbined with an (else: ) macro to check the current value of a $variable and display different things depending on the true/false result.
    (if: $money > 5000)[[[Buy your way out->Next Passage]]]\
    (else:)[[[Fight your way out->Next Passage]]]
    
    note: The above is using a backslash character to remove the line-break character at the end of the (if:) macro, this results in the output of both macros appearing on the same line.
Sign In or Register to comment.