Standalone [1.3] tModLoader - A Modding API

Solo-Ion

Dungeon Spirit
How can I make an Item to be either crafted with an iron bar or a lead bar, like the craftinggroup method?
I don't want to add two recipes, and since crafting group method was removed, I am confused what to do.
Terraria has a built in craft group for iron or lead. Just do the following.
recipe1.AddRecipeGroup("IronBar", 3); //vanilla recipe group - adds 3
A custom craft group looks like the following.
Code:
public override void AddRecipeGroups()
{
    //Add the silver ranked group
    RecipeGroup group = new RecipeGroup(() => Lang.misc[37] + " Silver", new int[]
    {
        ItemID.SilverBar,
        ItemID.TungstenBar
    });
    RecipeGroup.RegisterGroup("rankSilver", group); //creates a group called "rankSilver". Use recipe.AddRecipeGroup("rankSilver", 1); to use this group in a recipe
}

I have a big problem now :(
the projectile just goes out a little distance and then stops until there is something to home in on. then it homes.
EDIT: Then it just sits there and never homes
Code:
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Microsoft.Xna.Framework;

namespace Pack.Projectiles
{
    public class TS : ModProjectile
    {
        public override void SetDefaults()
        {
            projectile.name = "Templite Spell";
            projectile.width = 8;
            projectile.height = 8;
            projectile.friendly = true;
            projectile.penetrate = 10;
            projectile.aiStyle = 0;
            projectile.magic = true;
            projectile.tileCollide = true;
            projectile.timeLeft = 60000;
            projectile.light = 10f;
        }
        public NPC FindNearest(Vector2 pos)
        {
            NPC nearest = null;
            float oldDist = 1001;
            float newDist = 1000;
            for (int i = 0; i < Terraria.Main.npc.Length - 1; i++)
            {
                if (!Collision.CanHit(pos, 1, 1, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height))
                    continue;
                if (!Terraria.Main.npc[i].CanBeChasedBy(projectile))
                    continue;
                if (nearest == null)
                    nearest = Terraria.Main.npc[i];
                else
                {
                    oldDist = Vector2.Distance(pos, nearest.position);
                    newDist = Vector2.Distance(pos, Terraria.Main.npc[i].position);
                    if (newDist < oldDist)
                        nearest = Terraria.Main.npc[i];
                }
            }
            return nearest;
        }
        int maxSpeed = 10;
        public override void AI()
        {
            NPC nearest = this.FindNearest(projectile.Center);
            Vector2 acceleration = Vector2.Zero;
            projectile.velocity += acceleration * 1.5f;
            projectile.velocity *= 0.95f;
            if (projectile.velocity.Length() > maxSpeed)
            {
                projectile.velocity.Normalize();
                projectile.velocity *= maxSpeed;
            }
            projectile.rotation = projectile.velocity.ToRotation();
        }
    }
}

where's the mistake in my code?
please help
Well first, I found the problem that was causing the slow down. You need to swap the two if conditions inside the findNearest function so that your projectile isn't checking collision with all 200 non-existent NPC's in the world. And your projectile wasn't homing because you had removed the position of its target from the equation, so that it never knew where to home to. Also, you forgot to check if your 'nearest' NPC was null, which would cause the projectile to disappear. I've fixed your AI for you.
Code:
public override void AI()
{
    NPC nearest = this.FindNearest(projectile.Center);
    Vector2 acceleration;
    if (nearest != null) //this is really important
    {
        acceleration = (nearest.position - projectile.position);
    } else
    {
        acceleration = Vector2.Zero; //maintain speed -- or whatever else you want to do here
    }
    projectile.velocity += acceleration * 1.5f;
    projectile.velocity *= 0.99f;
    if (projectile.velocity.Length() > maxSpeed)
    {
        projectile.velocity.Normalize();
        projectile.velocity *= maxSpeed;
    }
    projectile.rotation = projectile.velocity.ToRotation();
}
Also, it looks like you've been removing my comments from the code. It's a good idea to leave those there so that the code is easier to interpret.
 

Itorius

Skeletron Prime
Ok, so when I tried to use the LoadCustomData I got the "Unable to read beyond the end of the stream" error.
Here's the code
Code:
 public override void LoadCustomData(BinaryReader reader)
{
            int loadVersion = reader.ReadInt32();
            item.pick = reader.ReadInt32();
            item.damage = reader.ReadInt32();
            item.useTime = reader.ReadInt32();
        }
 

Marf Master

Skeletron Prime
Terraria has a built in craft group for iron or lead. Just do the following.
recipe1.AddRecipeGroup("IronBar", 3); //vanilla recipe group - adds 3
A custom craft group looks like the following.
Code:
public override void AddRecipeGroups()
{
    //Add the silver ranked group
    RecipeGroup group = new RecipeGroup(() => Lang.misc[37] + " Silver", new int[]
    {
        ItemID.SilverBar,
        ItemID.TungstenBar
    });
    RecipeGroup.RegisterGroup("rankSilver", group); //creates a group called "rankSilver". Use recipe.AddRecipeGroup("rankSilver", 1); to use this group in a recipe
}


Well first, I found the problem that was causing the slow down. You need to swap the two if conditions inside the findNearest function so that your projectile isn't checking collision with all 200 non-existent NPC's in the world. And your projectile wasn't homing because you had removed the position of its target from the equation, so that it never knew where to home to. Also, you forgot to check if your 'nearest' NPC was null, which would cause the projectile to disappear. I've fixed your AI for you.
Code:
public override void AI()
{
    NPC nearest = this.FindNearest(projectile.Center);
    Vector2 acceleration;
    if (nearest != null) //this is really important
    {
        acceleration = (nearest.position - projectile.position);
    } else
    {
        acceleration = Vector2.Zero; //maintain speed -- or whatever else you want to do here
    }
    projectile.velocity += acceleration * 1.5f;
    projectile.velocity *= 0.99f;
    if (projectile.velocity.Length() > maxSpeed)
    {
        projectile.velocity.Normalize();
        projectile.velocity *= maxSpeed;
    }
    projectile.rotation = projectile.velocity.ToRotation();
}
Also, it looks like you've been removing my comments from the code. It's a good idea to leave those there so that the code is easier to interpret.
Ok, thanks I got it to work now.
I was even able to make two custom crafting groups.
But how can I give a tile the Ability to work like a Mythril Anvil or an Adamantite Forge?
 

Itorius

Skeletron Prime
Ok, thanks I got it to work now.
I was even able to make two custom crafting groups.
But how can I give a tile the Ability to work like a Mythril Anvil or an Adamantite Forge?
In the class where is your block just add this line of code in the SetDefults
Code:
adjTiles = new int[] { TileID.Anvils };
you should find both mythril anvil and the forge there.
 

DoYouEvenCookie

Terrarian
Well first, I found the problem that was causing the slow down. You need to swap the two if conditions inside the findNearest function so that your projectile isn't checking collision with all 200 non-existent NPC's in the world. And your projectile wasn't homing because you had removed the position of its target from the equation, so that it never knew where to home to. Also, you forgot to check if your 'nearest' NPC was null, which would cause the projectile to disappear. I've fixed your AI for you.
Code:
public override void AI()
{
NPC nearest = this.FindNearest(projectile.Center);
Vector2 acceleration;
if (nearest != null) //this is really important
{
acceleration = (nearest.position - projectile.position);
} else
{
acceleration = Vector2.Zero; //maintain speed -- or whatever else you want to do here
}
projectile.velocity += acceleration * 1.5f;
projectile.velocity *= 0.99f;
if (projectile.velocity.Length() > maxSpeed)
{
projectile.velocity.Normalize();
projectile.velocity *= maxSpeed;
}
projectile.rotation = projectile.velocity.ToRotation();
}
Also, it looks like you've been removing my comments from the code. It's a good idea to leave those there so that the code is easier to interpret.

less of a problem now :)

all it does now is go out a small distance and the stops and then homes when there is something to home on.
but it still is stopping when there is nothing to home on.

Code:
public NPC FindNearest(Vector2 pos)
        {
         
            NPC nearest = null;
            float oldDist = 1001;
            float newDist = 1000;
            for (int i = 0; i < Terraria.Main.npc.Length - 1; i++)
            {
                if (!Collision.CanHit(pos, 1, 1, Main.npc[i].position, Main.npc[i].width, Main.npc[i].height))
                    continue;
                if (!Terraria.Main.npc[i].CanBeChasedBy(projectile))
                    continue;
                if (nearest == null)
                    nearest = Terraria.Main.npc[i];
                else
                {
                    oldDist = Vector2.Distance(pos, nearest.position);
                    newDist = Vector2.Distance(pos, Terraria.Main.npc[i].position);
                    if (newDist < oldDist)
                        nearest = Terraria.Main.npc[i];
                }
            }
            return nearest;
        }
        int maxSpeed = 10;
        public override void AI()
        {
            NPC nearest = this.FindNearest(projectile.Center);
            Vector2 acceleration;
            if (nearest != null) // extremely important
            {
                acceleration = (nearest.position - projectile.position);
            } else
            {
                acceleration = Vector2.Zero; // maintain speed -- or whatever else you want to do here
            }
            projectile.velocity += acceleration * 1.5f;
            projectile.velocity *= 0.95f;
            if (projectile.velocity.Length() > maxSpeed)
            {
                projectile.velocity.Normalize();
                projectile.velocity *= maxSpeed;
            }
            projectile.rotation = projectile.velocity.ToRotation();
        }

also, since this homing is almost complete, how might I make an arkhalis type weapon. I have tried, but the projectile for the blade doesn't even show up!

Weapon Code defaults:
Code:
 public override void SetDefaults()
        {
            item.name = "Solarius";
            item.damage = 75;
            item.melee = true;
            item.width = 48;
            item.height = 48;
            item.useTime = 1;
            item.useAnimation = 1;
            item.useStyle = 1;
            item.noMelee = true;
            item.knockBack = 2;
            item.noUseGraphic = true;
            item.channel = true;
            item.value = 100000;
            item.rare = 9;
            item.useSound = 1;
            item.autoReuse = true;
            item.shoot = mod.ProjectileType("SolarisP");
            item.shootSpeed = 20;
        }

Projectile Code defaults:
Code:
public class SolarisP : ModProjectile
    {
        public override void SetDefaults()
        {
            projectile.name = "Solarius";
            projectile.width = 36;
            projectile.height = 20;
            projectile.friendly = true;
            projectile.penetrate = -1;
            projectile.aiStyle = 20;
            projectile.melee = true;
            projectile.tileCollide = false;
            projectile.light = 2f;
        }
the class is shown so you know why the weapon is refering to that name for the projectile.
pls help me
I am so lost :(
 
Last edited:

Cookiecan10

Terrarian
Does anybody else also get this error message when trying to run the example mod?
Missing mod: ExampleMod/Dusts/Bubble
at Terraria.ModLoader.ModLoader.GetTexture(String name)
at Terraria.ModLoader.Mod.AddDust(String name, ModDust dust, String texture)
at Terraria.ModLoader.Mod.AutoloadDust(Type type)
at Terraria.ModLoader.Mod.Autoload()
at Terraria.ModLoader.ModLoader.do_Load(Object threadContext)
 

Zahro

Official Terrarian
Does anyone know if the tModLoader server has a config file? I'd like to change how much ram it uses cause my friend has been experiencing some lag issues because of all the mods. Thanks.
 

Odran

Steampunker
Does anybody else also get this error message when trying to run the example mod?
Missing mod: ExampleMod/Dusts/Bubble
at Terraria.ModLoader.ModLoader.GetTexture(String name)
at Terraria.ModLoader.Mod.AddDust(String name, ModDust dust, String texture)
at Terraria.ModLoader.Mod.AutoloadDust(Type type)
at Terraria.ModLoader.Mod.Autoload()
at Terraria.ModLoader.ModLoader.do_Load(Object threadContext)

Did you check that file (Bubble)? Did you edit anithing?
 

UberCAKE

Spazmatism
c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\CopperDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\CopperDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\CopperDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\GoldDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\GoldDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\GoldDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\IronDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\IronDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\IronDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\LeadDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\LeadDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\LeadDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\SilverDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\SilverDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\SilverDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TinDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TinDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TinDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TungstenDrill123.cs(34,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TungstenDrill123.cs(35,34) : error CS0103: The name 'ItemID' does not exist in the current context

c:\Users\Administrator\Documents\My Games\Terraria\ModLoader\Mod Sources\CakeMod\Items\TungstenDrill123.cs(36,28) : error CS0103: The name 'TileID' does not exist in the current context


The code:
using Terraria;
using Terraria.ModLoader;

namespace CakeMod.Items
{
public class CopperDrill123 : ModItem
{
public override void SetDefaults()
{
item.name = "Copper Drill";
item.damage = 4;
item.melee = true;
item.width = 20;
item.height = 12;
item.toolTip = "";
item.useTime = 15;
item.useAnimation = 23;
item.channel = true;
item.noUseGraphic = true;
item.noMelee = true;
item.pick = 35;
item.useStyle = 5;
item.knockBack = 2;
item.value = Item.buyPrice(0, 1, 20, 0);
item.rare = 5;
item.useSound = 23;
item.autoReuse = true;
item.shoot = mod.ProjectileType("CopperDrill123");
item.shootSpeed = 40f;
}
public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.CopperBar, 12);
recipe.AddIngredient(ItemID.Wood, 4);
recipe.AddTile(TileID.Anvils);
recipe.SetResult(this);
recipe.AddRecipe();
}
}
}


Even though there were several files in the error message, all of them are almost the same.
 

PlayerPlaysTERR

Steampunker
hlp me plz
c:\Users\William\Documents\My Games\Terraria\ModLoader\Mod Sources\TheElectronMod\Projectiles\ElectronFlailProjectile.cs(24,96) : error CS0246: The type or namespace name 'Color' could not be found (are you missing a using directive or an assembly reference?)
 

Odran

Steampunker
hlp me plz
c:\Users\William\Documents\My Games\Terraria\ModLoader\Mod Sources\TheElectronMod\Projectiles\ElectronFlailProjectile.cs(24,96) : error CS0246: The type or namespace name 'Color' could not be found (are you missing a using directive or an assembly reference?)

Have you added the using directive "using Microsoft.Xna.Framework"?
 
Top Bottom