How can I set a javascript to print a passage in twine

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.14
Story Format: Sugarcube 2.34.1

I’m trying to make a basic dynamic descriptor system that will fill out an appearance description based on the value of certain descriptor variables. I currently have it working correctly with the following code as well as having it set to redirect to another passage when any of the variables go over a certain value.

setup.test = function()
{
var text
if(State.variables.test == 0)
{
text = “Placeholder”
}
else if(State.variables.test > 0 && State.variables.test <= 2)
{
text = “Placeholder”
}
else if(State.variables.test > 2 && State.variables.test <= 4)
{
text = “Placeholder”
}
else if(State.variables.test > 4 && State.variables.test <= 6)
{
text = “Placeholder”
}
else if(State.variables.test > 6 && State.variables.test <= 8)
{
text = “Placeholder”
}
else if(State.variables.test > 8 && State.variables.test <= 10)
{
text = “Placeholder”
}
else if(State.variables.test > 10 && State.variables.test <= 12)
{
text = “Placeholder”
}
return text
}

var oneThreshold = 13;
// Force the player to the “Too Much blank” passage if they let the variable get too high.
Config.navigation.override = function (dest) {
var sv = State.variables;

if (sv.test1 >= oneThreshold) {
    return "Too Much test";
}
if (sv.test2 >= oneThreshold) {
    return "Too Much test";
}
if (sv.test3 >= oneThreshold) {
    return "Too Much test";
}
if (sv.test4 >= oneThreshold) {
    return "Too Much test";
}

};

However For the more detailed descriptions I’d like to use That would mean taking up a LOT of space in that code with just raw text. So i was trying to figure out how i could, instead, just write the descriptions i want in standalone twine passages and have the javascript Print those passages wherever I’m telling it to print that particular variable.

so instead of it printing text in the [text = “Placeholder”] space in the code, it would call up and print the content in a [[Description One]] passage. That way i don’t have to have a bunch of raw text in the code itself.

however I’m a total noob at javascript and barely know how to minorly edit code other people give to me to copy paste, lol. So I’m unsure exactly how to do it. Any advice would be appreciated.

You could just replace the first part of your code with something like this:

setup.test = function() {
	var text = "";
	if (State.variables.test <= 0)
		text = "<<include 'Description One'>>";
	} else if (State.variables.test <= 2) {
		text = "<<include 'Description Two'>>";
	} else if (State.variables.test <= 4) {
		text = "<<include 'Description Three'>>";
	} else if (State.variables.test <= 6) {
		text = "<<include 'Description Four'>>";
	} else if (State.variables.test <= 8) {
		text = "<<include 'Description Five'>>";
	} else if (State.variables.test <= 10) {
		text = "<<include 'Description Six'>>";
	} else if (State.variables.test <= 12) {
		text = "<<include 'Description Seven'>>";
	}
	return text;
};

(Note: You don’t need the State.variables.test > N && stuff in this case, because that’s already implied by the structure of the if statement. That’s why I removed it.)

Now, doing:

<<= `setup.test()`>>

in your passages will cause it to use the <<include>> macro to display the contents of the passage passed to that macro within the setup.test() function. (Note: <<=>> is just a shorthand version of the <<print>> macro.)

Hopefully that answers your question! :slight_smile:

P.S. When posting code here, you should mark it as “preformatted text” using the “</>” button in the post editor’s toolbar, such as I did above. This forum tends to sometimes eat code if you don’t do that.

Thats EXACTLY what i wanted. Thanks!

I knew it was something stupidly simple. I dunno why I never thought about putting twine macros into the text the script prints. I spent literally over an hour this morning googling around trying to find someone else that had already described how to do it and couldnt find it.

And yea, sorry. literally first time posting here, haha. I’ll try to do that in the future. Thanks again for the help <3