Skip to content

Instantly share code, notes, and snippets.

@csmith
Created December 3, 2015 11:31
Show Gist options
  • Save csmith/9ed5994d84d52afceb24 to your computer and use it in GitHub Desktop.
Save csmith/9ed5994d84d52afceb24 to your computer and use it in GitHub Desktop.
Demo SpaceEngineers script showing use of ElapsedTime while accounting for various edge cases
TimeSpan LastElapsedTime;
void Main(string argument)
{
if (ElapsedTime.TotalSeconds == 0)
{
// First run. The script has just been created/compiled, or the game was loaded.
SetupThings();
}
else if (LastElapsedTime == null || ElapsedTime < LastElapsedTime.Add(TimeSpan.FromSeconds(10)))
{
// ElapsedTime looks reasonable: either we have nothing to compare it to, or it's
// within 10 seconds of the previous value.
DoThings();
LastElapsedTime = ElapsedTime;
}
else
{
// ElapsedTime is weird. Maybe we've unpaused?
LastElapsedTime = ElapsedTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment