Anonymous

Mod Creation/Mod Context API Reference: Difference between revisions

From Melvor Idle
Line 843: Line 843:
ctx.patch(TownshipResource, 'amount').get(() => 999999);</nowiki>
ctx.patch(TownshipResource, 'amount').get(() => 999999);</nowiki>


==== PropertyPatch.set(setter: (value: any) => void): void ====
==== PropertyPatch.set(setter: (o: (value: any) => void, value: any) => void): void ====


Execute the provided function when a setter property is accessed.
Execute the provided function when a setter property is accessed.
Line 849: Line 849:
'''Parameters'''
'''Parameters'''


<code>setter: (value: any) => any</code> The getter function to be executed, with the <code>value</code> argument containing the value being set.
<code>setter: (o: (value: any) => void, value: any) => void</code> The setter function to be executed. The first parameter, <code>o</code>, is a reference to the setter method being replaced, which is either a previous setter patch or the original setter method. The second parameter, <code>value</code>, contains the value being set.


'''Example'''
'''Example'''


  <nowiki>// Also unlimited township resources, in a roundabout way
  <nowiki>// Sorry, there aren't many setters in the game to use for a practical example
// Sorry, there aren't many setters in the game to use for a good example
// Doubles whatever resource amount is being set
ctx.patch(TownshipResource, 'amount').set(function(amount) {
ctx.patch(TownshipResource, 'amount').set((o, amount) => o(amount * 2));
  if (amount < 0) this._amount = 999999;
// While in-game
  else this._amount = amount;
game.township.resources.getObjectByID('melvorF:Wood').amount = 1000;
});</nowiki>
game.township.renderQueue.resourceAmounts = true;
// 2000 wood is available</nowiki>


==== PropertyPatch.replace(getter?: () => any, setter?: (value: any) => void): void ====
==== PropertyPatch.replace(getter?: () => any, setter?: (value: any) => void): void ====
91

edits