Text from one passage to another

Harlowe 3.3.7

I am wondering about something odd that i can’t quite figure out. I am trying to store text within one passage and have it be read in another passage. An example of what i am trying to do.

{
(set: $Dmgtext to (random:1,3))

(if: $Dmgtext is 1)[
A Simple Hit
]

(if: $Dmgtext is 2)[
A Typical Strike
]

(if: $Dmgtext is 3)[
A Rather Weak Attack
]
}

I am wondering if i can add that as an example into one passage and then display the text in another passage so i don’t end up filling up a passage i might be using.

then i can have lots of random dialogue and possible results and not fill a passage i might be using for other passage links and other bits of code.

any examples, advice or guides would be appreciated. i also am not sure how to use datamaps IF they are required for this to work of course. I appreciate the help as always!

I don’t understand your problem. You’re basically setting your variable in one passage and calling it the next one, it seems very straightforward.
What do I miss?

@aaa425

It sounds as if you wish to use passages as snippets of code to reuse and keep your story/game more organized. I believe you simply want to include a passage, process its logic, but not navigate to it. The (display:) macro will achieve this.

https://twine2.neocities.org/#macro_display

That way, you can make a change to the logic in one passage and it will change across the entire story. You can have nested passage (display:) macros so one passage might include any number of other passages and those (display:) macros can be inside (if:) macros for conditional passages.

Taking that concept further, you might want to investigate Harlowe’s storylet feature. From my understanding, it works by turning passages into a deck of cards that can be shuffled, arranged, selected in almost any manner. It’s way more complicated, but more powerful.

Personally, if you are a novice programmer, I would do the (display:) macro route and see how far that takes you. Good luck!

1 Like

Additional to HAL9000’s suggesting to use (display:) to include the content of “child” Passages…

In situations where you need to use one of the (if:) family of macros to check if a single variable contains one of a range of variables, the 2nd and later such checks in the series should be (else-if:) macros. And if all the possible values in the range are checked for then the last such check in the series could be an (else:) macro instead.

The reason (if:) should not be used for all of the checks in a series is because once one of the series of checks evaluates to true the later checks logically will be false, so there is no need to preform those later checks. And (if:) macros are always evaluate, where as an (else-if:) macro is only evaluated if no condition before it in the series was true.

eg. In your example the value of $Dmgtext can only be one of three possible values (1 or 2 or 3), so you should use (else-if:) for the 2nd and later checks…

{
    (set: $Dmgtext to (random: 1,3))
    
    (if: $Dmgtext is 1)[
        A Simple Hit
    ]
    (else-if: $Dmgtext is 2)[
        A Typical Strike
    ]
    (else-if: $Dmgtext is 3)[
        A Rather Weak Attack
    ]
}

…and because all possible values in the range are being checked for in the series of checks, it is possible to replace the last (else-if:) macro call with an (else:) macro…

{
    (set: $Dmgtext to (random: 1,3))
    
    (if: $Dmgtext is 1)[
        A Simple Hit
    ]
    (else-if: $Dmgtext is 2)[
        A Typical Strike
    ]
    (else:)[
        A Rather Weak Attack
    ]
}
1 Like