Navigation » Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter SDK » [Discussion] Any advice on how to deal with timers, delays and such events?

Orbiter SDK Orbiter software developers post your questions and answers about the SDK, the API interface, meshing, etc here.

Reply
 
Thread Tools
Old 07-29-2010, 05:04 PM   #1
Moach
Crazy dude with a rocket
 
Moach's Avatar
 
Question Any advice on how to deal with timers, delays and such events?

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

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


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


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?)

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


any thoughts?
Moach is offline   Reply With Quote
Click here to register an account today!

Old 07-29-2010, 05:12 PM   #2
Urwumpe
Addon Developer
 
Urwumpe's Avatar
 
Default

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 is online now   Reply With Quote
Old 07-30-2010, 05:36 PM   #3
Urwumpe
Addon Developer
 
Urwumpe's Avatar
 
Default

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 by Urwumpe; 07-30-2010 at 05:39 PM.
Urwumpe is online now   Reply With Quote
Reply

  Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter SDK

Thread Tools

Posting Rules
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump