Search code examples
c++timewait

How can I make a user wait before being able to use a function again?


I'm sure this has already been answered somewhere but I can't find..

say i have a function inside a simple game such as work, and i wan't to make the user have to wait 3 minutes before being able to use work again after using it, how can I do this?

I am new to programming so I need help! Thank you !!


Solution

  • You could use a static time variable inside a function:

    void work()
    {
      static bool      first_pass = true;
      static Time_Type unlock_time;
      if (first_pass)
      {
          // Assign unlock time to now.
          // Add 3 minutes to unlock time;
      }
      else
      {
         Time_Type time_now = /* ... */;
         if (time_now > unlock_time)
         {
             // unlock_time = time_now + 3 minutes;
             // Remaining function goes here.
         }
      }
    }
    

    Static variables inside a function keep their values after execution leaves the function.

    For time functions and variables, see std::chrono.