# Using gametime.ElapsedGameTime to trigger an event

I have a splashscreen object that waits for a user input before loading the next screen, but I would also like the next screen to load after a certain amount of time has passed. I tried adding a check to see if the elapsed game time is greater than or equal to a set amount of time in seconds. The user input still changes the screen, but the screen will not change after that time requirement has been met. What am I doing wrong?

• Welcome to stack overflow. Please post code as text rather than screenshots. This makes it easier to read and answer the question. – craftworkgames Nov 26 '17 at 23:22

I figured it out. gameTime.ElapsedGameTime is a TimeSpan that refers to the amount of time that has passed since the last update, which is a fraction of a second. By using gameTime.TotalGameTime, I was able to get the total amount of time that has passed since the program started running. Also, for some reason I can't explain, using > in when comparing gameTime.TotalGameTime.Seconds to autoChangeTime.Seconds will not work. It only worked when I used == to compare them, which is fine because there are quite a few Update cycles where gameTime.TotalGameTime.Seconds is equal to autoChangeTime.Seconds.

The problem is, if you use == and if update() is delayed for over a second when it reaches changeTime, then it wont ever be true. I cannot see why > would not work either they are both Ints? Have you tried nesting the two parts of the if statement to see if it works on its own after you have checked for the key presses?

• If I nest the statements, the screen will only change after an input has been been detected. I would like the screen to change without any user input as well. Edit: I removed the Key press check entirely, and it does not work with >=. – Ryan Ennis Nov 27 '17 at 20:01
• I see the issue with using ==, but is it likely that there would ever be a situation where the game is running less than 1 update cycle per second? – Ryan Ennis Nov 27 '17 at 20:05
• It can happen but very unlikely and most people will bang the keyboard if they wait too long anyway, so there shouldn't be a problem. All I can suggest about the >= is to double check the values you are comparing(gameTime & autoChangeTime) either by putting in a break point at the check or just before it you can also Console.Write them to check them. – Neil Nov 27 '17 at 23:10
• So I nested that if statement in another if statement that goes like this: if(gameTime.TotalGameTime.Seconds >= MinChangeTime) where MinChangeTime is an int. This blocks any user input until the time requirement is met. For some reason, greater than or equal to works in this case, @Neil. – Ryan Ennis Nov 28 '17 at 16:37

You are correct, if you simply set it (=) it will only ever be equal to the time since the last update call. If add that time each iteration however, you'll create the timer you originally needed.

 switch (currentState)
{
case States.otherStuff:
timer = 0;
if (!condition)
currentState = States.splashscreen;
break;
case States.splashscreen:
timer += gameTime.ElapsedGameTime.TotalSeconds;
if (condition || timer >= maxWaitTime)
currentState = States.otherStuff;
break;
default:
break;
}