Howdy, Stranger!

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

Equivalent of the programming return; ?

Brand new to Twine, I'm starting to experiment a little bit with branching narration and the likes.
Being a programmer, I find the logic of Twine / Harlowe both very useful and very frustrating.

My issue is the following:
I have a bunch of (if:)/(else:), and I'm sure you all know these things can start to be a relative spaghetti-mess of code and lines if you need a numerous very specific cases in 1 single passage.
On top of that, I suddenly have a new condition, that if fullfilled, would simply skip over all the others.
The obvious way of doing that, which the little knowledge of Twine / Harlowe I have, would be to add that if/else encompassing all the others. That's okay, but what if I need to add encompass only a part of the others? What if my condition gets more complicated, and have to deal with some of the other if: before being processed (and then completely skip whatever is remaining to do).

As I said, I'm a programmer, so if I would have to do that while coding, I would usually put my new if at the very top (or at the relevant place), and then put a return; to end the function here and skip all the rest.

Is there an equivalent for that programming return; in Twine?
If not, why?

Comments

  • The simple answer is No, because where would it return to?

    Your analogy is based on a one line of code calling a method, said method checks a condition and instantly returns to caller, which results in the next line of code being executed.

    If transposing that analogy to traversing from one passage to another then where would the logical place to return to be:

    a. The start of the previous passage?
    This would result in the contents of that passage being processed and executed again.

    b. The line directly after the one containing the link that caused the traversal?
    As a passage does not have the equivalent of line numbers this would be difficult, it could also cause the previous passage's variables to end in an unstable state.

    You could instead use an (if:) macro combined with a (go-to:) macro to automatically forward to another passage:
    (if: $var is "value")[(go-to: "some passage")]
    
    Rest of passage's content......
    

    You can simulate a method call using a (display:) macro as demonstrated by the following code, it consists of two passages (Introduction and Calculate Age) and is written using Twee Notation.
    :: Introduction
    This is the Introduction passage.
    (display: "Calculate Age")
    Rest of passage's content......
    
    :: Calculate Age
    (if: $var is "value)[
    	(set: $age to 20)
    ] (else:) [
    	(set: $age to 25)
    ]
    
  • Actually what I was looking for and wondering if it would exist, is not about the result of what a classical return; would do, I don't want to return anywhere.
    But rather, using the analogy of a passage being a function, having the return; just stopping processing (and displaying) any lines that comes after it in that passage.
    Kind of like a "stop", just a command saying to the script "just stop here in this passage, do not display any other lines under here".

    That would allow to, instead of having stuff like:

    (if)[
    [Some content]
    ] (else) [
    [Some more content with a lot of ifs and whatnot]
    ]

    having something like this:

    (if)[
    (return) / (stop) / (whatever you want to name it)
    ]
    [Some more content with a lot of ifs and whatnot]

    This would have the tremendous effect of simplifying the logic flow, and thus the readability and maintenance of these kind of passages (when you have several conditions nested into each other, removing even one pair of [] can be very useful :) )

    But I guess that, coming from a programming background, my hopes in terms of logic construction and conditions were too high :)
  • As far as I know there is no way to stop the process that transposes the current passage's content into HTML elements and background Javascript, and any such mechanism would need to be smart enough to clean up after itself.

    eg. create any required end tags, produce any missing Javascript, etc...
Sign In or Register to comment.