Howdy, Stranger!

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

Condensing options, hidden options, and for each loop

Three questions I couldn't answer by looking through the wiki, or the discussions already posted.

1) and 2) How can I create something that outputs this:
The baker waits patiently

show pies

click show pies...
The baker waits patiently

Apple Pie, Blueberry Pie, Lemon Cream Pie, hide pies

If the player was later told to ask the baker for a snozzberry pie...
The baker waits patiently

Apple Pie, Blueberry Pie, Lemon Cream Pie, Snozzberry Pie, hide pies

3) My game has simulation elements, and so the player can only do so much in a given day I could do something like
<<if $task 1 is 1>>\
    <<display "job1">>\
<<if $task 1 is 2>>\
    <<display "job2">>\

/*...*/

<<if $task 2 is 1>>\
    <<display "job1">>\
<<if $task 2 is 2>>\
    <<display "job2">>\

Which would get rather tedious and hard to maintain if the player can do up to 5 tasks in a day.

I'm unsure of how lists work in twine. in c++ I would say
int task[5] = [1,2,1,0,0]

for (int t =0, t<5, t++)
{
 if (task[t] =1)
    {
     //do job1
    }
if (task[t] =2)
    {
     //do job2
    }
}

Is there some way I can replicate that in Twine?

Comments

  • edited March 2016
    You need to state which Story Format you are using when asking a questions, as answers can be different each one.

    Based on the syntax of your example for point 3 I am going to assume you are using SugarCube 1.x, which has excellent documentation and I suggest you may want to read the section on macros.

    note: I have added indents, extra line-breaks and line-break suppressions to the following examples to make them more readable, these can be safely removed.

    RE: point 1 & 2
    In your examples you don't state if the items shown in the pie list or "hide pies" item are markup links or not, so I am going to consider them plain text.

    The following example uses an id'ed span element and the <<replace>> macro to display the list of pies both before and after an extra pie is added to the list.
    <<set $pies to ["Apple Pie", "Blueberry Pie", "Lemon Cream Pie"]>>\
    
    The baker waits patiently
    
    <span id="pie-list">\
    	<<click "show pies">>\
    		<<replace "#pie-list">>\
    			<<print $pies>>, hide pies\
    		<</replace>>\
    	<</click>>\
    </span>\
    
    
    <span id="other-click">
    <<click "click here after you have clicked on the show pies link to add a Snozzberry Pie to the list">>
    	<<set $pies.push("Snozzberry Pie")>>\
    	<<replace "#pie-list">>\
    		<<print $pies>>, hide pies\
    	<</replace>>
    	<<replace "#other-click">><</replace>>\
    <</click>>
    </span>
    

    RE: 3
    The following example is a direct translation of your example using SugarCube's
    <<for>> macro and <<if>> macro
    <<set $task to [1,2,1,0,0]>>\
    <<for $t to 0, $len to $task.length; $t lt $len; $t++>>\
    	<<if $task[$t] is 1>>\
    		/% do job1 %/
    	<<elseif $task[$t] is 2>>\
    		/% do job2 %/
    	<</if>>\
    <</for>>\
    
  • Hey, thanks for the advice. I'll take look at the macros, I'm slowly becoming aware that macros are quite important to sugarcube...

    I may need to ask some more questions after I read it, but that link to sugarcube documentation does look quite useful indeed. Sorry if I look like a complete noob, I'm only a rookie when it comes to twine, honest! =p
  • Nester64 wrote: »
    I'm slowly becoming aware that macros are quite important to sugarcube...
    Macros are important for both SugarCube and Harlowe, they are how you do most things besides showing text and markup links.

    Remember to state your Story Format (name and version) when you create new Question threads for your other questions.
  • edited March 2016
    actually, I do have questions. Let's say I want the player to be able to click on a pie, deduct its price from their wallet, and add the pie to their inventory. I don't suppose simply replacing the text with

    << click "Apple Pie">>\
        <<set $wallet = $wallet - $piePrice>>
        <<$inventory.push("Apple Pie:)>>
        <<print "You pay the man and pocket the pie">>
    

    would work at all. Unless there's a way I can push all that into a string and get it to unpack at runtime?
  • There are a number of errors in your example: invalid space before click macro name; a colon instead of a double quote; a missing <</click>>; printing text in a click macro's body. You also referencing variables ($wallet, $piePrice, and without $inventory defining them.
    A working copy of your example
    <<set $wallet to 100>>\
    <<set $piePrice to 25>>\
    <<set $inventory to []>>\
    
    <span id="output"></span>
    
    <<click "Apple Pie">>\
    	<<set $wallet = $wallet - $piePrice>>
        <<set $inventory.push("Apple Pie")>>\
    	<<replace "#output">>You pay the man and pocket the pie<</replace>>
    <</click>>
    

    There are a number of possible solutions to your question, one of them is to use a <<widget>> macro to create your own custom macro.

    Place the following into a new passage with a widget tag and a nobr tag, it creates a buyable macro:
    // usage: <<buyable itemName price>>
    // Assumes:
    //		1. that the $wallet and $inventory variables have been defined.
    //		2. that an element with an id of 'output' exists.
    //
    <<widget "buyable">>
    <<if $args.length is 2>>
    	<<click $args[0]>>
    		<<set $wallet = $wallet - $args[1]>>
    		<<set $inventory.push($args[0])>>
    		<<replace "#output">>You pay the man and pocket the pie<</replace>>
    	<</click>>
    <</if>>
    <</widget>>
    

    You can now use the buyable macro like so:
    <<set $wallet to 100>>\
    <<set $inventory to []>>\
    <<set $pies to ["Apple Pie", "Blueberry Pie", "Lemon Cream Pie"]>>
    
    <span id="output"></span>
    
    <<for $t to 0, $len to $pies.length; $t lt $len; $t++>>\
    	<<buyable $pies[$t] 25>><<if $t lt $len - 1>>, <</if>>
    <</for>>\
    
  • Well, for starters, thank you for pointing out my errors in the example I offered. I knew that I would have to define those variables at some point, and assumed that they were so elsewhere in my example, but your thoroughness is extremely appreciated. I'll be sure to use that in my project!
Sign In or Register to comment.