Programming Question please help with custom vessel states

Mr Martian

Orbinaut/Addon dev/Donator
Addon Developer
Donator
Joined
Jun 6, 2012
Messages
317
Reaction score
124
Points
43
Location
Sydney, Australia, Earth, Sol
Website
www.orbithangar.com
hey guys i am trying to code a vessel that has multiple Vessel States. so far i have this:


in my .h:

Code:
enum VesselStatus {STAT1, STAT2, STAT3} VesselStatus;

enum PayloadStatus {ARMED, RELEASED, ACTIVE} PayloadStatus;

this works, but i cant save and load the PayloadStatus, only the VesselStatus. i know that i have to add a custom line in clbkSaveState, but i dont know how... if anyone could help, i would be grateful, this is what i have in clbkSaveState so far:


Code:
{
	int i = 0;
	char cbuf[256];
	
	// Write default vessel parameters to scenario file
	VESSEL3::clbkSaveState (scn);

	sprintf (cbuf, "%i", VesselStatus);							     oapiWriteScenario_string (scn, "STATE", cbuf);

	sprintf (cbuf, "%i", PayloadStatus);							     oapiWriteScenario_string (scn, "PAYLOAD", cbuf);

}


and in clbkLoadStateEx:


Code:
{
	int i = 0;
	char *cbuf; 				


	while (oapiReadScenario_nextline (scn, cbuf)) 
	{
		// Load Vessel State
		if (!_strnicmp( cbuf, "STATE", 5))  
		{
			sscanf(cbuf+5, "%i", &VesselStatus);
		}

		else if (!_strnicmp( cbuf, "PAYLOAD", 5))  
		{
			sscanf(cbuf+5, "%i", &PayloadStatus);
		}
}


but only VesselStatus is saved and loaded, not PayloadStatus...

thanks in advance :cheers:
 

Face

Well-known member
Orbiter Contributor
Addon Developer
Beta Tester
Joined
Mar 18, 2008
Messages
4,404
Reaction score
581
Points
153
Location
Vienna
Code:
{
    int i = 0;
    char *cbuf;                 


    while (oapiReadScenario_nextline (scn, cbuf)) 
    {
        // Load Vessel State
        if (!_strnicmp( cbuf, "STATE", 5))  
        {
            sscanf(cbuf+5, "%i", &VesselStatus);
        }

        else if (!_strnicmp( cbuf, "PAYLOAD", 5))  
        {
            sscanf(cbuf+5, "%i", &PayloadStatus);
        }
}
but only VesselStatus is saved and loaded, not PayloadStatus...

thanks in advance :cheers:

The function __strnicmp(char *string1, char *string2, int length) is comparing up to <length> characters. So it is already a small mistake there to use 5 for "PAYLOAD" instead of a correct 7. In your context, though, it wouldn't cause much harm, as other things are not called like "PAYLO", anyway.

The real problem is the sscanf call. You use "cbuf+5", which mean that the scanner starts 5 characters away from cbuf string's starting character, so it sees "AD <number>", of course converting that first part up until the first whitespace as integer. Unfortunately, "AD" converts to zero, so your code will never see the <number> and always set PayloadStatus to ARMED.

You can fix this by using "cbuf+7" instead.

regards,
Face
 
Top