tModLoader Official tModLoader Help Thread

Here it is:
Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;

namespace Firstmod.Items.NPCs          //We need thiFs to basically indicate the folder where it is to be read from, so you the texture will load correctly
{
    public class PlagueDoctor : ModNPC
    {
        public override bool Autoload(ref string name)
        {
            name = "PlagueDoctor";
            return mod.Properties.Autoload;
        }

        public override void SetDefaults()
        {
            npc.townNPC = true; //This defines if the npc is a town Npc or not
            npc.friendly = true;  //this defines if the npc can hur you or not()
            npc.width = 18; //the npc sprite width
            npc.height = 46;  //the npc sprite height
            npc.aiStyle = 7; //this is the npc ai style, 7 is Pasive Ai
            npc.defense = 25;  //the npc defense
            npc.lifeMax = 250;// the npc life
            npc.HitSound = SoundID.NPCHit1;  //the npc sound when is hit
            npc.DeathSound = SoundID.NPCDeath1;  //the npc sound when he dies
            npc.knockBackResist = 0.5f;  //the npc knockback resistance
            Main.npcFrameCount[npc.type] = 6; //this defines how many frames the npc sprite sheet has
            NPCID.Sets.ExtraFramesCount[npc.type] = 0;
            NPCID.Sets.AttackFrameCount[npc.type] = 0;
            NPCID.Sets.DangerDetectRange[npc.type] = 150; //this defines the npc danger detect range
            NPCID.Sets.AttackType[npc.type] = 1; //this is the attack type,  0 (throwing), 1 (shooting), or 2 (magic). 3 (melee)
            NPCID.Sets.AttackTime[npc.type] = 30; //this defines the npc attack speed
            NPCID.Sets.AttackAverageChance[npc.type] = 10;//this defines the npc atack chance
            NPCID.Sets.HatOffsetY[npc.type] = 4; //this defines the party hat position
            animationType = NPCID.Guide;  //this copy the cyborg animation
        }
        public override string TownNPCName()     //Allows you to give this town NPC any name when it spawns
        {
            switch (WorldGen.genRand.Next(1))
            {
                case 0:
                    return "The Plague Doctor";
                default:
                    return "The PLague Doctor";
            }
        }

        public override void SetChatButtons(ref string button, ref string button2)  //Allows you to set the text for the buttons that appear on this town NPC's chat window.
        {
            button = "Buy Potions";   //this defines the buy button name
        }
        public override void OnChatButtonClicked(bool firstButton, ref bool openShop) //Allows you to make something happen whenever a button is clicked on this town NPC's chat window. The firstButton parameter tells whether the first button or second button (button and button2 from SetChatButtons) was clicked. Set the shop parameter to true to open this NPC's shop.
        {

            if (firstButton)
            {
                openShop = true;   //so when you click on buy button opens the shop
            }
        }

        public override void SetupShop(Chest shop, ref int nextSlot)       //Allows you to add items to this town NPC's shop. Add an item by setting the defaults of shop.item[nextSlot] then incrementing nextSlot.
        {
            if (NPC.downedMechBossAny)   //this make so when the king slime is killed the town npc will sell this
            {
                shop.item[nextSlot].SetDefaults(ItemID.GreaterHealingPotion);  //an example of how to add a vanilla terraria item
                nextSlot++;
                shop.item[nextSlot].SetDefaults(ItemID.GreaterManaPotion);
                nextSlot++;
            }
            if (NPC.downedQueenBee)   //this make so when Skeletron is killed the town npc will sell this
            {
                shop.item[nextSlot].SetDefaults(ItemID.HealingPotion);
                nextSlot++;
                shop.item[nextSlot].SetDefaults(ItemID.ManaPotion);
                nextSlot++;
            }
            shop.item[nextSlot].SetDefaults(ItemID.LesserHealingPotion);
            nextSlot++;
            shop.item[nextSlot].SetDefaults(mod.ItemType("Thechefknife"));  //this is an example of how to add a modded item
            nextSlot++;

        }
        public override bool CanTownNPCSpawn(int numTownNPCs, int money)
        {

            for (int k = 0; k < 255; k++)
            {
                Player player = Main.player[k];
                if (!player.active)
                {
                    if (Main.hardMode)
                    {
                        return true;
                    }
                    return false;
                }
                return false;


            }
        }
           
       

        public override string GetChat()       //Allows you to give this town NPC a chat message when a player talks to it.
        {
            switch (Main.rand.Next(4))    //this are the messages when you talk to the npc
            {
                case 0:
                    return "Ah, please keep your distance";
                case 1:
                    return "Medical school..? I beg your pardon?";
                case 2:
                    return "Please refrain from touching.";
                case 3:
                    return "That modern nurse has no respect for my practices.";
                default:
                    return "We'll settle this later.";

            }
        }
        public override void TownNPCAttackStrength(ref int damage, ref float knockback)//  Allows you to determine the damage and knockback of this town NPC attack
        {
            damage = 40;  //npc damage
            knockback = 2f;   //npc knockback
        }

        public override void TownNPCAttackCooldown(ref int cooldown, ref int randExtraCooldown)  //Allows you to determine the cooldown between each of this town NPC's attack. The cooldown will be a number greater than or equal to the first parameter, and less then the sum of the two parameters.
        {
            cooldown = 5;
            randExtraCooldown = 10;
        }
        //------------------------------------This is an example of how to make the npc use a sward attack-------------------------------
        public override void DrawTownAttackSwing(ref Texture2D item, ref int itemSize, ref float scale, ref Vector2 offset)//Allows you to customize how this town NPC's weapon is drawn when this NPC is swinging it (this NPC must have an attack type of 3). Item is the Texture2D instance of the item to be drawn (use Main.itemTexture[id of item]), itemSize is the width and height of the item's hitbox
        {
            scale = 1f;
            item = Main.itemTexture[mod.ItemType("TheSpatula")]; //this defines the item that this npc will use
            itemSize = 56;
        }

        public override void TownNPCAttackSwing(ref int itemWidth, ref int itemHeight) //  Allows you to determine the width and height of the item this town NPC swings when it attacks, which controls the range of this NPC's swung weapon.
        {
            itemWidth = 56;
            itemHeight = 56;
        }

        //----------------------------------This is an example of how to make the npc use a gun and a projectile ----------------------------------
        /*public override void DrawTownAttackGun(ref float scale, ref int item, ref int closeness) //Allows you to customize how this town NPC's weapon is drawn when this NPC is shooting (this NPC must have an attack type of 1). Scale is a multiplier for the item's drawing size, item is the ID of the item to be drawn, and closeness is how close the item should be drawn to the NPC.
          {
              scale = 1f;
              item = mod.ItemType("GunName");
              closeness = 20;
          }
          public override void TownNPCAttackProj(ref int projType, ref int attackDelay)//Allows you to determine the projectile type of this town NPC's attack, and how long it takes for the projectile to actually appear
          {
              projType = ProjectileID.CrystalBullet;
              attackDelay = 1;
          }

          public override void TownNPCAttackProjSpeed(ref float multiplier, ref float gravityCorrection, ref float randomOffset)//Allows you to determine the speed at which this town NPC throws a projectile when it attacks. Multiplier is the speed of the projectile, gravityCorrection is how much extra the projectile gets thrown upwards, and randomOffset allows you to randomize the projectile's velocity in a square centered around the original velocity
          {
              multiplier = 7f;
             // randomOffset = 4f;

          }   */

    }
}
Sorry if its messy
 
Here it is:
Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria.ID;
using Terraria;
using Terraria.ModLoader;

namespace Firstmod.Items.NPCs          //We need thiFs to basically indicate the folder where it is to be read from, so you the texture will load correctly
{
    public class PlagueDoctor : ModNPC
    {
        public override bool Autoload(ref string name)
        {
            name = "PlagueDoctor";
            return mod.Properties.Autoload;
        }

        public override void SetDefaults()
        {
            npc.townNPC = true; //This defines if the npc is a town Npc or not
            npc.friendly = true;  //this defines if the npc can hur you or not()
            npc.width = 18; //the npc sprite width
            npc.height = 46;  //the npc sprite height
            npc.aiStyle = 7; //this is the npc ai style, 7 is Pasive Ai
            npc.defense = 25;  //the npc defense
            npc.lifeMax = 250;// the npc life
            npc.HitSound = SoundID.NPCHit1;  //the npc sound when is hit
            npc.DeathSound = SoundID.NPCDeath1;  //the npc sound when he dies
            npc.knockBackResist = 0.5f;  //the npc knockback resistance
            Main.npcFrameCount[npc.type] = 6; //this defines how many frames the npc sprite sheet has
            NPCID.Sets.ExtraFramesCount[npc.type] = 0;
            NPCID.Sets.AttackFrameCount[npc.type] = 0;
            NPCID.Sets.DangerDetectRange[npc.type] = 150; //this defines the npc danger detect range
            NPCID.Sets.AttackType[npc.type] = 1; //this is the attack type,  0 (throwing), 1 (shooting), or 2 (magic). 3 (melee)
            NPCID.Sets.AttackTime[npc.type] = 30; //this defines the npc attack speed
            NPCID.Sets.AttackAverageChance[npc.type] = 10;//this defines the npc atack chance
            NPCID.Sets.HatOffsetY[npc.type] = 4; //this defines the party hat position
            animationType = NPCID.Guide;  //this copy the cyborg animation
        }
        public override string TownNPCName()     //Allows you to give this town NPC any name when it spawns
        {
            switch (WorldGen.genRand.Next(1))
            {
                case 0:
                    return "The Plague Doctor";
                default:
                    return "The PLague Doctor";
            }
        }

        public override void SetChatButtons(ref string button, ref string button2)  //Allows you to set the text for the buttons that appear on this town NPC's chat window.
        {
            button = "Buy Potions";   //this defines the buy button name
        }
        public override void OnChatButtonClicked(bool firstButton, ref bool openShop) //Allows you to make something happen whenever a button is clicked on this town NPC's chat window. The firstButton parameter tells whether the first button or second button (button and button2 from SetChatButtons) was clicked. Set the shop parameter to true to open this NPC's shop.
        {

            if (firstButton)
            {
                openShop = true;   //so when you click on buy button opens the shop
            }
        }

        public override void SetupShop(Chest shop, ref int nextSlot)       //Allows you to add items to this town NPC's shop. Add an item by setting the defaults of shop.item[nextSlot] then incrementing nextSlot.
        {
            if (NPC.downedMechBossAny)   //this make so when the king slime is killed the town npc will sell this
            {
                shop.item[nextSlot].SetDefaults(ItemID.GreaterHealingPotion);  //an example of how to add a vanilla terraria item
                nextSlot++;
                shop.item[nextSlot].SetDefaults(ItemID.GreaterManaPotion);
                nextSlot++;
            }
            if (NPC.downedQueenBee)   //this make so when Skeletron is killed the town npc will sell this
            {
                shop.item[nextSlot].SetDefaults(ItemID.HealingPotion);
                nextSlot++;
                shop.item[nextSlot].SetDefaults(ItemID.ManaPotion);
                nextSlot++;
            }
            shop.item[nextSlot].SetDefaults(ItemID.LesserHealingPotion);
            nextSlot++;
            shop.item[nextSlot].SetDefaults(mod.ItemType("Thechefknife"));  //this is an example of how to add a modded item
            nextSlot++;

        }
        public override bool CanTownNPCSpawn(int numTownNPCs, int money)
        {

            for (int k = 0; k < 255; k++)
            {
                Player player = Main.player[k];
                if (!player.active)
                {
                    if (Main.hardMode)
                    {
                        return true;
                    }
                    return false;
                }
                return false;


            }
        }
         
     

        public override string GetChat()       //Allows you to give this town NPC a chat message when a player talks to it.
        {
            switch (Main.rand.Next(4))    //this are the messages when you talk to the npc
            {
                case 0:
                    return "Ah, please keep your distance";
                case 1:
                    return "Medical school..? I beg your pardon?";
                case 2:
                    return "Please refrain from touching.";
                case 3:
                    return "That modern nurse has no respect for my practices.";
                default:
                    return "We'll settle this later.";

            }
        }
        public override void TownNPCAttackStrength(ref int damage, ref float knockback)//  Allows you to determine the damage and knockback of this town NPC attack
        {
            damage = 40;  //npc damage
            knockback = 2f;   //npc knockback
        }

        public override void TownNPCAttackCooldown(ref int cooldown, ref int randExtraCooldown)  //Allows you to determine the cooldown between each of this town NPC's attack. The cooldown will be a number greater than or equal to the first parameter, and less then the sum of the two parameters.
        {
            cooldown = 5;
            randExtraCooldown = 10;
        }
        //------------------------------------This is an example of how to make the npc use a sward attack-------------------------------
        public override void DrawTownAttackSwing(ref Texture2D item, ref int itemSize, ref float scale, ref Vector2 offset)//Allows you to customize how this town NPC's weapon is drawn when this NPC is swinging it (this NPC must have an attack type of 3). Item is the Texture2D instance of the item to be drawn (use Main.itemTexture[id of item]), itemSize is the width and height of the item's hitbox
        {
            scale = 1f;
            item = Main.itemTexture[mod.ItemType("TheSpatula")]; //this defines the item that this npc will use
            itemSize = 56;
        }

        public override void TownNPCAttackSwing(ref int itemWidth, ref int itemHeight) //  Allows you to determine the width and height of the item this town NPC swings when it attacks, which controls the range of this NPC's swung weapon.
        {
            itemWidth = 56;
            itemHeight = 56;
        }

        //----------------------------------This is an example of how to make the npc use a gun and a projectile ----------------------------------
        /*public override void DrawTownAttackGun(ref float scale, ref int item, ref int closeness) //Allows you to customize how this town NPC's weapon is drawn when this NPC is shooting (this NPC must have an attack type of 1). Scale is a multiplier for the item's drawing size, item is the ID of the item to be drawn, and closeness is how close the item should be drawn to the NPC.
          {
              scale = 1f;
              item = mod.ItemType("GunName");
              closeness = 20;
          }
          public override void TownNPCAttackProj(ref int projType, ref int attackDelay)//Allows you to determine the projectile type of this town NPC's attack, and how long it takes for the projectile to actually appear
          {
              projType = ProjectileID.CrystalBullet;
              attackDelay = 1;
          }

          public override void TownNPCAttackProjSpeed(ref float multiplier, ref float gravityCorrection, ref float randomOffset)//Allows you to determine the speed at which this town NPC throws a projectile when it attacks. Multiplier is the speed of the projectile, gravityCorrection is how much extra the projectile gets thrown upwards, and randomOffset allows you to randomize the projectile's velocity in a square centered around the original velocity
          {
              multiplier = 7f;
             // randomOffset = 4f;

          }   */

    }
}
Sorry if its messy
Try adding return false; at the end of
C#:
            for (int k = 0; k < 255; k++)

            {

                Player player = Main.player[k];

                if (!player.active)

                {

                    if (Main.hardMode)

                    {

                        return true;

                    }

                    return false;

                }

                return false;

            }
So it would instead be like this:
C#:
            for (int k = 0; k < 255; k++)

            {

                Player player = Main.player[k];

                if (!player.active)

                {

                    if (Main.hardMode)

                    {

                        return true;

                    }

                    return false;

                }

                return false;
            }

            return false;
 
Hmmm, I suppose I should make a Basic Town Npc tutorial for coding beginners. I am not a coding expert, but I have been coding for a month now.
 
ok here's my Player class... What is causing the no suitable way to override?
Code:
using DepressionCollection.Tiles;
using DepressionCollection.Items;
using DepressionCollection.Items.Accessories;
using DepressionCollection.Projectiles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using static Terraria.ModLoader.ModContent;

namespace DepressionCollection
{
    public class MyPlayer : ModPlayer
    {
        public bool vampCharm;

        public override void ResetEffects()
        {
            vampCharm = false;
        }

        public override void OnHitNPC(Player player, NPC target, int damage, float knockBack, bool crit)
        {
            if (vampCharm)
            {
                int amountToHeal = damage / 10; // Heal tenth of damage
                if (amountToHeal + player.statLife > player.statLifeMax2)
                    amountToHeal = player.statLifeMax2 - player.statLife; // If healing is larger than health currently missing.
                player.statLife += amountToHeal;
            }
        }

    }
}

btw i dont really know that much c sharp but if i replace override to virtual there's no error but it doesnt run...
 
ok here's my Player class... What is causing the no suitable way to override?
Code:
using DepressionCollection.Tiles;
using DepressionCollection.Items;
using DepressionCollection.Items.Accessories;
using DepressionCollection.Projectiles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.DataStructures;
using Terraria.GameInput;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;
using static Terraria.ModLoader.ModContent;

namespace DepressionCollection
{
    public class MyPlayer : ModPlayer
    {
        public bool vampCharm;

        public override void ResetEffects()
        {
            vampCharm = false;
        }

        public override void OnHitNPC(Player player, NPC target, int damage, float knockBack, bool crit)
        {
            if (vampCharm)
            {
                int amountToHeal = damage / 10; // Heal tenth of damage
                if (amountToHeal + player.statLife > player.statLifeMax2)
                    amountToHeal = player.statLifeMax2 - player.statLife; // If healing is larger than health currently missing.
                player.statLife += amountToHeal;
            }
        }

    }
}
Remember to replace 'override' with 'virtual' if that error happens.
 
if i replace override with virtual the code doesn't do anything... or something... i doesnt crash tho so its better :/
I am busy making a guide, tmodloader should give you the line of code the error is located in. For example: My Documents/My Games/Terraria/Tmodloader/Mod Sources/ModName/ModPlayer.cs(9, 12) Error CS1002: ; expected.
So I know that it is on Line 9 and word/Column 12

1
2
3
4
5
6
7
8
9 npc.aiStyle = 34 // missing ;
10
11
12
13
14 }
 
Last edited:
if i replace override with virtual the code doesn't do anything... or something... i doesnt crash tho so its better :/
My mistake, I thought that your code was in ModItem for some reason :/
Working code:
Code:
public override void OnHitNPC(Item item, NPC target, int damage, float knockback, bool crit)
{
    if (vampCharm)
    {
        int amountToHeal = damage / 10; // Heal tenth of damage
        if (amountToHeal + player.statLife > player.statLifeMax2)
            amountToHeal = player.statLifeMax2 - player.statLife; // If healing is larger than health currently missing.
        player.statLife += amountToHeal;
        player.HealEffect(amountToHeal); // I'm assuming you want the healing effect (text) ?
    }
}
 
Ok, I used hero mod to manually spawn the NPC and it worked! Now I just need the NPC to be able to spawn automatically.
Try this:
Code:
        public override bool CanTownNPCSpawn(int numTownNPCs, int money)
        {
            for (int k = 0; k < 255; k++)
            {
                Player player = Main.player[k];
                if (!player.active)
                {
                    return false;
                }
            }
           
            if (Main.hardMode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Before, you were returning false immediately after entering the loop, regardless of whether if(!player.active) was entered
This code will check if all of the players are active, and if they are, then it returns false.
Otherwise, it checks if it's hardmode, if it is, it returns true.
I'm not sure you need to check if all of the players are active though, because otherwise your NPC won't be able to move in unless ALL players are active. And I don't think that players that aren't in the game count as active, so it might just be best to remove the loop and it's contents
 
Thank you, and I'm also having a sprite issue. Does anybody know how Tmodloader registers different frames for different town NPC actions(i.e.Attacking, walking, and talking?) My animation for my town NPC is severely glitchy and is using the attack animation during the walk animation. Is this sprite sheet correct?
Plague_Doctor_Spritesheet.png
 
Thank you, and I'm also having a sprite issue. Does anybody know how Tmodloader registers different frames for different town NPC actions(i.e.Attacking, walking, and talking?) My animation for my town NPC is severely glitchy and is using the attack animation during the walk animation. Is this sprite sheet correct?
Plague_Doctor_Spritesheet.png
Have a look at Vanilla sprite sheets, and also have a look at how other mods have done it. tModLoader will likely do them the same as Terraria handles it's NPC sprites, so using that should get you what you want. I haven't made an NPC, so I can't help too much here
 
Back
Top Bottom