Friday Night Funkin' Cookbook
Friday Night Funkin' CookbookExpertPatching code functions

Patching code functions

Reading time: 2 minutes

Simiarly to the sister article Script Merging, this is the variant for modifying base game code (rather than asset file scripts) without overrides - instead patching the start or the end of a function. Deciding to change the arguments, the state of the object, or if to run the original function at all. This functionality is very similar to Harmony Patching or Minecraft Mixins.

To start a patch, you will create a class (in CPPIA or hscript) - and decorate it with @patch - shown in the example below.

These cannot patch asset scripts (or mods).

Prefix

The prefix is a way of patching the before-call of a function, its what will run before the original function is invoked.

// access to `PatchState`
import polymod.patch.PatchState;

// the class and function to patch
@patch("funkin.play.scoring.Scoring", "scoreNote")
class MyPolymodPatch
{
    public static function prefix(state:PatchState):Void
    {
        // Patch state gives access to many things, like:
        // - the object (`.instance`)
        // - the arguments (`.args[]`)
        // - class name (`.className`)
        // - method name (`.field`)

        trace("This ran before the start of scoreNote");

        // You can choose to skip the resulting next call to the *actual* function by setting .skip to true
        // state.skip = true;
    }
}

The trace in that function will run before the start of scoreNote, and if .skip is set to true, it won't run scoreNote.

Postfix

A postfix is the natural extenstion to the prefix, something ran after the original function call. Here we are doubling the score:

import polymod.patch.PatchState;

@patch("funkin.play.scoring.Scoring", "scoreNote")
class MyPolymodPatch
{
    public static function postfix(state:PatchState):Void
    {
        // The postfix variant gives you fewer, but mostly the same set.
        // It's unique property is:
        // - the result (`.result`)
        // (which of course, lets you change the return value of the function)

        state.result *= 2; // double score...
    }
}

Multiple patches

You can setup @patch (the patch decoration) to take an array so you can patch multiple things in one class:

// ...

@patch(["funkin.ui.debug.charting.ChartEditorState", "handlePostUpdate"],
  ["funkin.ui.debug.charting.handlers.ui.ChartEditorUIHandler", "setupUIListeners"],
  ["funkin.ui.debug.charting.handlers.chart.ChartEditorCursorHandler", "handleCursor"])
class ChartEditorPad
{
    // ...

    public static function prefix(state:PatchState):Void
    {
        if (state.field == 'handleCursor' && padWantsMouse(cast state.args[0])) state.skip = true;
    }

    public static function postfix(state:PatchState):Void
    {
        if (state.field == 'handlePostUpdate') tick(cast state.instance);
        else if (state.field == 'setupUIListeners') buildMenu(cast state.args[0]);
    }


    // ...
}

Contributors:
kade-github
Last modified:
Created:
Category:  Expert
Tags: