Console Timer

Monger

New member
Joined
Mar 14, 2017
Messages
3
Programming Experience
Beginner
Ok for some reason I can't wrap my head around this just finished a basic C# class and I'm trying to get ahead of the game and learn some stuff and explore some options. I have my project that I want to add a timer each Potion : Item but, also follow some oop and inheritance concepts but I can't seem to find good info on how to implement the timer for this type of method or scaling. any help would be grateful and examples would be grateful.
copy of project linked HERE I'm using OSX version of VS but .cs files can still be opened.
 
Perhaps you could explain what you're trying to achieve. This:
I want to add a timer each Potion : Item
doesn't really tell us much. Generally speaking, you would use a System.Timers.Timer and handle its Elapsed event. More than that, we can't really tell you without an better idea of the purpose and context.
 
I'm looking to add a timer to the useable Potion and be able to set it to have a cool down before it can be used again
class Potion : Item
{
public override void Use(Character mcharacter)
{
mcharacter.Health = mcharacter.Health + IntEffect;
}
}




abstract class Item : IUseable
{
//Variables
private string _strEffect;
private int _intEffect;
private string _strDescription;
}



interface IUseable
{
void Use(Character mcharacter);
}
 
Last edited:
Firstly, you've actually made your code hard to read there. For future reference, please post code snippets as plain text inside appropriate formatting tags, i.e.

[xcode=c#]your code here[/xcode]

As for the issue, I'm not sure that you're looking in the right direction. The purpose of a Timer is to notify you that a set period of time has passed so that you can do something but I don't think that that's what you need here but, even if it is, I don't think that it should be part of the Potion class. I would do something like this:
class Potion
{
    public DateTime LastUsed { get; private set; }

    public int CoolDownPeriod { get; set; }

    public bool CanBeUsed
    {
        get
        {
            return DateTime.Now >= LastUsed.AddSeconds(CoolDownPeriod);
        }
    }

    public bool Use()
    {
        var canUse = CanBeUsed;

        if (canUse)
        {
            // The potion can be used.
            // ...

            LastUsed = DateTime.Now;
        }

        return canUse;
    }
}

Now you can always know whether a Potion can be used or not and you can simply call Use and it will tell you whether it was able to be used or not. That's all you need if the user doesn't need to be notified ahead of time which potions they can use and which they can't.

If you want to notify the user when a used Potion becomes usable again then you can use a Timer but you would only need one Timer and it would not be part of the Potions. You could simply add a Potion to a list when it's used and then have one Timer that raises its Elapsed event every, say, 1 second and then you check the whole list and remove the ones that are now usable:
private static readonly Timer potionTimer = new Timer(1000);

private static readonly List<Potion> unusablePotions = new List<Potion>();

static void Main(string[] args)
{
    potionTimer.Elapsed += PotionTimer_Elapsed;
}

private static void PotionTimer_Elapsed(object sender, ElapsedEventArgs e)
{
    for (var i = unusablePotions.Count - 1; i >= 0; i--)
    {
        var potion = unusablePotions[i];

        if (potion.CanBeUsed)
        {
            // Notify the user that this potion can be used.

            unusablePotions.Remove(potion);
        }
    }
}
 
Back
Top Bottom