0 votes
by (290 points)
retagged by

Hi everyone! I'm brand new to Twine so apologies if this is an obvious question. This forum has been a super big help, but I haven't been able to find a solution for a specific problem I'm having.

Currently using Twine Version 2.2.1 and Sugarcube 2.21.0

I'm hoping to simulate pop-ups in my game, so that when you close one dialogue box, a different one appears--and then have that repeat another 2-3 times.

I'm able to get a single dialogue box appear with the following:

Dialog.wiki(Story.get("Popup 1").processText());

And I see that there is a closeFn function when looking through the Sugarcube documentation, but I'm not able to figure out a way to get that command to open another dialog box. Any ideas on how I might be able to get this to work? I feel like it's doable but seems to be just beyond my knowledge.

Thanks so much for your help!

1 Answer

+1 vote
by (63.1k points)
selected by
Best answer

I don't think the closeFn argument of the Dialog.addClickHandler() method is the way to go here. Instead, I'd use recursion (which is when a function calls itself) and the :dialogclose event, with jQuery's one() method; something like:

setup.chainDialog = function (list, i) {
    if (i === undefined) {
        i = 0;
    if (i >= list.length) {
        // do nothing if we've emptied the list
    $(document).one(':dialogclose', function () {
        // have this function call itself and increase the counter by one
        setup.chainDialog(list, ++i);
    // show the dialog indicated by the counter


<<run setup.chainDialog(['popup 1', 'popup 2', 'popup 3', 'popup 4'])>>

Just pass the function an array of passage names to show, and it will show them in order, one after another, as they're closed, until it runs out of them.

by (290 points)
Yesss that worked exactly like I was hoping it would. Thank you so much Chapel!