Howdy, Stranger!

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

Creating and calling functions / methods under Harlowe syntax

Hi everyone!  First off I apologize if this is an incredibly basic question, I am just looking into Twine now (starting off with 2.0, yay), played through the youtube tutorials, read a bit through the Harlowe page, tried searching through this forum... and I seem to be missing something really basic?

So, I see you can set variables easy enough. 
(set: $player_height to 8)
And you can use variables easy enough. 
$player_height
And you can put if statements easy enough
(if: $player_height < 10)[(set: $player_height to it + 1)]
That's awesome, straightforward, slightly different from normal programming syntax in other languages but fine - nothing overwhelming.

How do you create functions though? 

For example, if you want to increase that player height without typing out that logic all over the place (and obviously other things may have even more complex logic and effects)?  Something more something like this:
Passage:PlayerAdjustment
IncreasePlayerHeight($adjustment)
{
(if: $player_height + $adjustment < 10)
[(set: $player_height to it + $adjustment)]
(if: $player_height > 7)
[(set: $player_height_desc to "tall")]
(else if: $player_height > 5)
[(set: $player_height_desc to "average")]
(else:)
[(set: $player_height_desc to "short")]
}

Passage:Start
[(set: $player_height to 7)]
[(set: $player_height_desc to "average")]
The character is $player_height_desc
Do you want the player's height to grow? Click this hook: [singleHook]<cl1|.
(click: ?cl1)[$IncreasePlayerHeight(1); The character is now $player_height_desc]
Yea, no idea how that would look getting called in / tied to a 'hook' (ie button) either..

Is there a syntax thing that I'm overlooking that does this, or is there a work-around that does it, or is this something beyond the capability / scope of Twine?

Thanks a lot!

Comments

  • Each of the story formats has different functionality and some are currently easier for an Author to extend than others, for instance SugarCube allows an Author to create their own macros via either it's widget macro or macro API.

    Harlowe currently does not allow Authors to create their own macros but you can simulate this by using the (display:) macro and functional passages.

    Using your example as a base, the following would do what you want:
    (note: using TWEE notation, lines starting with two colons indicate a new passage, the passage's title is the text following the double colons)

    :: Start
    Initialize variables: {(set: $player_height to 8)(set: $player_height_desc to "unknown")}

    Player Details Before Call: $player_height - $player_height_desc

    Setup Parameter: adjustment: {
    (set: $adjustment to -3)
    $adjustment
    }

    Call Pseudo Function: (display: "IncreasePlayerHeight")

    Player Details After Call: $player_height - $player_height_desc


    :: IncreasePlayerHeight
    {
    (if: $player_height + $adjustment < 10)[(set: $player_height to it + $adjustment)]
    (if: $player_height > 7)[(set: $player_height_desc to "tall")]
    (elseif: $player_height > 5)[(set: $player_height_desc to "average")]
    (else:)[(set: $player_height_desc to "short")]
    }
    The IncreasePlayerHeight passage acts like a function, (display:)ing the passage is similar to calling the function, and assigning $adjustment a value is like passing a parameter.

    Harlowe does allow an Author to create new Javascript functions within the Edit Story Javascript editor, but currently using/calling these functions is very messy.
  • Thank you so much! that works perfectly, you are fantastic :)
Sign In or Register to comment.