Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ChapelR committed Jan 24, 2020
1 parent 1e1ab73 commit bf7d59d
Showing 1 changed file with 84 additions and 3 deletions.
87 changes: 84 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,94 @@ You can use the JavaScript `this` keyword to access the *macro context* from wit

### Creating Command Macros

Creating command macros is fairly straight-forward.
Creating command macros is fairly straight forward.

TODO
Command macros need a name and a handler function and can return values to be used as arguments to other macros or to be displayed to users. For example:

```javascript
Harlowe.macro('mymacro', function () {
return 'Hey there!';
});
```

In a passage:

```
(set: $var to (mymacro:))
$var <!-- 'Hey there!' -->
```

You can read the arguments passed to a macro as well:

```javascript
Harlowe.macro('greet', function (name) {
return 'Hey, ' + name + '!';
});
```

In passage:

```
(set: $var to (greet: 'Bob'))
$var <!-- 'Hey, Bob!' -->
(greet: 'Jane') <!-- 'Hey, Jane!' -->
```

Of course, checking to make sure you get the arguments you expect, or setting up sane defaults is always wise:

```javascript
Harlowe.macro('greet', function (name) {
if (!name || typeof name !== 'string' || !name.trim()) {
name = 'Player';
}
return 'Hey, ' + name + '!';
});
```

You may opt instead to throw errors:

```javascript
Harlowe.macro('greet', function (name) {
if (typeof name !== 'string' || !name.trim()) {
throw new TypeError('The "name" parameter of the (' + this.name + ':) macro should be a non-empty string!');
}
return 'Hey, ' + name + '!';
});
```

Macros do not need to return anything in specific and can also be used to simply run any arbitrary JavaScript code.

```javascript
Harlowe.macro('log', function (content) {
if (content !== undefined) {
console.log(content);
}
});
```

You can access arguments via the macro context (via `this`) instead of the function's parameters:

```javascript
Harlowe.macro('log', function () {
if (this.content !== undefined) {
console.log(this.content);
}
});
```

You can also access the macro's name through the macro context:

```javascript
Harlowe.macro('log', function () {
if (this.content !== undefined) {
console.log('The macro (' + this.name + ':) says:', this.content);
}
});
```

### Creating Changer Macros

Changer macros are significantly more complex than command macros.
Changer macros are significantly more complex than command macros. Fortunately, most of what applies to command macros also applies to these macros. The biggest difference is that you can't return anything from the handlers of changer macros, and that changer macros have an additional changer function argument that handles most of the actual logic.

TODO

Expand Down

0 comments on commit bf7d59d

Please sign in to comment.