Howdy, Stranger!

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

Creating and Using a Simple JavaScript Macro

edited January 2014 in Help! with 1.x
To keep from derailing my last thread, I decided to split the conversation here.

Unfortunatley, the only resource I found for creating macros in Twine was this: http://eturnerx.blogspot.com/2012/12/how-to-create-custom-macros-in-twine.html

As the author states, it was not intended for JavaScript beginners.

In addition to seeking general advice, I have three questions:


1. If a person wants to create a JavaScript macro for use in Twine, how does one set the name of the macro for use in Twine?

2. How does one use Twine variables in a JavaScript Macro?

3. How does one set Twine variable values in a Macro for use in Twine?

Comments

  • The article you linked contains this example code:

    macros['macrodemo'] = {
    handler: function(place, macroName, params, parser) {
    new Wikifier(place, "Hello World!");
    },
    init: function() { },
    };
    Here the macro's code is put into a function assigned to the 'handler' field; that part is missing in your attempt. Also there is a function assigned to the 'init' field; even though it is empty its presence might be required.

    By the way, SugarCube has the <<widget>> macro to simplify the generation of custom macros. You can find an exanple in the second code fragment in this post.
  • Thanks! With that boiled-down version, I corrected my attempt:
    macros['test'] = {
    handler: function(place, macroName, params, parser) {

    var foo = state.history[0].variables.foo;
    foo = foo + 1;
    state.history[0].variables.foo = foo;

    },
    init: function() { },
    };
    That works! It adds 1 to $foo, just like I wanted. The name of the macro is test.

    That's all three questions answered! :D

    Thanks again!
Sign In or Register to comment.