How would you implement flexible time?

Hi, I am new here, just learning things about in monogame.
I struck upon a small problem, so I decided it was time I ask the gurus to see how other would handle this.
Thank you in advance, I hope I am not being inappropriate ;(

So, here’s my problem. A lot of games have “flexible time”.
By this, I mean gametime flowing x2, x3 times faster than it should…

Is your character sleeping? Does your wonder construction take a few real-time hours?
Press this button, and BAM! its done in a few moments! kind of thing.

Its nothing new or unique, so I assume everyone knows what I am on about :wink:

Here’s my gut feeling of how I think I want to tackle this problem.

  1. I would like to set up a code so that it could adjust the amount of time added to GameTime,
    in effect just speeding things up single-handed-ly… Though I am afraid it is being way too inefficient.

  2. So maybe my best chances are to split up main gametime with another value, which is basically (gametime * float) or something similar, and make only the necessary things to speed up.

Problem is… I don’t know how I can implement any of them. So I don’t even know what’s best for me :frowning:

I understand that this topic can be quite large and vague, so I am not asking for very specific replies.
Though I could really use some guidance over how I should tackle this problem…
A link to a tutorial, or an example project if there’re any out there, would be very nice,
but I will be happy and welcoming anything at all.

TL;DR : title says it all. Though specific answer may be difficult, please help a poor soul asking for guidance…
edit : word & nuances

I’ve not done much monogame stuff, but have an idea on this. I don’t think it will be possible to change the game time, as that is the time from the point of running the game.

For the “construction” object you would need a construction timer - this timer would be updated everytime the “update()” method is called - so say the timer is set in minutes, you would have 120 for 2 hours of construction time, then in the update reduce this value by time elapsed, when it gets to zero then BAM built.

Now the only way to speed this up that I can think of is to use an overall “GameSpeed” variable - default being set to 1 - when the user speeds up the game x2, x4 - then change this value.

Once you’ve added that function in, you will need to edit the construction update() method to take this into account… construction.time - (elapsed * gamespeed)

I’m sure the more experienced developers on monogame will help with a better idea though.

1 Like

One way to do this is by using elapsed time and a scale.

For example first you get your elapsed time
Something like
Floats dt = (float)game time.elaspedtime.totalseconds;

Next you scale this to value of 1. Since we know that we update 60* a second we know that dt should be 1/60

So you could do this
Timeframe = 60 * dt;

Now we have a time scale variable and multiply that by timeframe

Time frame *= scale;

Now you can use this variable in all your calculations to see how far an object moves etc

Ps sorry for formatting replayed from my phone

Edit sorry fixed error with the Timeframe forumla

1 Like

Cool! thank you guys, great thinking.

Incidentally, I just wrote a “WorldTime” class for my game, which basically keeps track of current time of a day, and day of a week. Maybe you’ll find it useful.

The important part is the AddTime method. As mentioned before in this thread, normally it’s updated in every frame with the elapsed milliseconds time, possibly multiplied by some global multiplier (in my case the multipler is 20.0f, so one in-game minute is 3 real-time seconds). But to skip a large time period, you can just call AddTime with some large number of milliseconds.

1 Like

I’ve got a handy set of classes that I use everywhere that can replicate this effect:
Create a GameClock object
Set the TimerSpeed = 2
When you Update the timer with the provided gameTime object, it will “double” the amount of time that has passed. Pretty simple :slight_smile:

Source, if you’s like to see how I did it:

or just use the nuget:

Hope this helps. Cheers!

1 Like