-3

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 !!

Zuwy
  • 1
  • 1

1 Answers1

0

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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1. i get an error if i try to use std::chrono, 2. what would i use to set unlock time to now, 3. " Add 3 minutes to unlock time" yeah how? – Zuwy Nov 20 '19 at 20:33
  • 1. What is the error message, verbatim. 2. There is a function called `now()`. 3. Either convert the time to minutes, then add 3, then convert back or see if there is a method for advancing or adding minutes. – Thomas Matthews Nov 21 '19 at 15:44