"Modularity": SugarCube (v2.18)

Summary

In programming terminology, modularity refers to dividing software into different sections related to their purpose or to better organize the whole. In SugarCube, this technique can be used through the <<include>> macro to print the contents of one passage in another. Parts of a story can often be re-used in this way.

You can also create <<widget>> macros, which offer a simplified way of creating your own custom macros using other SugarCube macros and TwineScript instead of JavaScript. When compared to <<include>>, widgets have the advantage of accepting arguments and expressions similar to the way other SugarCube macros can. When you create widgets you need to place them in passages tagged widget. You can access the arguments of widgets through the special $args array.

Live Example

Download: Live Example

Twee Code

:: StoryTitle
Modularity in SugarCube

:: Start
<<set $lineOne to "Give us a verse">>
<<set $lineTwo to "Drop some knowledge">>

<<include "showLineOne">>
<<include "showLineTwo">>

<<showLine 1>>
<<showLine 2>>

:: showLineWidget [widget]
<<widget 'showLine'>>\
    <<nobr>>
        <<if $args[0] is 1>>
            $lineOne
        <<elseif $args[0] is 2>>
            $lineTwo
        <</if>>
    <</nobr>>\
<</widget>>

:: showLineOne
$lineOne

:: showLineTwo
$lineTwo

Download: Twee Code

results matching ""

    No results matching ""