i'm currently struggling with what i believe is a rather simple problem. i'm working on a game of pong using c#. I'm trying to implement a 30 second countdown timer that will kick the player back to the main menu once the time has ran out.
Here is the method that handles starting a new game. I believe this would be the best place to put the timer.
public void StartNewGame()
{
scene = new GameScene(this);
//Start timer
//If timer <= 0
{
load main menu
}
}
I'm absolutely fine when it comes to loading up different scenes but i cant for the life of me work out how to use timers.
If you have any further questions please don't hesitate to ask and as always, any help is appreciated! Thanks.
You need to use threading for this.
On the one thread you will have your game running, containing some variable defining whether or not the game should still be running.
On the other thread you will simply sleep for the amount you'd want to wait. C# offers all kinds of fancy Timers if you care to look around for a library. Once the sleeping is done, or once your Timer class ends, you flip the flag in your game thread, which should make the game stop.
You can run your game thread like this:
Thread thread = new Thread();
thread.Start(() => { //start your game });
You can also use the Task
static class which might be better performance wise, as long as you run your game asynchronously.