[Sugarcube] Displaying a passage using a variable

What I need to do is to display a passage, but having the passage be a variable which has already been set as the text of one of many possible passages.

I’ve tried the following making it as simple as possible for trying to work this out where that variable is a passage title I know to exist:

<<set $variable to "passage name">>
<<display '$variable'>>
<<display $variable>>

And some more variations but on testing it claims it’s some variation of undefined, which it’s not as checking the varaible as text right afterwards gives what I’d expect it to be.

Would really appreciate some help as trying to google this all of the questions are about variables in links which is a separate issue to what I’m trying to do.

It seems to be working for me in Sugarcube 2 without quotes around the variable.

<<set $variable to "passage name">>
<<display $variable>>

Have you checked for capitalization differences in the relevant passage title?

Are you sure that the undefined error isn’t part of the passage that you are trying to display? Is it possible that the displayed passage is trying to set the variable as well?

I tried it with just plain text in the displayed passage.

1 Like

Okay I have no idea what I was trying there, but redoing it without the quotes does indeed work. Must have been a capitalization or something like that when I was trying it all the times before.

Thanks and sorry for wasting your time due to me gotting lost in the woods and so failing to spot the tree right in front of me :slight_smile:

1 Like

FYI - The <<display>> macro is depreciated. You should use the <<include>> macro instead.

Also, you can run your HTML through SugarValidator to help catch depreciated macros like that, as well as detecting some kinds of broken code.

Hope that helps! :slight_smile:

2 Likes

Oh wow, that things amazing. Thank you very much!

Okay one quick question on that tool if you don’t mind.

I’m getting some of these when I put my game file in.

Found an opening '<<' without matching '>>' Line: <<set $2d6 to Math.clamp($2d6 /* 2, 0, 15)&gt;&gt;

That was one I specifically copied and pasted the working >>'s from the line above to see what was going on after getting the first report, and it cropped up that exact line again. I get &gt is a > so maybe an issue with the text formatting, but I’m not sure why that and some other lines are being complained about, why it persisted after being pasted over, and is it something to be too concerned with?

It looks like the file isn’t being parsed properly for some reason, since those &gt; bits should be checked as though they’re > instead.

You could report the problem at the SugarValidator “Issues” page on GitHub, though they’ll likely need a link to your HTML file to figure out the problem.

However, it might be because you’re using an invalid variable name. $2d6 isn’t a valid variable name, because the first character after the $ or _ in a SugarCube variable name should pretty much only be an upper- or lower-case English letter (A to Z or a to z). (Technically speaking, $ or _ are also valid there, but they could cause formatting issues due to being mistaken for Twine markup, so I’d avoid using them there.) Numbers are fine after that, so you could do $die2d6, they’re just not valid at the start of the variable name. See the “Variable Names” section of the SugarCube documentation.

Hope that helps! :slight_smile:

2 Likes

Appreciate the response and good to know on that specific variable which I’ll change. Will play around with it as it seems to be that specific form of setting a numeric variable as it’s happened on some others that did start with a letter correctly (Edit: on checking likely as that’s the wrong way to multiply with it should be '=’ and not '/’ based on the documentation, huh, wonder where I got that from originally as I’ve used it for all variable multiplication in the game, well easy fix). Thanks for your help, cheers!

Ah! I see what’s going on now. I had assumed that the /* was just a copying glitch and that the forum was displaying it differently than your actual code, since it clearly wasn’t valid code.

The problem was that /* is the start of a comment marker (see the SugarCube “Comment” documentation), thus everything after that was a comment, hence why SugarValidator couldn’t find the >> at the end, since it ignores the contents of comments.

Anyways, what you actually want to do is this:

<<set $die2d6 = Math.clamp($die2d6 * 2, 0, 15)>>

You don’t want to use *= there, since you don’t need to assign a value at that point, you just need to get the product. The *= should be used if you want to do something like this:

<<set $die2d6 *= 2>>

Which would set $die2d6 equal to itself multiplied by 2.

See the MDN article “Expressions and operators” for details on how all of those things work.

Have fun! :slight_smile:

2 Likes

You’ve been really helpful, thanks again!