91
edits
Buttchouda (talk | contribs) |
Buttchouda (talk | contribs) (Clarified that traditional function expressions are more suitable than arrow functions for method patching) Tag: visualeditor-switched |
||
Line 619: | Line 619: | ||
From there you can use that patch to perform any of the following actions. | From there you can use that patch to perform any of the following actions. | ||
=== A Quick Note on Function Syntax === | |||
When patching methods, for most scenarios you'll want to use a traditional function expression, rather than the arrow expression syntax. This will ensure <code>this</code> is bound to the class instance that is calling the method, rather than the context where the patch was defined. | |||
For example, | |||
<syntaxhighlight lang="js" line="1">export function setup({ patch }) { | |||
const methodPatch = patch(Class, 'method'); | |||
// Do this | |||
methodPatch.before(function () { }); | |||
// Or this | |||
function beforePatch () { } | |||
methodPatch.before(beforePatch); | |||
// Not this, unless you understand the implications of doing so | |||
methodPatch.before(() => { }); | |||
}</syntaxhighlight> | |||
=== Do Something Before === | === Do Something Before === | ||
Line 624: | Line 642: | ||
Use the <code>before</code> method on the patch object to execute code immediately before the patched method. In addition, the callback hook will receive the arguments that were used to call the patched method as parameters, and can optionally modify them by returning the new arguments as an array. | Use the <code>before</code> method on the patch object to execute code immediately before the patched method. In addition, the callback hook will receive the arguments that were used to call the patched method as parameters, and can optionally modify them by returning the new arguments as an array. | ||
<syntaxhighlight lang="js" line>// setup.mjs | <syntaxhighlight lang="js" line="1">// setup.mjs | ||
export function setup({ patch }) { | export function setup({ patch }) { | ||
patch(Skill, 'addXP').before((amount, masteryAction) | patch(Skill, 'addXP').before(function (amount, masteryAction) { | ||
console.log(`Doubling XP from ${amount} to ${amount * 2}!`); | console.log(`Doubling XP from ${amount} to ${amount * 2}!`); | ||
return [amount * 2, masteryAction]; // Double all XP gains | return [amount * 2, masteryAction]; // Double all XP gains | ||
Line 636: | Line 654: | ||
Use the <code>after</code> method on the patch object to execute code immediately after the patched method. In addition, the callback hook will receive the value returned from the patched method along with the arguments used to call it as parameters. Optionally, an after hook can choose to override the returned value by returning a value itself. '''''Only''' a return value of <code>undefined</code> will be ignored.'' | Use the <code>after</code> method on the patch object to execute code immediately after the patched method. In addition, the callback hook will receive the value returned from the patched method along with the arguments used to call it as parameters. Optionally, an after hook can choose to override the returned value by returning a value itself. '''''Only''' a return value of <code>undefined</code> will be ignored.'' | ||
<syntaxhighlight lang="js" line>// setup.mjs | <syntaxhighlight lang="js" line="1">// setup.mjs | ||
export function setup({ patch }) { | export function setup({ patch }) { | ||
patch(Player, 'rollToHit').after((willHit) | patch(Player, 'rollToHit').after(function (willHit) { | ||
if (!willHit) console.log('A miss? I think not!'); | if (!willHit) console.log('A miss? I think not!'); | ||
return true; | return true; |
edits