Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Add Macros in Twine 2 with SugarCube 2

New to Twine, started with Twine 2 and SugarCube 2.

I want to experiment with custom macros, but every time I try to play my Story with any macros I get an error. The alert dialog is partially cut off, but I think it reads: "Error [tw-user-script-0]: undefined is not a function". The macro in question is:
macros.add("mytest", {
		handler: function () {
			try {
				console.log("This is a test.");
			}
			catch (e) {
				return this.error("Yay, you broke it! " + e.message);
			}
		}
});

Help?

Comments

  • I have finally found reference to that error in regards to using SugarCube 1 code with SugarCube 2, so presumably the examples I was working with aren't valid for SugarCube 2. Sigh!
  • I suggest reading the Macros API documentation, it contains examples and note the uppercase 'M' on the word Macro in the Macro.add call.

    Without testing I think your original example would look like:
    Macro.add("mytest", {
        handler  : function () {
            try {
                console.log("This is a test.");
    
            } catch (e) {
                return this.error("bad conditional expression: " + e.message);
            }
        }
    });
    
  • Yep, that was it- lower case 'm' when it should be capital 'M' and 'macros' plural instead of 'macro' singular. Thank you!
Sign In or Register to comment.