tModLoader Need help with stackable debuff

agneovo123

Official Terrarian
I had the idea, to make a stackable 'Bleeding' debuff.
The idea is as follows:
It starts out at 2 dmg 5 times a second, and with each additional stack the dmg increases by 2. Kind of like how the Daybreak works, except thie bleeding could be staced unlimitedly.
Now I have tried to make it work, but it just kept failing and failing, I would appreciate some help.
My current code:
GlobalNPC class: this is where I tried the stacking
C#:
public class GlobalNPC : Terraria.ModLoader.GlobalNPC
{
    public override bool InstancePerEntity => true;
    public bool SBleed;
    public int BleedStack;
    public override void ResetEffects(NPC npc)
    {
        SBleed = false;
        BleedStack = GlobalVariables.CBS;
        //////GlobalVariables.CBS++;
    }
    public override void UpdateLifeRegen(NPC npc, ref int damage)
    {
        if (SBleed)
        {
            if (npc.lifeRegen > 0)
            {
                npc.lifeRegen = 0;
            }
            //////BleedStack = GlobalVariables.CBS;
            GlobalVariables.CBS++;
            npc.lifeRegen -= GlobalVariables.CBS*2 * 10;
            if (damage < GlobalVariables.CBS*2)
            {
                damage = GlobalVariables.CBS*2;
            }
        }
    }
}

The buff class:
C#:
public class SBleed : ModBuff
{
    public override void SetDefaults()
    {
        DisplayName.SetDefault("Stackable Bleed");
        Description.SetDefault("This bleed can stack (woah)");
    }
    public override void Update(NPC npc, ref int buffIndex)
    {
        npc.GetGlobalNPC<NPCs.GlobalNPC>().SBleed = true;
    }
}

GlobalVariables class: only using it as an external variable (CBS = Current Bleed Stacks)
C#:
class GlobalVariables
{
    public static int CBS = 1;
}

And lastly I have a test sword with a 100% chance to inflict the debuff to test it.
C#:
public override void OnHitNPC(Player player, NPC target, int damage, float knockBack, bool crit)
{
   target.AddBuff(mod.BuffType("SBleed"), 180);
}


I have tried commenting out certain parts of GlobalNPC, tried without the GlobalVariable CBS thing
tried to count the stacks within the buff class, nothing worked the way I wanted to.
The closest is the current code, which increses the DoT by 2 every time it ticks (I want it to increase every time I hit the target), and it is global so if I hit another enemy it doesn't start from 2, it starts from where it left off the last one. (I know the problem for this one is the global variable CBS, being you know: Global, but without it, it just wouldn't increase the stack damage).

Thanks in advance for any help.

Edit: I used the CODE feature
 
Last edited:
I have had a good 12 hours of sleep and came up with this:

GlobalNPC class: a few changes, the main one being that I got rid of the Global variable CBS.
C#:
public class ModGlobalNPC : Terraria.ModLoader.GlobalNPC
{
    public override bool InstancePerEntity => true;
    public bool SBleed;
    public int BleedStack = 0;
    public override void ResetEffects(NPC npc)
    {
        SBleed = false;
    }
    public override void UpdateLifeRegen(NPC npc, ref int damage)
    {
        if (SBleed)
        {
            if (npc.lifeRegen > 0)
            {
                npc.lifeRegen = 0;
            }
            npc.lifeRegen -= BleedStack * 2 * 10; // stack*2 for damage, and *10 to make it tick 5 times a second
            if (damage < BleedStack * 2)
            {
                damage = BleedStack * 2;
            }
        }
        else
        {
            BleedStack = 0; //To reset the stack, when it's not refreshed
        }
    }
}

The "SBleed" buff class was unchanged
And I entirely removet the GlobalVariables class.

The test sword is largely the same with this important change:
C#:
public override void OnHitNPC(Player player, NPC target, int damage, float knockBack, bool crit)
{
    //if (Main.rand.Next(10) == 1) //The if is commented out for testing reasons.
    {
        target.AddBuff(mod.BuffType("SBleed"), 180); //Apply (or refresh) the bleeding effect for 180/60=3 seconds
        target.GetGlobalNPC<NPCs.ModGlobalNPC>().BleedStack++; //Increase the current bleedstack.
    }
}

Now this works mighty fine:
The 1st stack does 2 dmg 5 times a second,
the 2nd stack does 4 dmg 5 times a second,
the 3rd stack does 6 dmg 5 times a second, and so on.
The stack only increases if you hit the target again while the debuff is still active, if the debuff runs out, on the next hit the stack will be reset to 1.
It isn't perfect though: sometimes, when the debuff wears off, a bunch of 1 dmg happens. (which I'm fine with)
 
Last edited:
Amazing work. I too am in a similar predicament in which instead of stackable debuff effects, I’m trying to make debuffs’/buffs’ times stack. I’m also trying to make it where certain debuffs can’t reapply if the player currently has that debuff.
 
Back
Top Bottom