Skip to content

Global Classes

JonataOliveiraa edited this page Nov 14, 2026 · 1 revision
Basics

Global classes change what already exists. Where a ModItem describes one new item, a GlobalItem sees every item in the game, the game's own and other mods' alike, and can change any of them. The same idea exists for projectiles, NPCs, tiles, buffs and more. This page covers GlobalItem and GlobalProjectile; GlobalNPC is explained together with ModNPC on NPCs.

Changing every item: GlobalItem

A GlobalItem runs for items that are not yours: the game's and other mods'. It loads like any content class (by itself, from your .dll).

public class HeavierWeapons : GlobalItem
{
    public override bool AppliesToEntity(Item entity, bool lateInstantiation) =>
        lateInstantiation && entity.damage > 0;

    public override void SetDefaults(Item entity) => entity.knockBack *= 1.5f;
}
  • AppliesToEntity(entity, lateInstantiation) picks the items it is for (true for all, by default). It is asked twice for each SetDefaults: first with lateInstantiation false, when only type is set (to choose by type), then with true for the globals that said no, after the item's own SetDefaults (to choose by damage, useStyle...).
  • SetDefaults(Item entity) runs after the item's own (the game's, or the ModItem's), for every global that applies.
  • SetStaticDefaults runs once, with every item in the game, before PostSetupContent.

Data per item: InstancePerEntity

By default there is one object of your GlobalItem for the whole game, so it cannot have instance fields: Bunny Loader refuses to load one that does, as tModLoader does. For data per item, ask for an instance per item:

public class KillCounter : GlobalItem
{
    public int Kills;

    public override bool InstancePerEntity => true;
}

// anywhere:
if (item.TryGetGlobalItem(out KillCounter counter)) counter.Kills++;
Call Gives
item.GetGlobalItem<KillCounter>() this item's instance; throws if the global does not apply to it
item.TryGetGlobalItem(out KillCounter counter) true and the instance, or false
item.EntityGlobals every global that applies to the item

A global without InstancePerEntity gives back the same object for every item. Item.Clone() gives the copy a Clone(from, to) of each instance (shallow, as MemberwiseClone; override it to copy lists). To keep that data in the save, override SaveData and LoadData; to send it to the other players in multiplayer, NetSend and NetReceive, as in tModLoader.

A GlobalItem has the same hooks as a ModItem, with the item as the first parameter: use, combat, tooltips, drawing and the rest. The full list is in the API reference.

Every projectile: GlobalProjectile

public class ExampleProjectileModifications : GlobalProjectile
{
    public override bool InstancePerEntity => true;
    public bool applyBuffOnHit;

    public override void OnHitNPC(Projectile projectile, NPC target, NPC.HitInfo hit, int damageDone)
    {
        if (applyBuffOnHit) target.AddBuff(BuffID.OnFire, 50);
    }
}

A GlobalProjectile has the same hooks as ModProjectile, with the Projectile first, plus SetDefaults(Projectile entity). They run for the game's projectiles and the mods'. AppliesToEntity(entity, lateInstantiation) picks the projectiles (first before the projectile's SetDefaults, then after it), and with InstancePerEntity each projectile gets its own instance: projectile.GetGlobalProjectile<T>(), TryGetGlobalProjectile<T>(out T) and EntityGlobals read it. The order is tModLoader's: in PreAI, OnTileCollide, PreKill, PreDrawExtras, PreDraw, CanHitNPC, CanHitPlayer, CanHitPvp, Colliding, GetAlpha and CanDamage the globals answer before the ModProjectile; in AI, PostAI, OnKill, OnSpawn, ModifyHitNPC, OnHitNPC, ModifyHitPlayer, OnHitPlayer and PostDraw the ModProjectile runs first. The loader's ProjectileLoader.PreAI, OnHitNPC and the others are public, as in tModLoader.

projectile.TryGetOwner(out Player player) gives the owning player of a player's projectile.


← Localization

Clone this wiki locally