Howdy, Stranger!

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

SugarCube print acting weird

edited June 2015 in Help! with 1.x
Whenever I use the <<print>> macro in SugarCube (1) it seems to print the answer a shade lighter than the rest of the text. This is rather annoying for formatting.

E.g.

Variable: <<print $variable>>%

In that, the result of the print will be a lighter shade of colour than the %. What's causing this?

To see it clearly, have a variable set to 1 and write something like:

<<print $variable>> 1

The two 1s will be different colours. At least in my story.

Comments

  • Neither the <<print>> macro, nor the naked $variable formatter, add anything to the resultant value before appending it to the render buffer (specifically, they do not wrap the value in any HTML). If you're seeing a difference, then it has to be because of something on your end (or your browser has gone mad).

    There are notable differences between them:
    • The <<print>> macro evaluates the expression passed to it, wikifies the final result, and appends that to the render buffer as-is.
    • The naked $variable formatter appends the $variable's value to the render buffer as-is.
    If you're simply printing the value of a $variable, then the only way you should possibly see a difference between them would be when using <<print>> on a value that contains markup (TiddlyWiki or HTML).

    Suggestions:

    You should already know this, however, you can inspect the generated DOM via your browser of choice's developer tools to ensure that nothing is wrapping the value, save whatever you (or some code you're using) might have put there.

    Try putting a naked $variable equivalent on the next line to see if you get the same result. For example:
    <<print $variable>> 1
    $variable 1
    
  • Have you added any CSS to your story, because if I create a new story, add <<set $variable to 1>> to my StoryInit special passage, then add just <<print $variable>> 1 to my Start passage and then test the story it generates the following HTML:
    <div class="body content">1 1</div>
    
    ... and I see nothing in the standard CSS that would cause the first of the 1's to be different to the second.
  • I just wanted to touch on this briefly:
    greyelf wrote: »
    […] it generates the following HTML:
    <div class="body content">1 1</div>
    
    ... and I see nothing in the standard CSS that would cause the first of the 1's to be different to the second.
    Correct, there is not. Beyond that, there cannot be, for a few important reasons:
    1. You cannot select non-element nodes via CSS. The initial "1" is a text node (well, both are, but we're only interested in the initial one here).
    2. Text nodes do not contain styling properties, and thus cannot be styled, even if you could select them somehow.
    Any styling of text nodes comes from, and must be applied to, ancestor element nodes.
  • edited June 2015
    100% positive it does not come from my css.

    This is because it only happens if the <<print>> is followed by another character.

    So <<print $variable>>% leads to the $variable display being light. However, simply writing 1% leads to it being normal colour. And <<print>> on its own is also normal.

    I will send you a screenshot so you can see I'm not going mad.

    Also I cannot use naked variables as I need to use that "%" directly after the print, so cannot have it merged with the variable.
  • edited June 2015
    So...

    In the area I have this shown, I am displaying these characters in black text on a black background with the following text shadow to create a white glow to illuminate them:
    	text-shadow: 0 0 2em #fff;
    	color: #000;
    

    What I am guessing is happening is that somehow, the glow from the neighbouring % sign is overlapping the printed variable and appearing on top of it, instead of undearneath it.

    That is exactly what it visually looks like.

    The easiest way to fix this would be to incorporate the % into the variable, somehow, so there is no adjacent other type of text, but I am not entirely sure how to write that. Having a brain fart right now.

    E.g. the variable needs to show somewhere between 1% and 100%, and dynamically update. I know how to do that with normal numbers, like 1, 2, 3 etc, but not sure if including the % in the variable numbers will break it?
  • edited June 2015
    Also, because I'm lazy, I'm using story-banner to show this dynamically updated variable, as story caption is already used for a hidden visibility task (providing hidden links to options menu).

    Is that a bad thing to use? :p I wanted to try using the inherent divs before I made a custom one.
  • edited June 2015
    Brain fart over.

    <<print $lucidity + "%">>

    fixed it. Now the % is contained in the print expression so its text shadow does not cause overlap.


    EDIT: Though I suppose I should update my SugarCube version... the one I am using doesn't seem to support naked variables. Maybe that would have fixed it too.
  • edited June 2015
    Claretta wrote: »
    Also I cannot use naked variables as I need to use that "%" directly after the print, so cannot have it merged with the variable.
    It won't be. The naked $variable formatter only formats $variables. The percent sign is not a valid identifier character, nor is it a valid part of either of the property access notations (i.e. the dot and square-bracket notations). The only time you should need to worry about the naked $variable formatter possibly doing the wrong thing, when other characters are snuggled next to a $variable, is when those characters are valid as part of an identifier or property access notation.

    TL;DR: The following works as expected:
    /* Assuming $lucidity is set to 100, the following will print "100%". */
    $lucidity%
    

    Claretta wrote: »
    What I am guessing is happening is that somehow, the glow from the neighbouring % sign is overlapping the printed variable and appearing on top of it, instead of undearneath it.

    That is exactly what it visually looks like.
    So your browser is going mad (i.e. you've found a browser bug, congratulations; at least, I'd hope that would be considered a bug). Have you tried this in other browsers to see what happens there? Not that I'm suggesting you don't work-around the issue, I'm just curious how widespread this issue is.

    It sounds like it's happening because the numeral and the percent sign are in different text nodes, so the easy work-around should be to conjoin them (which you've already thought of and done).

    Claretta wrote: »
    EDIT: Though I suppose I should update my SugarCube version... the one I am using doesn't seem to support naked variables. Maybe that would have fixed it too.
    Unfortunately not, if I'm correct about why (adjacent text nodes), since the $varaible's value and the percent sign would be processed separately, again resulting in two text nodes. The work-around you found is the correct one.
  • edited June 2015
    Yeah, I just upgraded to latest SugarCube and naked variables still do the bug. Oh well, least it's fixed. :p

    This is in Opera. I'll try other browsers.
  • edited June 2015
    Something I just thought of, for maximum uniformity you may wish to move the "Lucidity" text into the <<print>> as well, so the entire thing is one text node. For example:
    <<print "Lucidity " + $lucidity + "%">>
    

    Also, I just setup a test case and I cannot replicate your issue in any browser using the CSS you provided, so it seems that the issue is more complicated than simply that. In other words, the cause doesn't seem to simply be the CSS you've shown here plus the separate text nodes, but something else as well. As a guess, it's probably a combination of CSS that's causing it, though what that would be I have no clue.
  • edited June 2015
    I did some further testing.

    The main culprit is the font choice for that specific text: http://www.identifont.com/show?FES

    It does some pretty odd things with the text shadows.

    I cannot replicate the bug with a less-insane looking font.

    Out of Opera, Chrome, IE and Firefox, only IE is immune to the bug with this specific font. Luckily my game will be nodejs only so I won't need to worry about such concerns.
  • Ah, ye olde font rendering issue. Good to know.
Sign In or Register to comment.