Changes

→‎What's new for everything else: + Static delegate builder
Line 4,954: Line 4,954:  
</syntaxhighlight>
 
</syntaxhighlight>
 
|}
 
|}
 +
 +
===Static delegate builder===
 +
For C# mods, <samp>StaticDelegateBuilder</samp> is a specialized class which converts a method name into an optimized delegate which can be called by code. This is used by vanilla code to optimize methods referenced in assets like [[#Custom machines|<samp>Data/Machines</samp>]].
 +
 +
For example, given a static method like this:
 +
<syntaxhighlight lang="c#">
 +
public static bool DropInDeconstructor(Item machine, Item droppedInItem, Farmer who, bool probe, ref Item output, ref int minutesUntilReady) { ... }
 +
</syntaxhighlight>
 +
 +
And a delegate which matches it:
 +
<syntaxhighlight lang="c#">
 +
public delegate bool MachineItemConversionDelegate(Item machine, Item droppedInItem, Farmer who, bool probe, ref Item output, ref int minutesUntilReady);
 +
</syntaxhighlight>
 +
 +
You can create an optimized delegate and call it like this:
 +
<syntaxhighlight lang="c#">
 +
if (StaticDelegateBuilder.TryCreateDelegate("StardewValley.Object.DropInDeconstructor", out MachineItemConversionDelegate method, out string error))
 +
    return method(this, inputItem, who, probe, ref newHeldItem, ref minutesUntilMorning);
 +
else
 +
    Game1.log.Warn($"Machine {ItemId} has invalid conversion method: {error}");
 +
</syntaxhighlight>
 +
 +
The <samp>TryCreateDelegate</samp> method will cache created delegates, so calling it again will just return the same delegate instance.
    
===Game logging changes===
 
===Game logging changes===
translators
8,445

edits