Skip to content

Drawing

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

Most of the time the game draws your content by itself, from its texture. This page is for when you want to draw something yourself: extra layers on the player, new hairstyles, custom drawing for an NPC, or text in a font of your own.

Drawing the player: PlayerDrawLayer

The player is drawn layer by layer, as in tModLoader: the game's layers are PlayerDrawLayers.Wings, Head, Torso, HeldItem and the rest, and a mod adds its own:

public class ExamplePlayerDrawLayer : PlayerDrawLayer
{
    public override bool IsHeadLayer => true;

    public override bool GetDefaultVisibility(PlayerDrawSet drawInfo) =>
        drawInfo.drawPlayer.HeldItem.type == ModContent.ItemType<ExampleItem>();

    public override Position GetDefaultPosition() => new BeforeParent(PlayerDrawLayers.Head);

    protected override void Draw(ref PlayerDrawSet drawInfo)
    {
        var texture = TextureAssets.Item[ModContent.ItemType<ExampleItem>()];
        var position = drawInfo.Center + new Vector2(0f, -20f) - Main.screenPosition;
        drawInfo.DrawDataCache.Add(new DrawData(texture.Value, position, null, Color.White, 0f, texture.Size() * 0.5f, 1f, SpriteEffects.None, 0));
    }
}

GetDefaultPosition takes Between(layer1, layer2) (either may be null), BeforeParent/AfterParent (drawn with the parent, and hidden with it) or Multiple. The order is set once after loading (PlayerDrawLayerLoader.DrawOrder). drawInfo.DrawDataCache works as tModLoader's list (Add, Insert, RemoveAt, RemoveAll, FindIndex, Count, the indexer, foreach) over the phone's array and count, and DrawData has tModLoader's constructors (a null source rectangle is the whole texture). TextureAssets.Item[type] is an Asset<Texture2D> and loads the game's texture when it is not loaded yet.

The phone draws the same layers as the PC 1.4.5, which tModLoader (1.4.4) does not all have: there is PlayerDrawLayers.ArmorBackCoat (between BalloonAcc and Skin) and no FinchNest, and HeldItem behind the front arm and FrontAccFront over the arm keep the game's torso offset.

Hook When
DrawEffects(drawInfo, ref r, ref g, ref b, ref a, ref fullBright) after PlayerDrawSet.BoringSetup; the player's colors are multiplied, and fullBright ignores the light
ModifyDrawInfo(ref drawInfo) right after it
ModifyDrawLayerOrdering(positions) once, when the order is built
HideDrawLayers(drawInfo) every draw, before the layers (call layer.Hide())
TransformDrawData(ref drawInfo) before DrawPlayer_RenderAllLayers, with every layer's DrawData
DrawPlayer(camera) after LegacyPlayerRenderer.PrepareDrawForFrame, once per player per frame
ModifyScreenPosition() after Main.DoDraw_UpdateCameraPosition, for the local player
ModifyZoom(ref zoom) when the game sets Main.GameViewMatrix.Zoom

Hairstyles: ModHair

public class ExampleHair : ModHair
{
    public override bool AvailableDuringCharacterCreation => false;

    public override void SetStaticDefaults() => HairID.Sets.DrawBackHair[Type] = true;

    public override IEnumerable<Condition> GetUnlockConditions()
    {
        yield return Condition.TimeNight;
    }
}

The textures are Texture and AltTexture (ExampleHair.png and ExampleHair_Alt.png, the one under a hat), set in AutoStaticDefaults into TextureAssets.PlayerHair/PlayerHairAlt (now Asset<Texture2D> arrays, as in tModLoader). The types come after the game's 228 (HairID.Count, HairLoader.Count, HairLoader.GetHair). The hair shows at character creation when AvailableDuringCharacterCreation, and at the Stylist when every GetUnlockConditions condition is met. HairID.Sets.DrawBackHair decides the back hair, the game's hairs included.

The game's player file keeps the hair as a number the game checks against its 228, so a mod hair is saved there as 0 and by name in the character's .plr.bl.json (bunny:hair). In multiplayer the game's appearance message turns a mod hair into 0; the Bunny Loader sends the real one right after it. RandomizedCharacterCreationGender decides the gender the character creation's random button picks along with the hair.

NPC drawing

Hook Behavior
DrawEffects(ref Color drawColor) Changes the light color before the NPC is drawn. The mod NPC runs first, then its globals. The game's own draw uses the new color too.
PreDraw(SpriteBatch spriteBatch, Vector2 screenPos, Color drawColor) false skips the game's drawing of the NPC. The globals run first, then the mod NPC. A false from a global skips the mod NPC.
PostDraw(SpriteBatch spriteBatch, Vector2 screenPos, Color drawColor) Runs after the game's drawing, even when PreDraw returned false. The mod NPC runs first, then its globals.
GetAlpha(Color drawColor) Returns Color?: the first non-null answer replaces the game's color. The globals run first, then the mod NPC. A bestiary icon receives white.
DrawBehind(int index) After the game sorts the NPCs into its draw lists (Main.CacheNPCDraws). Set NPC.behindTiles or NPC.hide there to move or hide the NPC. The Main.instance.DrawCacheNPCs* lists are raw game objects for now (generic List<int> proxies are not generated yet).
BossHeadSlot(ref int index), BossHeadRotation(ref float rotation), BossHeadSpriteEffects(ref SpriteEffects spriteEffects) Change the icon that the boss bar and the map use.

The hooks run for the game's NPCs too, through globals, and in the bestiary. screenPos is the screen position to subtract from world coordinates. On the phone the sprite batch groups draws by texture. An NPC that overrides PreDraw or PostDraw gets a batch of its own, so its glow stays on top of its body and does not mix with another NPC of the same type.

A file named after BossHeadTexture (by default Texture + "_Head_Boss") becomes the NPC's boss icon on its own. Other icons are added in Load with Mod.AddBossHeadTexture("ModName/Path", npcType), which returns the slot. ModContent.GetModBossHeadSlot("ModName/Path") finds a slot later, for BossHeadSlot.

A mod's own font

tModLoader requests a DynamicSpriteFont; on mobile the game's font class is SpriteFont, and that is what a C# mod requests:

using Microsoft.Xna.Framework.Graphics;

SpriteFont font = ModContent.Request<SpriteFont>("MyMod/Assets/Fonts/MyFont").Value;
Utils.DrawBorderStringFourWay(spriteBatch, font, "CPM", x, y, Color.Gold, Color.Black, Vector2.Zero, 0.9f);
  • The file is a blfont/1 JSON with its pages (MyFont.json and MyFont.0.png), anywhere in the mod; Assets/Fonts is the convention. Build it from a .ttf with the measurements of a game font:

    python tools/mods/fonts.py ttf "MyFont.ttf" --only Mouse_Text --name MyFont --out "MyMod/Assets/Fonts"
  • Value is a game SpriteFont: it works with SpriteBatch.DrawString, Utils.DrawBorderString*, MeasureString and the UI elements.

  • It starts as a copy of the game's MouseText, so characters the file lacks use the game's letters, and it keeps the file's lineSpacing and spacing.

  • It loads on the game thread once the content is ready: PostSetupContent, a UI built there, or a hook. Earlier, Value throws.

  • The same file gives the same asset (Mod.Assets.Request<SpriteFont> too); ModContent.HasAsset finds fonts; a missing file throws FileNotFoundException.

  • Font packs change the game's fonts, not the copies mods made.

  • The game's fonts by name: ModContent.Request<SpriteFont>("Terraria/Fonts/Mouse_Text") (also Item_Stack and Death_Text) is the same asset as FontAssets.MouseText.

The Example Mod draws the "CPM" label of its coins panel (/coins) with New Rocker: ExampleCoinsUI.cs and Assets/Fonts.


← User interface  ·  Screen effects →

Clone this wiki locally