Howdy, Stranger!

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

Twine 2, Harlow, how can I reduce repeated code?

I have included a very simple game skeleton, where items have to be found, collected and used.
Is there a way to assign the blocks of repeated code to a hook?
And other tips for doing things better, making the output neater, etc

Comments

  • 1. You can use the (display:) macro to include content within a passage. So if you created a new passage named something like Weapon Description and copied your gun/bullet related logic into it like so:
    {(text-color:red)[(if: $HaveGun is true)[You have a gun.]
    (if: $bulletCount is 1)[You have 1 bullet](elseif: $bulletCount > 0)[You have $bulletCount bullets]]}
    
    ... then you could replace every instance of that content with a (display: "Weapon Description") call. eg. your Living room passage:
    You are in the living room. You see doors to three other rooms, and the front door of the house. 
    You hear a horrible wailing. It seems to be coming from outside
    (display: "Weapon Description")
    
    What should you do?
    [[Go into the kitchen|Kitchen]]
    [[Go into the laundry|Laundry]]
    [[Go into the bathroom|Bathroom]]
    [[Open the front door|Open door]]
    

    2. I suggest moving the variable initialization from your first passage into a startup tagged passage (named something like Startup), any passage with that tag gets processed before your story's first passage. You will need to include the following CSS in your Story Stylesheet area to stop the startup tagged passage from incorrectly showing it's contents.
    tw-passage tw-include[type="startup"] {
    	display: none;
    }
    
  • Excellent. Thank you
  • edited November 2015
    It appear that selecting an answer may break spoilers, for those interested the code example for Living Room was:
    .....
    You hear a horrible wailing. It seems to be coming from outside
    (display: "Weapon Description")
    
    What should you do?
    ....
    
  • What's a spoiler?
    The (display:) works great, but I can't get the Startup to work (unless I make it the first passage)
    I've fleshed it out a bit too
Sign In or Register to comment.