API Question Callback when simulation starts

Kurt M. Weber

New member
Joined
Dec 7, 2008
Messages
98
Reaction score
0
Points
0
Location
Riverside
is opcOpenRenderViewport() appropriate for use for initializations that take place when the simulation starts? The documentation says it's for when you need to know how big the rendering window is, but that only takes place when you start the sim, so...

As a practical matter, I've tested it and it seems to work, but what I'm asking is if it's the correct way to do this?
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
I'm not sure if this is a correct way or not, but if I need to do something right after the sim starts, I typically use clbkprestep and a boolean flag. Inside the method I have an if(flag) and at the end of that, flag=false so it will only run once.
 

Kurt M. Weber

New member
Joined
Dec 7, 2008
Messages
98
Reaction score
0
Points
0
Location
Riverside
I'm not sure if this is a correct way or not, but if I need to do something right after the sim starts, I typically use clbkprestep and a boolean flag. Inside the method I have an if(flag) and at the end of that, flag=false so it will only run once.

But still (absent some really really funky optimization algorithm that I don't think is actually in use in any compiler--though I can envision how it would work), that flag would have to be checked every frame--which may not be terribly expensive, but is unnecessarily expensive if there exists a better way.
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
But still (absent some really really funky optimization algorithm that I don't think is actually in use in any compiler--though I can envision how it would work), that flag would have to be checked every frame--which may not be terribly expensive, but is unnecessarily expensive if there exists a better way.
opcOpenRenderViewport is how Orb:Connect does it's one-time initialization, and it's been working that way pretty well for awhile. It's also called after the simulation state is already established, right before things get going, so you should be able to access whatever simulation data you need.

One gotcha: Make sure you clean up anything declared in opcCloseRenderViewport, because it's possible for the simulation to be restarted (ie, user exits to the launchpad, and then starts again), and if you don't clean things up you could end up using variables left over from the previous run.

If the initialization can be done only once (and it's irrelevant if it persists across runs), InitModule may be preferable. The simulation state will not be available (since there's no simulation running), but that way you won't be affecting the sim load time.
 

Kurt M. Weber

New member
Joined
Dec 7, 2008
Messages
98
Reaction score
0
Points
0
Location
Riverside
One gotcha: Make sure you clean up anything declared in opcCloseRenderViewport

Goes without saying :D

If the initialization can be done only once (and it's irrelevant if it persists across runs), InitModule may be preferable.
In this case, it sets an alarm that, when expires, performs certain functions that only make sense in the context of the sim itself, so having it run right from Orbiter startup or module load is not what I want.
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
One gotcha: Make sure you clean up anything declared in opcCloseRenderViewport, because it's possible for the simulation to be restarted (ie, user exits to the launchpad, and then starts again), and if you don't clean things up you could end up using variables left over from the previous run.

Well only if for some reason the user is making orbiter deallocate the memory. By default orbiter respawns the process, so leaving things uncleaned wouldn't persist across subsequent runs, the process is simply terminated (as far as I know).

Code:
#define STRICT
#define ORBITER_MODULE
#include <orbitersdk.h>
bool test = false;
DLLCLBK void opcPreStep(double simt, double simdt, double mjd){
	if ( test )sprintf(oapiDebugString(), "false");
	if ( simt > 10 )test=true;
}

Run that with default settings, wait ten seconds, quit, then go back in simulation without restarting the launch pad. Now do the same thing after changing the settings in the extra tab to deallocate the memory. Then it'll persist across multiple runs.


opcOpenRenderViewport() is handy when something needs to be initialized that requires the simulation data. InitModule is good for initializing things such as GDI resources.
 
Top