Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
Tutorial explaining how to get started writing custom parsers in AdventureJS. tutorial, parsers

Advanced Scripting:Custom Parsers

Parsing player input is obviously the core of what AdventureJS does. However, authors can inject custom parsers to handle input before the AdventureJS parser runs. Custom parsers can modify player input before passing it on, or override the main parser entirely.

Custom parser allow authors to write specialized parsers to handle things like directed dialog, or navigating within a simulated terminal console – anything that lies beyond the focus of the default parser.

Once created, custom parsers can be enabled and disabled at will. So, for instance, they can be turned on for specific parts of a game. They can pass the original player input or a modified version of it to the default parser. Alternately, they can end the turn. If the game is handling queued inputs, a custom parser can return null to advance to the next input, or false to end the turn entirely.

Custom parser methods:

Examples

We offer a bare bones example because writing the parser is up to you. We offer the container – it's up to you to fill it. Basically all you need to know is that your parser will receive an input param containing the unedited player input.

MyGame.parser.createParser({
  MyParser: function (input) {
    console.log("MyParser", input);

    // if conditions apply
    // do something to input

    return input; // or false or null to end turn
  },
});

Newly created parsers are enabled by default. To disable a parser...

MyGame.disableParser(MyParser);

To enable a parser...

MyGame.enableParser(MyParser);

You can establish multiple parsers with one createParser() call and then disable / enable them individually.

MyGame.parser.createParser({

  DialogParser: function (input) {
    console.log("DialogParser", input);

    // if conditions apply
    // do something to input

    return input; // or false or null to end turn
  },

  ConsoleParser: function (input) {
    console.log("ConsoleParser", input);

    // if conditions apply
    // do something to input

    return input; // or false or null to end turn
  },
  
});