Skip to main content
The project's repository is archived as part of the GitHub Archive Program. RogueLibs' code and the documentation will no longer be updated.

Recharging Items

RogueLibs doesn't provide any explicit functionality for rechargeable items, but you can easily implement that yourself, using the IDoUpdate interface. You'll find some useful code snippets below, that you can reuse for your own items.

Making items rechargeable

Make your custom item's class implement the IDoUpdate interface:

MyRechargeableItem.cs
public class MyRechargeableItem : CustomItem, IDoUpdate
{
/* ... */
}

Presets

Cooldown represents the amount of seconds to wait until full recharge.

    public float Cooldown { get; private set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime, 0f);

With adjustable recharging speed:

    public float RechargeSpeed = 1f;

public float Cooldown { get; private set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime * RechargeSpeed, 0f);

Usage:

    public bool UseItem()
{
if (Cooldown != 0f) return false;
/* ... */
Cooldown = 1.5f;
return true;
}
info

You can use other activation methods too, like CombineItems, TargetObject, TargetPosition and etc.

If you want to display Cooldown as the item's count, then override the GetCountString method:

    public override CustomTooltip GetCountString()
{
if (Cooldown != 0f) return new CustomTooltip(Cooldown, Color.red);
return base.GetCountString(); // display default count
}

note

There's also a vanilla way of recharging items, but it's really messy and unreliable.

Examples

using UnityEngine;

namespace RogueLibsCore.Test
{
[ItemCategories(RogueCategories.Food, RogueCategories.Technology)]
public class QuantumFud : CustomItem, IItemUsable, IDoUpdate
{
[RLSetup]
public static void Setup()
{
RogueLibs.CreateCustomItem<QuantumFud>()
.WithName(new CustomNameInfo("Quantum Fud"))
.WithDescription(new CustomNameInfo("A very complicated piece of quantum technology. When you eat it, its quantum equivalent clone is consumed, while the original thing remains intact."))
.WithSprite(Properties.Resources.QuantumFud)
.WithUnlock(new ItemUnlock
{
UnlockCost = 10,
LoadoutCost = 15,
CharacterCreationCost = 10,
Prerequisites = { VanillaItems.FoodProcessor },
});
}

public override void SetupDetails()
{
Item.itemType = ItemTypes.Food;
Item.itemValue = 180;
Item.healthChange = 1;
Item.cantBeCloned = true;
Item.goesInToolbar = true;
}

public float Cooldown { get; set; }
public void Update() => Cooldown = Mathf.Max(Cooldown - Time.deltaTime, 0f);

public bool UseItem()
{
if (Cooldown != 0f) return false;

int heal = new ItemFunctions().DetermineHealthChange(Item, Owner);
Owner!.statusEffects.ChangeHealth(heal);

if (Owner.HasTrait(VanillaTraits.ShareTheHealth)
|| Owner.HasTrait(VanillaTraits.ShareTheHealth2))
new ItemFunctions().GiveFollowersHealth(Owner, heal);

gc.audioHandler.Play(Owner, VanillaAudio.UseFood);
Cooldown = 0.5f;
return true;
}
}
}

The project's repository is archived as part of the GitHub Archive Program. RogueLibs' code and the documentation will no longer be updated.