APIリファレンス

Complete reference for tModLoader's C# API with examples and best practices

APIリファレンス 全レベル v2025.07.3.0

コアクラス

ModItem

カスタムアイテムを作成するための基本クラス. Inherit from ModItem to create weapons, tools, accessories, and consumables.

主要メソッド:

SetDefaults()

アイテムプロパティを設定 like damage, size, rarity, and use style

AddRecipes()

製作レシピを定義 for your item

UseItem(Player player)

アイテム使用時のカスタム動作

実装例:

public class MagicSword : ModItem
{
    public override void SetDefaults()
    {
        Item.damage = 80;
        Item.DamageType = DamageClass.Magic;
        Item.mana = 10;
        Item.width = 44;
        Item.height = 44;
        Item.useTime = 25;
        Item.useAnimation = 25;
        Item.useStyle = ItemUseStyleID.Shoot;
        Item.knockBack = 5;
        Item.value = Item.buyPrice(gold: 5);
        Item.rare = ItemRarityID.Pink;
        Item.UseSound = SoundID.Item20;
        Item.autoReuse = true;
        Item.shoot = ProjectileID.MagicMissile;
        Item.shootSpeed = 16f;
    }
}

ModNPC

カスタムNPCを作成 including enemies, town NPCs, and bosses with custom AI and behavior.

必須メソッド:

SetDefaults()

NPCのステータスとプロパティを設定

AI()

カスタムAI動作ロジック

ModifyNPCLoot()

ドロップテーブルと戦利品を定義

OnKill()

NPC撃破時のアクション

public class ExampleEnemy : ModNPC
{
    public override void SetDefaults()
    {
        NPC.width = 40;
        NPC.height = 56;
        NPC.damage = 15;
        NPC.defense = 6;
        NPC.lifeMax = 100;
        NPC.HitSound = SoundID.NPCHit1;
        NPC.DeathSound = SoundID.NPCDeath2;
        NPC.value = 60f;
        NPC.knockBackResist = 0.5f;
        NPC.aiStyle = NPCAIStyleID.Fighter;
        AIType = NPCID.Zombie;
    }
}

一般的な開発パターン

レシピの作成

public override void AddRecipes()
{
    Recipe recipe = CreateRecipe();
    recipe.AddIngredient(ItemID.IronBar, 5);
    recipe.AddIngredient(ItemID.Wood, 10);
    recipe.AddIngredient(ModContent.ItemType<CustomItem>(), 1);
    recipe.AddTile(TileID.WorkBenches);
    recipe.AddCondition(Recipe.Condition.InGraveyard);
    recipe.Register();
}

Recipes are automatically synced in multiplayer and appear in recipe browsers.

カスタム飛び道具

public class MagicBolt : ModProjectile
{
    public override void SetDefaults()
    {
        Projectile.width = 16;
        Projectile.height = 16;
        Projectile.friendly = true;
        Projectile.penetrate = 3;
        Projectile.timeLeft = 600;
        Projectile.light = 0.5f;
        Projectile.DamageType = DamageClass.Magic;
    }

    public override void AI()
    {
        // Custom movement and visual effects
        Projectile.rotation += 0.4f;

        if (Main.rand.NextBool(3))
        {
            Dust.NewDust(Projectile.position, Projectile.width,
                        Projectile.height, DustID.Electric);
        }
    }
}

グローバル変更

既存のゲームコンテンツを変更 using Global classes:

public class GlobalItemChanges : GlobalItem
{
    public override void SetStaticDefaults()
    {
        // Modify all items of a specific type
    }

    public override void ModifyTooltips(Item item, List<TooltipLine> tooltips)
    {
        if (item.type == ItemID.TerraBlade)
        {
            tooltips.Add(new TooltipLine(Mod, "CustomTooltip",
                        "Enhanced by YourMod!"));
        }
    }
}

ベストプラクティス

Do's

  • Use ModContent.ItemType<T>() for referencing custom items
  • Implement proper null checking
  • Use meaningful variable and class names
  • Test in multiplayer environment
  • Follow C# naming conventions
  • Document your code with comments

Don'ts

  • Don't hardcode item IDs - use constants
  • Don't access Main.player directly in multiplayer
  • Don't forget to handle edge cases
  • Don't use heavy operations in AI() methods
  • Don't modify vanilla content unnecessarily
  • Don't ignore compiler warnings

便利なユーティリティ&ヘルパー

一般的なヘルパーメソッド

// Get item type from mod item class
int itemType = ModContent.ItemType<MyCustomItem>();

// Check if mod is loaded
bool calamityLoaded = ModLoader.HasMod("CalamityMod");

// Create dust effects
Dust.NewDust(position, width, height, DustID.Electric);

// Spawn projectiles
Projectile.NewProjectile(source, position, velocity, type, damage, knockback);

// Play sounds
SoundEngine.PlaySound(SoundID.Item1, position);

ネットワーキングヘルパー

// Send data to server
ModPacket packet = Mod.GetPacket();
packet.Write((byte)MessageType.ExampleMessage);
packet.Write(someData);
packet.Send();

// Handle received packets
public override void HandlePacket(BinaryReader reader, int whoAmI)
{
    MessageType msgType = (MessageType)reader.ReadByte();
    switch (msgType)
    {
        case MessageType.ExampleMessage:
            // Handle message
            break;
    }
}

外部リソース