Howdy, Stranger!

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

Extreme beginner tips and hints

edited March 2014 in Chit-Chat
I draw for a living, so this whole coding-thing is extremely new for me. For that reason, I've started compiling a concise list of handy commands I use a lot in Twine and which were far from immediately apparent to me when I started. For the sake of other noobtards like me, I'll be sharing those extreme beginner tips here, and urge you others to do the same if you have anything you want to add (corrections, improvements and optimizations for my own stuff is extremely welcome as well). I'll be adding to the list as I discover new things and trying to keep it up to date if people add something awesome.

To add to a Variable: <<set $variable +=20>>

To subtract from a Variable: <<set $variable -=20>>

Putting several Variables in one String: <<if $variable gte 1 or $variable lte 7>>

Checking if a Passage has been visited previously: <<if visited ("passage title")>>

Make the User type in a Variable name: <<set $variable to prompt("query","default input")>> example: <<set $name to prompt("What's your name?","Alyssa P. Hacker")>>

Change a Variable through a Link: [[link name|passage title][$variable = expression]] example: [[Return|3_cliffsStart][$mana +=1]]
Change several Variables through a Link: [[link name|passage title][$variable = expression]] example: [[Return|3_cliffsStart][$mana +=1;$ammo +=1]]

Print global turncount: <<turns()>>

Generate a random number: <<set $variable = (Math.floor(Math.random()*range))+1>> example: <<set $diceRollOne = (Math.floor(Math.random()*5))+1>>

Comments

  • [quote]
    Print global turncount: <<turns()>>


    You'd do that a bit differently in SugarCube:

    "Turns" (including the Start passage): <<print state.length>>
    "Turns" (excluding the Start passage): <<print state.length - 1>>

    [quote]
    Generate a random number: <<set $variable = (Math.floor(Math.random()*range))+1>> example: <<set $diceRollOne = (Math.floor(Math.random()*5))+1>>


    While that works in SugarCube, there's a better way:

    // For integers (whole numbers)
    random(max)
    random(min, max)

    // For floating-point (real numbers)
    randomFloat(max)
    randomFloat(min, max)
    For example:

    /% Generate a pseudo-random number from 0 to 5 %/
    <<set $randomNum = random(5)>>

    /% Generate a pseudo-random number from 1 to 20 %/
    <<set $randomNum = random(1, 20)>>

    /% Simulate the roll of 3d6 %/
    <<set $diceRoll = random(1, 6) + random(1, 6) + random(1, 6)>>
  • For a six sided dice, either(1,2,3,4,5,6) should work - and is somewhat clearer than a lot of Math functions.
  • You can <<print>> and <<display>> in a more concise fashion:

    <<passageName>> Displays a passage.
    <<$variableName>> Prints a variable.


    More about Twine's Functions:

    http://twinery.org/wiki/function


    Here is the official Twine FAQ, mandatory reading for all newbies:

    http://twinery.org/wiki/frequently_asked_questions
  • Good stuff, guys!

    @MadExile: Been hearing Sugarcube mentioned all over, but have yet to try it for myself. I've been assuming it is another Story Format like Jonah or Responsive, but more powerful, and figured I'd start out by sticking to the simple starter Formats included with Twine. Currently using Sugarcane with a custom CSS on top of it. Am I being completely stupid by not using Sugarcube?
  • Bluntie wrote:

    Currently using Sugarcane with a custom CSS on top of it. Am I being completely stupid by not using Sugarcube?


    If you're looking for an unbiased answer, then I'm not really the person to ask (you'd be better off asking SugarCube users).  That said, let me try to give you a fair answer.

    If you were using one of the old vanilla headers, then I'd say yes, no questions asked.  Since you're using one of the new vanilla headers, however, the answer is more complicated.

    If you definitely plan on using SugarCube at some point because it has some feature(s) you want, then I'd probably have to say yes, you're not doing yourself any favors by waiting.  In fact, you're probably hurting yourself, since SugarCube and the new vanilla headers are not 100% compatible.

    On the other hand, if SugarCube has no must have  feature(s) for you, then there's no pressing need to switch, the new vanilla headers are serviceable enough.
  • I think the killer feature of SugarCube is the save system. That is something that the vanilla headers don't have and it would be a lot of work to build it yourself in a way that works in all browsers. So if that is something you'd like to have in your story, it would certainly be a good idea to give SugarCube a try.

    Another thing I like about SugarCube is that it has good documentation.
Sign In or Register to comment.