Display int variable

This feels so simple, but I can't get a number variable to display the same as a text variable. I started with just doing this

@set sanity = 100

@dec sanity

Sanity equals {sanity}.

It displays as "Sanity equals null." From past programming stuff, my guess would be that I need to tell it to convert the int to text, but I don't know how to properly do it in Squiffy. My last attempt, I got the code light up in colors, as if it was recognizing the Javascript, but then all it did was display the code in a box...

@set sanity = 100

@dec sanity

    var dSanity = sanity.toString();

Sanity equals {dSanity}.

I don't know why your first code isn't working; it should work fine. Type conversion in squiffy is implicit. There is no reason that shouldn't work; so I'd be checking for typos or other non-obvious errors. (Is there a problem with having spaces before and after the =? I don't remember now)

The second piece of code will not work. Squiffy attributes and javascript variables are not the same thing. If you really wanted to do that, you'd have to do something like:

    var sanity = squiffy.get("sanity");
    squiffy.set("dSanity", sanity.toString());

However, this should not be necessary. Type conversion is automatic; you never have to do it manually.

My last attempt, I got the code light up in colors, as if it was recognizing the Javascript, but then all it did was display the code in a box...

This sounds like it's parsing it as text. I can't double-check the order now, as it seems the online editor is broken. But I know that order matters to Squiffy. Should the javascript come before the @ lines? I forgot what the correct order is.


Thank you for clarifying! I thought that was the case, from the way it's written in the documentation. Knowing that with more certainty, I went back and did some more experimenting. I seem to have found a solution, although it's not ideal to me. I think I can work with it though. I'll try to lay out the larger picture while trimming as much excess as I can. This is maybe gonna look a little weird. To explain, what I really needed here was a switch case of some kind. The player is given some option on how to try and stay awake on a long drive. So what I had before was this:

Window:  {rotate car_window:open:closed}
<br>Music: {rotate car_music:off:radio:eMuse}

[[Try to stay awake]]

[[Try to stay awake]]:
@set path_sorter = 0
## ...some code to sort the outcome by numbers to go to different passages, including...

{if path_sorter=3:{off_open}}

[off_open]:
A bunch of scary stuff happens here.
@set sanity = 100
@dec sanity
Current Sanity: {sanity}

No arrangement of that seemed to work, but if I just took those 3 lines about sanity and put them in a blank new game, they worked fine, as they should. Finally, I found that if I made the if case lead to a button to activate the passage with square brackets, instead of directly displaying the passage as embedded with the curly brackets, it worked. So:

{if path_sorter=3:{off_open}}
# had to be changed to
{if path_sorter=3:[off_open]}

forcing the player to hit another button, rather than go straight to the text. Why that matters remains entirely beyond me, something about scope, or calling an embed from an embed? At any rate, I think I can make it work, and I'll try to avoid "embed inception" in the future, lol.


If I remember correctly, a section name in curly brackets outputs the literal text part of the section, but does not run its scripts.

I think the only way to get around that extra click would be using javascript.
Something like (off the top of my head; I could have misremembered the function names)…

[[Try to stay awake]]:
    if(squiffy.get("path_sorter") == 3) {
        squiffy.story.run(squiffy.section.passages["off_open"]);
    }

@set path_sorter = 0
## ...some code to sort the outcome by numbers to go to different passages, including...

{if path_sorter=3:{off_open}}

[off_open]:
A bunch of scary stuff happens here.
@set sanity = 100
@dec sanity
Current Sanity: {sanity}

Manually running the @ lines before the passage is inserted.


Actually, a more common way to do this would be jumping to the passage directly. For example:

[[Try to stay awake]]:
    if(squiffy.get("path_sorter") == 3) {
        squiffy.story.passage("off_open");
    }

@set path_sorter = 0

Note that any text here will be displayed
*after* the off_open passage

This is an actual bug but mrangel found it and fixed it in his unofficial Squiffy 5.1.4.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums