0 votes
by (2.4k points)

Hey there !

I'm using Chapel's <<message>> macro 

 

// message macro, by chapel (with help from T.M. Edwards); for sugarcube 2
// version 1.0.0
// see the documentation: https://github.com/ChapelR/custom-macros-for-sugarcube-2#message-macro

//intialize namespace
setup.messageMacro = {};

// default text option:
setup.messageMacro.default = 'Help';

// <<message>> macro
Macro.add('message', {
    tags    : null,
    handler : function () {
        var message  = this.payload[0].contents;
        var $wrapper = $(document.createElement('span'));
        var $link    = $(document.createElement(this.args.includes('btn') ? 'button' : 'a'));
        var $content = $(document.createElement('span'));

        $link
            .wiki(this.args.length > 0 && this.args[0] !== 'btn' ? this.args[0] : setup.messageMacro.default)
            .ariaClick(function () {
                if ($wrapper.hasClass('open')) {
                    $content
                        .css('display', 'none')
                        .empty();
                }
                else {
                    $content
                        .css('display', 'block')
                        .wiki(message);
                }

                $wrapper.toggleClass('open');
            });

        $wrapper
            .attr('id', 'macro-' + this.name + '-' + this.args.join('').replace(/[^A-Za-z0-9]/g, ''))
            .addClass('message-text')
            .append($link)
            .append($content)
            .appendTo(this.output);
    }
});

 

Which works perfectly well.  "This macro displays a link or, optionally, a button. The link or button can be clicked to display a message immediately below it in the passage text, and clicked again to collapse the message."


I have, however, an issue with it. I'm using that macro in my StoryCapion passage. Which means the macro loads again and again. 
Whenever I change passage within the story, the unfolded text re-folds itself within the link, and the player has to click the link again to display it. Would there be a way to solve this ? I was thinking of adding a boolean variable, something like <<set $opened to true>> whenever it would be opened, and <<set $opened to false>> whenever it would be closed, and it wouldn't close whenever $opened is true. 
But I have no idea how to include that within the javascript part of the code...

Thanks a lot ! 

1 Answer

0 votes
by (1.1k points)
Just off the top of my head, what if you put an <<if>> (to see if it is in the state you want) in the PassageHeader? That way it would render first and see if the need has been met?
by (2.4k points)
Hey there, I'm not sure to understand what you mean?
The <<message>> macro is used within StoryCaption, how can I interact with it in PassageHeader ?

Thanks
by (1.1k points)
Right. Exactly. I am saying to ask a question in the PassageHeader and then based on that response set a variable to show or not show your item in the StoryCaption. In my current game I ask if a character has a book. If they do, then trip a display in the Caption. And if they do not then do not.

Does that make sense?
by (2.4k points)
Oh alright, I see what you want to do. I could do that for an item, yes. But that doesn't solve my <<message>> macro issue, does it ? If so, I don't see how it could... :(
...