Setting Variables using Sugarcube

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2.3.13
Story Format: Sugarcube 2.34.1

Currently, I’m creating a temperature tracker (point system) with the square element in the right-hand corner.

I have been following this guide on how to create a point system, but for some reason, the value I have set for the point system is not printing. Ideally, the Temperature should be set to 40 degrees Celsius and I am trying to create the $point variable so I can let the buttons affect the value printed in the square.

I have created a passage called StoryInit which contains:

$points=40;

Points: $points

Points: <<print $points>>

I am trying to print the $points in the StoryInterface passage I have created that contains:

<div class="container">
	       <div id="passages"></div>
		<div id="story-caption">
		  Temperature: <br><br> <span id="temp"><<print $points>>°C</span>
</div>
</div>

I have styled this element via:

#story-caption{
  font-family: Osmose;
  font-weight: 300;
  height: 150px;
  width: 150px;
  border-radius: 8px;
  position: fixed; 
  top: 0; 
  right: 0;
  margin: 50px;
  z-index: 1;
  background-color: white;
  color: black; 
  display: flex;
  flex-direction: column; 
  align-items: center;
  justify-content: center;
  
}

#temp{

 font-family: Osmose; 
 font-weight: bold;
 font-size: 40px;

}

Thank you for your time.
Sam

Ah…if you read carefully, that’s not exactly what that guide says. You shouldn’t be displaying the points in StoryInit, you just want to set the initial value there. Plus you need a <<set>> command to set or change the value of a variable.

So. StoryInit should have only <<set $points = 40>> (or <<set $points to 40>> if you prefer).

The rest of your code should work, I think, though you should be able to just write $points to instead of <<print $points>> to print it: see SugarCube’s docs on the naked variable syntax. You only need the <<print>> command for printing more complicated expressions.

Hi Josh,

I think you’ve gotten me closer now that I’ve made the updates you mentioned. Now it prints the variable’s title instead of the value.

StoryInit now just holds this:

<<set $points = 40>>

StoryInterface now contains this:

<div class="container">
		<div id="passages"></div>
		<div id="story-caption">
		  Temperature: <br><br> <span id="temp"> $points °C</span>
</div>
</div>

I read the doc on naked variable syntax and it seems like it should be printing based on the example. Do I need to initiate the variable again or something within StoryInterface for the number 40 to appear?

Sam

By default the content of your StoryInterface special Passage is considered raw HTML so that content isn’t being processed by the TwineScript parser, the exception to this is your passages ID’ed <div> element. This is why the current value of the $points variable is not being injected into that HTML.

There are a number of ways to resolve your issue, the following uses the setPageElement() function combined with a Passage to inject the output you want into the story-caption ID’ed <div> element. It also uses a :passagerender event handler to control when that injection occurs.

The following example is written in TWEE Notation.

:: StoryInit
<<set $points to 40>>

:: StoryInterface
<div class="container">
	<div id="passages"></div>
	<div id="story-caption"></div>
</div>

:: Story Caption
Temperature:
<br><br>
<span id="temp"><<print $points>>°C</span>

:: Start
The initial passage of the project...

Add the following JavaScript to your project’s Story JavaScript area, it causes the story-caption element to be updated with the contents of the Story Caption passage each time a Passage is shown.

$(document).on(':passagerender', function (ev) {
	setPageElement("story-caption", "Story Caption");
});

Alternatively. If per-turn updates are acceptable, then simply make use of the data-passage content attribute on the #story-caption element. For example (reusing Greyelf’s example):

:: StoryInit
<<set $points to 40>>

:: StoryInterface
<div class="container">
	<div id="passages"></div>
	<div id="story-caption" data-passage="Story Caption"></div>
</div>

:: Story Caption
Temperature:
<br><br>
<span id="temp">$points °C</span>

No JavaScript necessary.

SEE: StoryInterface special passage for details on that.

I totally forgot that the HTML Attribute > Special Attribute feature also works in StoryInterface, and missed the mention of that fact in the interface documentation, thank you.