SDK Question oapiOpenInputBox Conflict [SOLVED]

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Similar to this:http://www.orbiter-forum.com/showthread.php?t=27611&highlight=oapiOpenInputBox

I'm getting conflict (wiping data from selected base) and replacing with inputed height data, voiding autopilot.

I've tried to apply same solution to this InputBox but get errors.

What do I need to do to protect the data this time?

This code is before attempted "solution". (excuse messy coding)
Code:
bool InputHeight(void *id, char *sethgt, void *user_data)
{
	//double hgt;
	if (sscanf(sethgt,"%lf",&hgt, &rt) == 1 )
	{
	shgt = ((7.9+hgt)*0.30480);
	drate = 50;
         
	}

	if (sscanf(sethgt,"%lf %lf",&hgt, &rt) == 2 )
		{
			shgt = ((7.9+hgt)*0.30480);
		drate = rt;
				
}
	return true;
}

bool InputTarget(void *id, char *bstr, void *user_data)
{
	if(n == NULL)
	{
	VESSEL3 *vessel = (VESSEL3*)oapiGetFocusInterface();
	oapiSetHUDMode (HUD_SURFACE);
	
OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");

hBase = oapiGetBaseByName(hPlanet, bstr);

	double lat;
	double lon;
if (hBase == NULL &&sscanf_s(bstr,"%lf %lf",&lat,&lon) != 2 ) return false;

if (sscanf_s(bstr,"%lf %lf",&lon,&lat) == 2 )
{
	tgtlat = lat *RAD ;
	tgtlng = lon *RAD ;
		hBase = NULL;
		NAVType = 0;

	base = bstr;
[COLOR="Red"]((ShuttlePB *)user_data)->Base = base;[/COLOR]
		Nav = 1;

}

if(hBase != NULL)
{
			int nBase=oapiGetBaseCount(hPlanet);

			for (int i=0; i< nBase; i++) {
				
				base = bstr;
				}
			
[COLOR="red"]((ShuttlePB *)user_data)->Base = base;[/COLOR]
Nav = 1;
	}
}
 return true;
}

Have tried

Code:
char* base = NULL;
char* height = NULL;

Code:
class ShuttlePB: public VESSEL3{ //3 {
public:
......
	std::string Hgt;
.....

Code:
bool InputHeight(void *id, char *sethgt, void *user_data)
{
	
	if (sscanf(sethgt,"%lf",&hgt, &rt) == 1 )
	{
		height = sethgt;
		((ShuttlePB *)user_data)->Hgt = height;
	
		shgt = ((7.9+hgt)*0.30480);
		drate = 50;// standard descent rate
	}

	if (sscanf(sethgt,"%lf %lf",&hgt, &rt) == 2 )
		{
			shgt = ((7.9+hgt)*0.30480);
		drate = rt;
		
				
}
	return true;
}

But CTD on inputing height........ Why?
 
Last edited:

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
Code:
if (sscanf_s(bstr,"%lf %lf",&lon,&lat) == 2 )
{
	tgtlat = lat *RAD ;
	tgtlng = lon *RAD ;
		hBase = NULL;
		NAVType = 0;

	base = bstr;
((ShuttlePB *)user_data)->Base = base;
		Nav = 1;

}

What type is ShuttlePB::Base?
And for that matter, who or what is base? I can't see it declared anywhere in the function...
 

kamaz

Unicorn hunter
Addon Developer
Joined
Mar 31, 2012
Messages
2,298
Reaction score
4
Points
0
Try

base = strdup(bstr);

The problem is that the bstr buffer is not guaranteed to be valid outsite the callback -- it can be either disposed or reused.
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
it's in the morning, so my mind works a bit better now...
I have now seen that you declared base as a char*, but I can't see the context. Is that declared globally? if yes, for what reason?

Then the other matter, on which I'm a lot more certain, which might be connected. In your alternate attempt, you do

Code:
((ShuttlePB *)user_data)->Hgt = height;

where Hgt is an std::string and height a char*. There is no implicit conversion for the two!
You can turn a char* into a string very easily, but to do so you have to use one of std::string's constructors, not an operator. If ShuttlePB::Base is also an std::string, you'll have the same problem there (and if it is a char*, then you have an entirely different problem, namely that its content will change when base changes. The two would point to the exact same data. And if you have several instances of ShuttlePB in the simulation, then char* ShuttlePb::Base would point to the same data for every insatnce). This should work a lot better:

Code:
((ShuttlePB *)user_data)->Hgt = string(height);
 
Last edited:

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Well, I slung everything in there and it works - no conflicts!
Code:
bool InputHeight(void *id, char *sethgt, void *user_data)
{
	
	if (sscanf(sethgt,"%lf",&hgt, &rt) == 1 )
	{
	shgt = ((7.9+hgt)*0.30480);
	drate = 50;
	}

	if (sscanf(sethgt,"%lf %lf",&hgt, &rt) == 2 )
		{
			shgt = ((7.9+hgt)*0.30480);
		drate = rt;
		
		}
	return true;
}
////////////////////////////////////////////=========================================///////////////////////
bool InputTarget(void *id, char *bstr, void *user_data)
{
	if(n == NULL)
	{
	VESSEL3 *vessel = (VESSEL3*)oapiGetFocusInterface();
	oapiSetHUDMode (HUD_SURFACE);
	
OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");

hBase = oapiGetBaseByName(hPlanet, bstr);
	double lat;
	double lon;
if (hBase == NULL &&sscanf_s(bstr,"%lf %lf",&lat,&lon) != 2 ) return false;

if (sscanf_s(bstr,"%lf %lf",&lon,&lat) == 2 )
		{
		tgtlat = lat *RAD ;
		tgtlng = lon *RAD ;
			hBase = NULL;
			NAVType = 0;

	base = strdup(bstr);////////////////////////////////////////
	((ShuttlePB *)user_data)->Base = string(base);//////////////
			Nav = 1;
		}

if(hBase != NULL)
		{
			int nBase=oapiGetBaseCount(hPlanet);

			for (int i=0; i< nBase; i++) {
				
				base = strdup(bstr);
										}
			
((ShuttlePB *)user_data)->Base = string(base);////////////////
Nav = 1;
		}
	}
 return true;
}

Thanks guys. :thumbup::tiphat:
 

kamaz

Unicorn hunter
Addon Developer
Joined
Mar 31, 2012
Messages
2,298
Reaction score
4
Points
0
Nitpicking, but here:

for (int i=0; i< nBase; i++) {
base = strdup(bstr);
}

...the loop makes no sense, because bstr never changes inside the loop.
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Yeah, would appear that way.

You've probably guessed that I'm a "cut n paste" programmer with little or no tech know-how.
I just plod along till it works (with a little help from my friends :bighug: )
Don't know the reason why/how it works but it searches somewhere through the list of bases just fine.
It's so long ago that I nicked (I mean set) this code that I can't remember the logic. It's only caused a problem now cause I added the Height input function.

Anyways, please continue to put up with the likes of me as we get a whole lot of pleasure from tinkering about with these codes - and quite enjoy flying the things too. :lol:

Thanks to everyone, amateur n professional alike, and to the illustrious Doctor for participating in such a grand project as ORBITER !
:tiphat:
 
Last edited:
Top