It is most definitely not the easiest thing for someone completely new to coding. The most basic tmodloader functions revolve around making basic items or npcs. What you are trying to make involves running a global player script that holds a variable for your resource which is then referenced in every script that uses said resource. Depending on how you want these buffs to activate you might need to catch player inputs in update methods and whatnot. It isn't the most complicated thing in the world but it requires a bit of knowledge of how object oriented programing works and it depends on how versed you are on the fundamentals of coding.
I only have a few months of coding experience, but I would really like to give it a try... do you have any idea how I would go about starting to code this? And if not, do you know how I could incorporate animated sprites to my buffs, as mentioned above? Thank you!
Edit: Turns out, I was able to code the resource first try! Well, almost... I'm having a small issue where the resource is regenerating and is able to be used, but the "bar" that is supposed to show how much of the resource the player has isn't showing up for some reason... any ideas? Here's all my code:
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
namespace Test
{
public abstract class EssenceItem : ModItem
{
public override bool CloneNewInstances => true;
public int essenceCost = 0;
public virtual void SafeSetDefaults()
{
}
public sealed override void SetDefaults()
{
SafeSetDefaults();
item.melee = false;
item.ranged = false;
item.magic = false;
item.thrown = false;
item.summon = false;
}
public override void ModifyWeaponDamage(Player player, ref float add, ref float mult, ref float flat)
{
add += EssencePlayer.ModPlayer(player).essenceDamageAdd;
mult *= EssencePlayer.ModPlayer(player).essenceDamageMult;
}
public override void GetWeaponKnockback(Player player, ref float knockback)
{
knockback += EssencePlayer.ModPlayer(player).essenceKnockback;
}
public override void GetWeaponCrit(Player player, ref int crit)
{
crit += EssencePlayer.ModPlayer(player).essenceCrit;
}
public override void ModifyTooltips(List<TooltipLine> tooltips)
{
TooltipLine tt = tooltips.FirstOrDefault(x => x.Name == "Damage" && x.mod == "Terraria");
if (tt != null)
{
string[] splitText = tt.text.Split(' ');
string damageValue = splitText.First();
string damageWord = splitText.Last();
tt.text = damageValue + " essence " + damageWord;
}
if (essenceCost > 0)
{
tooltips.Add(new TooltipLine(mod, "Essence Cost", $"Uses {essenceCost} essence"));
}
}
public override bool CanUseItem(Player player)
{
var exampleDamagePlayer = player.GetModPlayer<EssencePlayer>();
if (exampleDamagePlayer.essenceCurrent >= essenceCost)
{
exampleDamagePlayer.essenceCurrent -= essenceCost;
return true;
}
return false;
}
}
}
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ModLoader;
namespace Test
{
public class EssencePlayer : ModPlayer
{
public static EssencePlayer ModPlayer(Player player)
{
return player.GetModPlayer<EssencePlayer>();
}
public float essenceDamageAdd;
public float essenceDamageMult = 1f;
public float essenceKnockback;
public int essenceCrit;
public int essenceCurrent;
public const int DefaultEssenceMax = 100;
public int essenceMax;
public int essenceMax2;
public float essenceRegenRate;
internal int essenceRegenTimer = 0;
public static readonly Color HealEssence = new Color(187, 91, 201);
public override void Initialize()
{
essenceMax = DefaultEssenceMax;
}
public override void ResetEffects()
{
ResetVariables();
}
public override void UpdateDead()
{
ResetVariables();
}
private void ResetVariables()
{
essenceDamageAdd = 0f;
essenceDamageMult = 1f;
essenceKnockback = 0f;
essenceCrit = 0;
essenceRegenRate = 1f;
essenceMax2 = essenceMax;
}
public override void PostUpdateMiscEffects()
{
UpdateResource();
}
private void UpdateResource()
{
essenceRegenTimer++;
if (essenceRegenTimer > 180 * essenceRegenRate)
{
essenceCurrent += 1;
essenceRegenTimer = 0;
}
essenceCurrent = Utils.Clamp(essenceCurrent, 0,essenceMax2);
}
}
}
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.UI;
using Terraria.ModLoader;
using Test;
namespace Test.UI
{
internal class EssenceBar : UIState
{
private UIText text;
private UIElement area;
private UIImage barFrame;
private Color gradientA;
private Color gradientB;
public override void OnInitialize()
{
area = new UIElement();
area.Left.Set(-area.Width.Pixels - 600, 1f);
area.Top.Set(30, 0f);
area.Width.Set(182, 0f);
area.Height.Set(60, 0f);
barFrame = new UIImage(ModContent.GetTexture("Test/UI/EssenceBarFrame"));
barFrame.Left.Set(22, 0f);
barFrame.Top.Set(0, 0f);
barFrame.Width.Set(138, 0f);
barFrame.Height.Set(34, 0f);
text = new UIText("0/0", 0.8f);
text.Width.Set(138, 0f);
text.Height.Set(34, 0f);
text.Top.Set(40, 0f);
text.Left.Set(0, 0f);
gradientA = new Color(123, 25, 138);
gradientB = new Color(187, 91, 201);
area.Append(text);
area.Append(barFrame);
Append(area);
}
public override void Draw(SpriteBatch spriteBatch)
{
if (!(Main.LocalPlayer.HeldItem.modItem is EssenceItem))
return;
base.Draw(spriteBatch);
}
protected override void DrawSelf(SpriteBatch spriteBatch)
{
base.DrawSelf(spriteBatch);
var modPlayer = Main.LocalPlayer.GetModPlayer<EssencePlayer>();
float quotient = (float)modPlayer.essenceCurrent / modPlayer.essenceMax2;
quotient = Utils.Clamp(quotient, 0f, 1f);
Rectangle hitbox = barFrame.GetInnerDimensions().ToRectangle();
hitbox.X += 12;
hitbox.Width -= 24;
hitbox.Y += 8;
hitbox.Height -= 16;
int left = hitbox.Left;
int right = hitbox.Right;
int steps = (int)((right - left) * quotient);
for (int i = 0; i < steps; i += 1)
{
float percent = (float)i / (right - left);
spriteBatch.Draw(Main.magicPixel, new Rectangle(left + i, hitbox.Y, 1, hitbox.Height), Color.Lerp(gradientA, gradientB, percent));
}
}
public override void Update(GameTime gameTime)
{
if (!(Main.LocalPlayer.HeldItem.modItem is EssenceItem))
return;
var modPlayer = Main.LocalPlayer.GetModPlayer<EssencePlayer>();
text.SetText($"Essence: {modPlayer.essenceCurrent} / {modPlayer.essenceMax2}");
base.Update(gameTime);
}
}
}
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
namespace Test.Items.Weapons
{
public class EssenceStaff : EssenceItem
{
public override void SetStaticDefaults()
{
Item.staff[item.type] = true;
}
public override void SafeSetDefaults()
{
item.CloneDefaults(ItemID.AmethystStaff);
item.Size = new Vector2(28, 36);
item.damage = 32;
item.knockBack = 3;
item.rare = ItemRarityID.Red;
item.mana = 0;
item.useStyle = ItemUseStyleID.HoldingOut;
essenceCost = 10;
}
}
}