tModLoader Official tModLoader Help Thread

How to override vanilla npc values such as damage and life?
Make a Global NPC file, use set defaults, check what ID the mob is, use that with an if statement to check id through the use of npc.type. Making a global npc file is like making an npc file for a custom npc but it will affect every npc if done right. Here's an example of a Global NPC file.

You can just c&p that and just use SetDefaults, so you should have

Code:
public override void SetDefaults(NPC npc)
{
    if(npc.type == 1) //Change the 1 to the ID number of the mob you want to edit
    {
       //Some code
    }
}
 
Anybody know a way to stop worm segments from disappearing? Halfway through the fight I find myself fighting a quick burrowing 1,000,000 health worm head, from the boss I made.

It's probably simple, but I'm too stupid to actually find out what it is.
 
Anybody know a way to stop worm segments from disappearing? Halfway through the fight I find myself fighting a quick burrowing 1,000,000 health worm head, from the boss I made.

It's probably simple, but I'm too stupid to actually find out what it is.
Well, all I cna think of is the fact that they're disappearing may be either:

1. Filled up too many of Main.npc array
2. Counter for timeLeft is quickly at 0 for segments.
 
Thanks, I'll try changing some values. On another note, would I be able to make an enemy emit light? If so, how would I do that?
 
Is there a size limit to sprite sheets?
One of my spritesheets is about 1200 x 17000 pixels, (24 frames), but when I use the spritesheet, the animation works fine, but the size of it is very tiny. Using projectile.scale on this just pixelates it.

EDIT: There does seem to be a limit. Used a small sprite sheet and increased the size slowly and after a point, it simply does not increase in its size anymore.
Is there anyway to bypass this?.
 
Last edited:
Thanks, I'll try changing some values. On another note, would I be able to make an enemy emit light? If so, how would I do that?
There are a couple of ways to go about it.
1. Use an invisible projectile that sticks to the npc and has projectile.light set to a value (though this takes up a slot in Main.projectile, so not really recommended)
2. you can just do it with any of these codes:

Code:
Lighting.AddLight(Vector2 position, Vector2 rgb)
Lighting.AddLight(Vector2 position, float r, float b, float g)
Lighting.AddLight(int posX, int posY, float r, float b, float g)

To use them, all you would need to do is add it to the AI hook of your NPC, sort of like this.

Code:
public override void AI()
{
    //Pretty certain it goes into decimals, not whole numbers so something like this
    float r = 50/255;
    float b = 50/255;
    float g = 50/255;

    //Center of the npc
    int positionX = npc.position.X + npc.width/2;
    int positionY = npc.position.Y + npc.height/2;
    //You can just set the positions to npc.Center.X and npc.Center.Y
  
    //Making a Vector2 object with the parameters of positionX and positionY
    Vector2 npcPos = new Vector2(positionX, positionY);
    //You can also just do:
    //- Vector2 npcPos = new Vector2(npc.Center.X, npc.Center.Y)
    //- Vector2 npcPos = npc.Center
    //These will harbor the same values
  
    //Making a Vector3 object with the parameters of r, g, and b
    Vector3 lightColor = new Vector2(r, g, b);
  
    //Using all three different AddLight methods, which basically all does the same thing
    Lighting.AddLight(npcPos, lightColor);
    Lighting.AddLight(npcPos, r, g, b);
    Lighting.AddLight(positionX, positionY, r, g, b);
}

There is however a clear advantage for each one.

Projectile Light: Having a projectile light follow an npc allows the programmer to adjust the brightness of the light, cannot change color, may distort images if too bright, and the fact that it takes up a precious slot in the Main.projectile array
Lighting.AddLight(): Adding color to a point light, however cannot change the brightness of the light, but, it is easier to use
 
Last edited:
Does anyone know how to make a weapon damage the player upon use?
You use this hook inside the item you want to modify to make it damage players on use. So it should look something like this inside your weapon cs file

Code:
public override bool UseItem(Player player)
{
   player.statLife -= 1; Just an example, I'll get a different one later.
   return true; //Makes sure item is used.
}
 
Can somebody go over this method? What does the actually float value represent in this method? Since it requires a return float statement if I wanted to alter the code to only spawn between certain depths or if I wanted to spawn only on a beach? So I am asking for somebody to break down the return statement? what do the two double variables represent (I have guessed time when playing with the values... Only played with the first. Played with to get certain enemies spawning during certain times.)

I have never been a large fan of C# mostly, stick to Java but, the code below keeps getting me lost. The method returns a float for CanSpawn. Using what appears to be an enhanced if statements

public override float CanSpawn(NPCSpawnInfo spawnInfo)
{
return spawnInfo.spawnTileY < Main.rockLayer && Main.dayTime ? 0.5f : 0f; // spawns at day

}

translates to.... ( I think )

if(spawnInfo.spawnTileY < Main.rockLayer 7& Main.dayTime){

return 0.5f;
}

else { return 0f; }
 
Can somebody go over this method? What does the actually float value represent in this method? Since it requires a return float statement if I wanted to alter the code to only spawn between certain depths or if I wanted to spawn only on a beach? So I am asking for somebody to break down the return statement? what do the two double variables represent (I have guessed time when playing with the values... Only played with the first. Played with to get certain enemies spawning during certain times.)

I have never been a large fan of C# mostly, stick to Java but, the code below keeps getting me lost. The method returns a float for CanSpawn. Using what appears to be an enhanced if statements



translates to.... ( I think )
Yeah, your translation of the ternary/conditional/?: expression is correct. As for what the 0.5f and 0f mean, here is the documentation, hope it helps:

Code:
public virtual float CanSpawn(NPCSpawnInfo spawnInfo)

Whether or not this NPC can spawn with the given spawning conditions. Return the weight for the chance of this NPC to spawn compared to vanilla mobs. All vanilla mobs combined have a total weight of 1. Returns 0 by default, which disables natural spawning.
 
How would I make it so that a Town NPC that has moved in would spawn when you re-open a world?

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

namespace Pootis.NPCs
{
    public class SylvarNPC : ModNPC
    {
        public override void SetDefaults()
        {
            npc.name = "Archdemon";
            npc.townNPC = true;
            npc.friendly = true;
            npc.width = 18;
            npc.height = 40;
            npc.aiStyle = 7;
            npc.damage = 10;
            npc.defense = 15;
            npc.lifeMax = 250;
            npc.soundHit = 1;
            npc.soundKilled = 1;
            npc.knockBackResist = 0.5f;
            Main.npcFrameCount[npc.type] = 25;          
            animationType = NPCID.Guide;
        }
        public override bool CanTownNPCSpawn(int numTownNPCs, int money)
        {
            for (int k = 0; k < 255; k++)
            {
                Player player = Main.player[k];
                if (player.active)
                {
                    for (int j = 0; j < player.inventory.Length; j++)
                    {
                        if (player.inventory[j].type == mod.ItemType("NilSword") || player.inventory[j].type == mod.ItemType("ArchSpear") | player.inventory[j].type == mod.ItemType("ThistleBlade"))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }



        public override string TownNPCName()
        {                                        //NPC names
            switch (WorldGen.genRand.Next(4))
            {
                default:
                    return "Sylvar Croati";
            }
        }




        public override string GetChat()
        {                                             //npc chat
        
            switch (Main.rand.Next(3))
            {
                case 0:
                    return "How many wives am I still married to? If I recall, all seven of them...";
                case 1:
                    return "I wish Yenti was here. Or maybe Llin. Or Inti.";
                case 2:
                    return "F--king hell, I just stubbed my toe.";
                default:
                    return "Yes, the canon says I'm a lesbian.";
            }
        }

        public override void SetChatButtons(ref string button, ref string button2)
        {
            button = Lang.inter[28];
            {
                button2 = Lang.inter[1];
            }
        }

        public override void OnChatButtonClicked(bool firstButton, ref bool shop)
        {
            if (firstButton)
            {
                shop = true;
            }
        }

        public override void SetupShop(Chest shop, ref int nextSlot)
        {
            shop.item[nextSlot].SetDefaults(mod.ItemType("NilSword"));
            nextSlot++;
            shop.item[nextSlot].SetDefaults (mod.ItemType("ArchSpear"));
            nextSlot++;
        }

    }
}
 
How would I make it so that a Town NPC that has moved in would spawn when you re-open a world?

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

namespace Pootis.NPCs
{
    public class SylvarNPC : ModNPC
    {
        public override void SetDefaults()
        {
            npc.name = "Archdemon";
            npc.townNPC = true;
            npc.friendly = true;
            npc.width = 18;
            npc.height = 40;
            npc.aiStyle = 7;
            npc.damage = 10;
            npc.defense = 15;
            npc.lifeMax = 250;
            npc.soundHit = 1;
            npc.soundKilled = 1;
            npc.knockBackResist = 0.5f;
            Main.npcFrameCount[npc.type] = 25;         
            animationType = NPCID.Guide;
        }
        public override bool CanTownNPCSpawn(int numTownNPCs, int money)
        {
            for (int k = 0; k < 255; k++)
            {
                Player player = Main.player[k];
                if (player.active)
                {
                    for (int j = 0; j < player.inventory.Length; j++)
                    {
                        if (player.inventory[j].type == mod.ItemType("NilSword") || player.inventory[j].type == mod.ItemType("ArchSpear") | player.inventory[j].type == mod.ItemType("ThistleBlade"))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }



        public override string TownNPCName()
        {                                        //NPC names
            switch (WorldGen.genRand.Next(4))
            {
                default:
                    return "Sylvar Croati";
            }
        }




        public override string GetChat()
        {                                             //npc chat
       
            switch (Main.rand.Next(3))
            {
                case 0:
                    return "How many wives am I still married to? If I recall, all seven of them...";
                case 1:
                    return "I wish Yenti was here. Or maybe Llin. Or Inti.";
                case 2:
                    return "F--king hell, I just stubbed my toe.";
                default:
                    return "Yes, the canon says I'm a lesbian.";
            }
        }

        public override void SetChatButtons(ref string button, ref string button2)
        {
            button = Lang.inter[28];
            {
                button2 = Lang.inter[1];
            }
        }

        public override void OnChatButtonClicked(bool firstButton, ref bool shop)
        {
            if (firstButton)
            {
                shop = true;
            }
        }

        public override void SetupShop(Chest shop, ref int nextSlot)
        {
            shop.item[nextSlot].SetDefaults(mod.ItemType("NilSword"));
            nextSlot++;
            shop.item[nextSlot].SetDefaults (mod.ItemType("ArchSpear"));
            nextSlot++;
        }

    }
}
For town NPC, you want to make sure the classname and npc.name match.
 
If I download somebody else's mod and I wanted to extract the .tmod file so I could view the contents of the mod how would I do that? I want to add some fishing content to the game and wanted to diagnose the code to make my own
 
If I download somebody else's mod and I wanted to extract the .tmod file so I could view the contents of the mod how would I do that? I want to add some fishing content to the game and wanted to diagnose the code to make my own
Tmodreader will do that, provided the original modder allowed for their mod to be unpacked. See tmodloader thread.
 
Hi, I am new to this, but I am getting this error:
"The namespace "items" does not exist in "SWBF" //I am making a Star wars mod :D
Any help? My code is as follows:


using Microsoft.Xna.Framework;
using SWBF.Items;
using System;
using Terraria;
using Terraria.Graphics.Effects;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;

namespace SWBF
{
public class SWBF
{
public SWBF

Properties = new ModProperties()
{
Autoload = true,
AutoloadGores = true,
AutoloadSounds = true
};
}
}
 
Hi, I am new to this, but I am getting this error:
"The namespace "items" does not exist in "SWBF" //I am making a Star wars mod :D
Any help? My code is as follows:


using Microsoft.Xna.Framework;
using SWBF.Items;
using System;
using Terraria;
using Terraria.Graphics.Effects;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;

namespace SWBF
{
public class SWBF
{
public SWBF

Properties = new ModProperties()
{
Autoload = true,
AutoloadGores = true,
AutoloadSounds = true
};
}
}
You don't have any classes in that namespace. Google if you don't know what that means
 
I don't understand why you can't help with that, I googled it and honestly what I read made no sense, and did not pertain to my situation.
You have using SWBF.Items but don't have any classes in that namespace. I can't really help unless you understand a little about what you are doing.
 
I don't understand why you can't help with that, I googled it and honestly what I read made no sense, and did not pertain to my situation.
Your right, I don't have much experience with this, I understand what most of the code means so I have been able to work with it up in till now. I don't know what a class is, and I was looking at the example mod and the code doesn't look that different from mine. Could you lead me in the right direction and tell me what a class is. Ik its probably not what you want to be doing, but please.
[doublepost=1480126588,1480126559][/doublepost]
You have using SWBF.Items but don't have any classes in that namespace. I can't really help unless you understand a little about what you are doing.
Woops replied to myself.
Your right, I don't have much experience with this, I understand what most of the code means so I have been able to work with it up in till now. I don't know what a class is, and I was looking at the example mod and the code doesn't look that different from mine. Could you lead me in the right direction and tell me what a class is. Ik its probably not what you want to be doing, but please.
 
Back
Top Bottom