Discussion Any advice on how to deal with timers, delays and such events?

Moach

Crazy dude with a rocket
Addon Developer
Joined
Aug 6, 2008
Messages
1,581
Reaction score
62
Points
63
Location
Vancouver, BC
i've come to a point in eveloping the G42 that i'm ready to begin coding the simulation logic behind the workings of the ship:cheers:

first thing i thought i'd do, is add a "mode switch" delay between engine configurations....

while this could be achieved easily by using "in-transition" states, this isn't much of a reusable approach, and will increase the spaghettiness of my module a bit :rolleyes:


so i was wondering how i could proceed with this and similar situations... in Flash and JavaScript, which i'm largely accustomed to, i'd just use setTimeout() and have it call a function after a set amount of milliseconds....

but c++ has no such function :shifty:


would it be worthy, or even advisable at all, to create my own implementation of setInterval? - and in doing so, should i go with using a separate thread, or do i have a loop checking live intervals?

i assume the latter would be more suitable, and safe to use... since Orbiter itself appears to run single-threaded (does it?) :uhh:

but with the advent of whole-bunch-of-cores-CPU's, i can't help but to feel like threading is the "proper way" of doing things :huh:


any thoughts?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,616
Reaction score
2,337
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
Define a double precision counter variable in the vessel class, and subtract the Delta-SimT every time step from it... or use the variable to store the simulation time when the timer was started and compare it to the current simulation time.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,616
Reaction score
2,337
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
As tiny addition... I usually define 2-3 general purpose timer variables in my add-on code (dfTimerA, dfTimerB, dfTimerC) which can be used in the sequence state automate. I never needed more than three so far.

Usually I initialize the variables with 0.0 and in PreStep decrement them by the length of the time step, if they are bigger than 0.0. A typical application would look like that:


  1. In the transition code from State q to State r, I set the available timer variable to the delay.
  2. In State r, I wait until the timer is zero again, and then transition to State s. This could for example include a staging event during the transition.
  3. In State s, I do what ever was needed to be delayed...for example a different autopilot
 
Last edited:
Top