This code creates a mod, that turns tears into dark matter tears (slowing black tears). It also adds one coin to the player every shot.
1 2 3 4 5 6 7 8 910
localmod=RegisterMod("Dark Matter tears",1)-- Register the mod in the API (dont change anything here, except the name)functionmod:onTear(tear)localplayer=Isaac.GetPlayer(0)--get the player entityplayer:AddCoins(1)-- add a cointear.TearFlags=tear.TearFlags|TearFlags.TEAR_SLOW-- add slowing effect to the teartear:ChangeVariant(TearVariant.DARK_MATTER)-- change appearance of the tearendmod:AddCallback(ModCallbacks.MC_POST_FIRE_TEAR,mod.onTear)-- Trigger the function "onTear()", when the "POST_FIRE_TEAR" callback is triggered.
localmod=RegisterMod("Dark Matter tears",1)-- Register the mod in the API (don't change anything here, except the name)localfunctiononTear(_,tear)localplayer=Isaac.GetPlayer(0)-- get the player entityplayer:AddCoins(1)-- add a cointear.TearFlags=tear.TearFlags|TearFlags.TEAR_SLOW-- add slowing effect to the teartear:ChangeVariant(TearVariant.DARK_MATTER)-- change appearance of the tearendmod:AddCallback(ModCallbacks.MC_POST_FIRE_TEAR,onTear)-- Trigger the function "onTear()", when the "POST_FIRE_TEAR" callback is triggered.
Instead of the function being stored inside of the mod variable it is it's own variable local to the level of creation. When creating a local function the first parameter looks for itself and this argument is commonly disregarded by storing it as an underscore. When calling/triggering this function only the function name is necessary, as it is not stored within the mod variable. Local functions are able to be stored within a table just like any other variable, for example:
12345
localfunctions={[1]=function(_,tear)end,}
where this function would be called via functions[1], and is used in place of onTear in the AddCallback function of the main example
Example code:
localmod=RegisterMod("Dark Matter tears",1)-- Register the mod in the API (don't change anything here, except the name)mod:AddCallback(ModCallbacks.MC_POST_FIRE_TEAR,function(_,tear)localplayer=Isaac.GetPlayer(0)-- get the player entityplayer:AddCoins(1)-- add a cointear.TearFlags=tear.TearFlags|TearFlags.TEAR_SLOW-- add slowing effect to the teartear:ChangeVariant(TearVariant.DARK_MATTER)-- change appearance of the tearend)-- Trigger the function created within the function argument of the AddCallback function, when the "POST_FIRE_TEAR" callback is triggered.
In this example the function being triggered by the callback is defined within the AddCallback function. This method of creation removes the need for storing and referencing your function from a variable, similarly to the local function example the function given will attempt to pass itself has the first argument so it is disregarded with an underscore. Since this function is not stored within your code it is not possible to call it from other places, so this method should only be used if it is to only be triggered by the callback