91
edits
m (fix double "the") |
Buttchouda (talk | contribs) (Add clarification that traditional function expressions are more suitable and preferred to arrow function expressions when patching methods and updated examples.) |
||
Line 772: | Line 772: | ||
== Game Object Patching/Hooking == | == Game Object Patching/Hooking == | ||
=== 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> | |||
=== patch(className: class, methodOrPropertyName: string): MethodPatch | PropertyPatch === | === patch(className: class, methodOrPropertyName: string): MethodPatch | PropertyPatch === | ||
Line 803: | Line 821: | ||
<syntaxhighlight lang="js" line>// Double all XP gains | <syntaxhighlight lang="js" line>// Double all XP gains | ||
ctx.patch(Skill, 'addXP').before((amount, masteryAction) | ctx.patch(Skill, 'addXP').before(function (amount, masteryAction) { | ||
return [amount * 2, masteryAction]; | |||
});</syntaxhighlight> | |||
==== MethodPatch.after(hook: (returnValue: any, ...args: any) => any | void): void ==== | ==== MethodPatch.after(hook: (returnValue: any, ...args: any) => any | void): void ==== | ||
Line 816: | Line 836: | ||
<syntaxhighlight lang="js" line>// The player never misses an attack | <syntaxhighlight lang="js" line>// The player never misses an attack | ||
ctx.patch(Player, 'rollToHit').after(willHit | ctx.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; | ||
Line 831: | Line 851: | ||
'''Example''' | '''Example''' | ||
<syntaxhighlight lang="js" line>ctx.patch(Skill, 'addXP').replace(function(o, amount, masteryAction) { | <syntaxhighlight lang="js" line>ctx.patch(Skill, 'addXP').replace(function (o, amount, masteryAction) { | ||
// Prevent any woodcutting XP | // Prevent any woodcutting XP | ||
if (this.id === 'melvorD:Woodcutting') return; | if (this.id === 'melvorD:Woodcutting') return; | ||
Line 846: | Line 866: | ||
<syntaxhighlight lang="js" line>const xpPatch = ctx.patch(Skill, 'addXP'); | <syntaxhighlight lang="js" line>const xpPatch = ctx.patch(Skill, 'addXP'); | ||
xpPatch.replace((o, amount, masteryAction) | xpPatch.replace(function (o, amount, masteryAction) { | ||
console.log('Replacement #1'); | console.log('Replacement #1'); | ||
return o(amount, masteryAction); | return o(amount, masteryAction); | ||
}); | }); | ||
xpPatch.replace( | xpPatch.replace(function (o, amount, masteryAction) { | ||
console.log('Replacement #2'); | console.log('Replacement #2'); | ||
return o(amount, masteryAction); | return o(amount, masteryAction); | ||
Line 872: | Line 892: | ||
<syntaxhighlight lang="js" line>// Effectively double available Township resources | <syntaxhighlight lang="js" line>// Effectively double available Township resources | ||
ctx.patch(TownshipResource, 'amount').get((o) | ctx.patch(TownshipResource, 'amount').get(function (o) { | ||
return o() * 2; | |||
}); | |||
// Or more practically, make resources unlimited | // Or more practically, make resources unlimited | ||
ctx.patch(TownshipResource, 'amount').get(() | ctx.patch(TownshipResource, 'amount').get(function () { | ||
return 999999; | |||
});</syntaxhighlight> | |||
==== PropertyPatch.set(setter: (o: (value: any) => void, value: any) => void): void ==== | ==== PropertyPatch.set(setter: (o: (value: any) => void, value: any) => void): void ==== | ||
Line 888: | Line 912: | ||
<syntaxhighlight lang="js" line>// Sorry, there aren't many setters in the game to use for a practical example | <syntaxhighlight lang="js" line>// Sorry, there aren't many setters in the game to use for a practical example | ||
// Doubles whatever resource amount is being set | // Doubles whatever resource amount is being set | ||
ctx.patch(TownshipResource, 'amount').set((o, amount) | ctx.patch(TownshipResource, 'amount').set(function (o, amount) { | ||
return o(amount * 2); | |||
}); | |||
// While in-game | // While in-game | ||
game.township.resources.getObjectByID('melvorF:Wood').amount = 1000; | game.township.resources.getObjectByID('melvorF:Wood').amount = 1000; |
edits