The TimeSpan class contains float values for in-game seconds, minutes, hours, days, weeks, months, seasons, and years. This is used in a variety of the GameTime methods, and can also be saved to isolate specific chunks of time that are important to your project.
Many methods which utilize this concept will allow both a TimeSpan object, or a list of floats designating the specific TimeSpan to utilize.
publicfloat Secondspublicfloat Minutespublicfloat Hourspublicfloat Dayspublicfloat Monthspublicfloat Years
TimeSpan does not track time itself, and does not decrease as time passes.
TimeSpan does not include Weeks or Seasons, but Gametime methods will include those when handling TimeSpan objects and doing date and time calculations.
Create a TimeSpan
// Create a new Timespan object for 2 weeks -- Each value is optional, defaults to 0TimeSpan myTimeSpan =newTimeSpan{ Weeks =2};// Create an empty Timespan, then populate itTimeSpan respawnTime =newTimeSpan();respawnTime.Years=1;respawnTime.Months=6;
publicTimeSpanMultiply(float multiplier)// ExampletimeSpan = timeSpan.Multiply(0.5f); // Cut the time in half
Remember that all values in a TimeSpan are based on in-game time.
Timespan Methods on Gametime.cs
These general methods will return a TimeSpan object, which may be saved and used later.
// Provides the difference between start and end. May be negative if // start is before end.publicTimeSpanTimeSpanBetween(float startGameTime,float endGameTime)// Provides the TimeSpan since one gameTime and another. Will use Now() if gameTime// is not provided. May return negative if otherGameTime is > gameTimepublic TimeSpan TimeSpanSince(float otherGameTime,float gameTime =-1)// Provides the TimeSpan until one gameTime and another. Will use Now() if gameTime// is not provided. May return negative if otherGameTime is < gameTimepublic TimeSpan TimeSpanUntil(float otherGameTime,float gameTime =-1)// ExampleTimeSpan timeSpanUntilNextWinterSolstice = TimeSpanUntil(NextWinterSolsticeGameTime());