tModLoader Official tModLoader Help Thread

How would I make an edible item grant the well-fed buff?
I have this, but my gut feeling tells me that it's incorrect.
Code:
public bool UseItem(Item item, Player player)
        {
            player.AddBuff((BuffID.WellFed) , 6000, true);
            return false;
            
        }
 
How would I make an edible item grant the well-fed buff?
I have this, but my gut feeling tells me that it's incorrect.
Code:
public bool UseItem(Item item, Player player)
        {
            player.AddBuff((BuffID.WellFed) , 6000, true);
            return false;
           
        }
Just remove the parentheses around BuffID.WellFed
 
How would I make an edible item grant the well-fed buff?
I have this, but my gut feeling tells me that it's incorrect.
Code:
public bool UseItem(Item item, Player player)
        {
            player.AddBuff((BuffID.WellFed) , 6000, true);
            return false;
           
        }
You don't need to do that - You can just put item.buffType = BuffID.WellFed; and item.buffTime = 6000; in SetDefaults
 
Hmmmmm...
I am making a mod that extends on mod milestones called Banners++ (Apparently, I soon realized that there was a Banners+ too. Might rename if I have to.)
Only, one problem, how do you detect a player wearing an accessory?
I know it has to be something like (player. == true) and I also know that this question is extremely dumb and I should know better.
I just can't exactly pinpoint my finger on it...
 
Hmmmmm...
I am making a mod that extends on mod milestones called Banners++ (Apparently, I soon realized that there was a Banners+ too. Might rename if I have to.)
Only, one problem, how do you detect a player wearing an accessory?
I know it has to be something like (player. == true) and I also know that this question is extremely dumb and I should know better.
I just can't exactly pinpoint my finger on it...
Some vanilla accessories will change a variable in player, like player.accFlipper for the flipper accessory, but for all others, I spotted some Item[] in Player called inventory, miscEquips and armor. You'll have to loop through the correct one out of them (there might be others I missed) and manually check, as far as I know there is no convenient method for it.
 
Does anyone know how to make a boss generate an ore underground? I have the scripts for actually generating the ore down, but my issue is the generation after killing a boss part of it. It either doesn't work, or I can just restart the game and kill the boss again and then the ore generates more.
 
Some vanilla accessories will change a variable in player, like player.accFlipper for the flipper accessory, but for all others, I spotted some Item[] in Player called inventory, miscEquips and armor. You'll have to loop through the correct one out of them (there might be others I missed) and manually check, as far as I know there is no convenient method for it.
Thanks for the reply! :happy:

I also have another question...
What about specific accessories? I want a vanilla enemy to be able to detect my modded accessories. Again, sorry if this sounds dumb.
 
Thanks for the reply! :happy:

I also have another question...
What about specific accessories? I want a vanilla enemy to be able to detect my modded accessories. Again, sorry if this sounds dumb.
I think for that you're going to have to subclass GlobalNPC and in AI() you'll have to first check the NPC type (if it's the one you want to detect accessories) and loop through Main.player and check each players inventory, maybe only if the player is within a certain distance of the NPC (or whatever your additional criteria). When you're looping through player.inventory, player.miscEquips or player.armor (whichever contains accessories, bit of trial and error to find out), you can just check each item with something like
Code:
if(item.type == ModContent.ItemType<MyModdedAccessory>()) {
}
 
Does anyone know how to make a boss generate an ore underground? I have the scripts for actually generating the ore down, but my issue is the generation after killing a boss part of it. It either doesn't work, or I can just restart the game and kill the boss again and then the ore generates more.
You'll need a static global bool like public static bool hasGeneratedOre in one of your classes, and only generate ore when that boss is defeated if hasGeneratedOre is false, and then you'll obviously need to set it to true after that. To keep this variable across each time you enter the world, you'll need to have a subclass of ModWorld and override Load and Save like so:
Code:
public override TagCompound Save()
{
    return new TagCompound
    {
        {"hasGeneratedOre", hasGeneratedOre}
    };
}

public override void Load(TagCompound tag)
{
    hasGeneratedOre = tag.GetBool("hasGeneratedOre");
}
 
I think for that you're going to have to subclass GlobalNPC and in AI() you'll have to first check the NPC type (if it's the one you want to detect accessories) and loop through Main.player and check each players inventory, maybe only if the player is within a certain distance of the NPC (or whatever your additional criteria). When you're looping through player.inventory, player.miscEquips or player.armor (whichever contains accessories, bit of trial and error to find out), you can just check each item with something like
Code:
if(item.type == ModContent.ItemType<MyModdedAccessory>()) {
}
Alright, thanks so much! :D
Even though that is a complicated process, I will learn.
 
I’m new to modding and planning on adding a Flower that only spawns in crimson, but for it to make sense I need to make the Deathweed stop spawning in the crimson. I have the code for a flower, but I don’t know how to stop the Deathweed from spawing.
(If anyone wants to know I’m going to make each flower make a potion)
 
Alright, thanks so much! :D
Even though that is a complicated process, I will learn.
Probably be useful to create a Helper class with some commonly used/useful functions in, like hasInInventory and hasEquippedAccessory

I’m new to modding and planning on adding a Flower that only spawns in crimson, but for it to make sense I need to make the Deathweed stop spawning in the crimson. I have the code for a flower, but I don’t know how to stop the Deathweed from spawing.
(If anyone wants to know I’m going to make each flower make a potion)
You can probably change that in GlobalTile, but I wouldn't know how sorry
 
Probably be useful to create a Helper class with some commonly used/useful functions in, like hasInInventory and hasEquippedAccessory


You can probably change that in GlobalTile, but I wouldn't know how sorry
That’s ok, now I can look up Global Tiles since I have not heard of those.
 
How do you assign a town NPC head sprite to a town NPC? Is it a line of code? Do you have to name the head sprite file something special?
 
Here is some code for an item I'm making, the game seems to load it perfectly fine, but it does not shoot, and it always displays 0 ammo. I knew it probably wouldn't work, as I'm not experienced in coding, and I had nothing to go off of here. I'm wondering if someone can help me to get the slingshot item to use stone blocks instead of typical ammunition.

public class Slingshot : ModItem

{
public override void SetStaticDefaults()
{
Tooltip.SetDefault("Uses stone.");
}
public override void SetDefaults()

{


item.damage = 15;
item.ranged = true;
item.width = 20;
item.height = 33;
item.useTime = 25;
item.useAnimation = 25;
item.crit = 13;
item.useStyle = 5;
item.noMelee = true;
item.knockBack = 3;
item.value = 200;
item.rare = 1;
item.UseSound = SoundID.Item5;
item.autoReuse = false;
item.shoot = mod.ProjectileType("StoneProj");
item.shootSpeed = 25f;
item.useAmmo = ItemID.StoneBlock;

}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.Cobweb, 3);
recipe.AddIngredient(ModContent.ItemType<Stick>(), 1);
recipe.AddTile(TileID.MythrilAnvil);
recipe.SetResult(this);
recipe.AddRecipe();
}


}
 
How do you assign a town NPC head sprite to a town NPC? Is it a line of code? Do you have to name the head sprite file something special?
Add [AutoloadHead] before the public class line. Like this:
C#:
using System;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using static Terraria.ModLoader.ModContent;

namespace ModName.NPCs
{
    [AutoloadHead]
    public class ModNPC : ModNPC
    {
Make sure you have a texture for the head, make sure it has a _Head.png at the end of it.
 
Here is some code for an item I'm making, the game seems to load it perfectly fine, but it does not shoot, and it always displays 0 ammo. I knew it probably wouldn't work, as I'm not experienced in coding, and I had nothing to go off of here. I'm wondering if someone can help me to get the slingshot item to use stone blocks instead of typical ammunition.

public class Slingshot : ModItem

{
public override void SetStaticDefaults()
{
Tooltip.SetDefault("Uses stone.");
}
public override void SetDefaults()

{


item.damage = 15;
item.ranged = true;
item.width = 20;
item.height = 33;
item.useTime = 25;
item.useAnimation = 25;
item.crit = 13;
item.useStyle = 5;
item.noMelee = true;
item.knockBack = 3;
item.value = 200;
item.rare = 1;
item.UseSound = SoundID.Item5;
item.autoReuse = false;
item.shoot = mod.ProjectileType("StoneProj");
item.shootSpeed = 25f;
item.useAmmo = ItemID.StoneBlock;

}

public override void AddRecipes()
{
ModRecipe recipe = new ModRecipe(mod);
recipe.AddIngredient(ItemID.Cobweb, 3);
recipe.AddIngredient(ModContent.ItemType<Stick>(), 1);
recipe.AddTile(TileID.MythrilAnvil);
recipe.SetResult(this);
recipe.AddRecipe();
}


}
It most likely seems to be due to the fact that the stone block is a vanilla item. Thus, you can do this:
C#:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;
using Terraria.DataStructures;
using Terraria.Localization;
using System.IO;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using System.Linq;

namespace ModName.Items
{
    public class StoneBlock : GlobalItem
    {
        public override void SetDefaults(Item item) {
            if (item.type == ItemID.StoneBlock) {
                item.damage = 15;
                item.ranged = true;
                item.shoot = mod.ProjectileType("StoneProj");
                item.ammo = item.type; // This makes it so the ammo type of this Stone Block is the first one in it's class.
                //item.ammo = ItemID.StoneBlock; // An alternative way to do it, since this is a vanilla item, it has an ID.
            }
        }
    }
}
It makes it so the Stone Block is ammo, and can be used for ammo
 
Cannot implicitly convert type 'Terraria.Item[]' to 'bool' Why you gotta do this too me Terraria!?!?!?

Here is the part the error is located in.
C#:
    public static void AI(Item item, Player player, NPC npc) {
        if (npc.type == 2) {
            if (player.miscEquips) {
                if (item.type == ModContent.ItemType<SightNecklace>()) {
                    npc.friendly = true;
                    npc.aiStyle = 24;
                    npc.damage = 0;
                }
            }
        }
    }
Of course, I expected it to be complicated, but didn't expect it to be thorny nor tedious.
 
Back
Top Bottom