How do you manage many many many scripts?

If there’s only two or three scripts, it may easy with story javascript page.
but with decades of scripts, it’s very hard to find or manage specific script.
and when I mistakenly push ctrl+z? all scripts will disapear and can’t be undo.

so, I wonder what is the most effective solution.
(both in performance and convenience)

  1. make script passage and include it in StoryInit passage to load on start
  2. make .js offline file and publish with it
  3. create a separated file with scripting tool and just copy that to story javascript page
  4. do it with widget as much I can. not scripts
  5. or any idea?

First, Twine v2.3.3 is out now, and it’s supposed to fix the CTRL+Z bug.

Next, unless it’s a short bit of code, I use Visual Studio Code (VSCode) along with the following extensions:

  • DeepScan - JavaScript checker (note: requires that you be online)
  • ESLint - JavaScript checker (note: requires that NPM is installed, which is included with Node.js, which is also required)
  • JSHint - JavaScript checker (note: incorrectly identifies a few things as problems and is slow on extremely large files, but catches a few things the other two don’t)
  • Numbered Bookmarks - Makes jumping around your code easier
  • Sugarcube2 - Syntax highlighter for SugarCube v2 code
  • Trailing Spaces - Highlights unnecessary spaces

There are a ton of other extensions for JavaScript and other things, such as CSS, so you might want to browse through the list of VSCode extensions.

You can then either edit your JavaScript in VSCode and then paste it into Twine, or you could use something like the code below to load the saved JavaScript file, which should be located in the same directory as the HTML (this goes in your JavaScript section):

if (window.hasOwnProperty("storyFormat") || document.location.href.toLowerCase().includes("/temp/")) {
	// Change this to the path where the HTML file is
	// located if you want to run this from inside Twine.
	setup.Path = "C:/Games/MyGame/";  // Running inside Twine application
} else {
	setup.Path = "";  // Running in a browser
}

setup.JSLoaded = false;
importScripts(setup.Path + "MyJavaScript.js")
	.then(function() {
		setup.JSLoaded = true;
	}).catch(function(error) {
		alert("Error: Could not find file 'MyJavaScript.js'.");
	}
);

Just change the path from "C:/Games/MyGame/" to the directory where the HTML will be located (make sure you match capitalization and use slashes /) and change MyJavaScript.js to the name of your JavaScript file.

Note that the SugarCube importScripts() function loads the JavaScript asynchronously, so any code in that JavaScript file won’t be ready to use until the .then() function is triggered (see the Promise object documentation for details). The code I gave above sets setup.JSLoaded to true once the JavaScript is loaded, so you can check setup.JSLoaded to determine if the JavaScript code is loaded or not.

As for which way is better? Well, I’d just paste it all into the JavaScript section to avoid any problems with the code being loaded asynchronously. That said, the best way to do it kind of depends on what you’re doing, since in other circumstances it may be better to have it as a separate file.

Hope that helps! :slight_smile:

2 Likes

You don’t have to use Twine 2. There are Twee compilers like TweeGo and my own Extwee. (Twee2 also exists but isn’t actively being maintained.)

If you are comfortable working in an editor, like @HiEv suggests for a larger amount of scripts, these may be good tools to consider. You could follow all of the suggestions HiEv includes and then use the Twee compiler to “compile” the project into a Twine 2-compatible HTML file.

If you are going to use a toolchain of linter, hinter, and other things, it might be worth moving over to a command-line tool where all of these could be run as part of scripted process.

However, all that written, if you need the visual editor to see the connections between passages, using a Twee compiler will take that away. Twee is simply a notation format for passages. (There are also syntax highlighters out there for various editors that help with writing Twee, though.)

I almost have no idea’s about coding and how to use text editor…
but with some strgling days, I found this is the best way to build my game.
thank you!