C++ Question different values of variable for several ships of same class?

naunau

New member
Joined
Jul 4, 2012
Messages
3
Reaction score
0
Points
0
Sorry for my English.
I do count my_simt.
for example:
Code:
class zst_t: public VESSEL2 {
public:
	zst_t (OBJHANDLE hVessel, int flightmodel): VESSEL2 (hVessel, flightmodel) 
	{
	}
	void clbkSetClassCaps (FILEHANDLE cfg);
	void clbkLoadStateEx (FILEHANDLE scn, void *vs);
	void clbkSaveState (FILEHANDLE scn);
	void clbkPreStep (double simt, double simdt, double mjd);
	int clbkConsumeBufferedKey (DWORD key, bool down, char *kstate);
	void clbkDrawHUD (int mode, const HUDPAINTSPEC* hps, HDC hdc);
private:

	[B]double my_simt[/B];
};

as a result all ships of class zst_t value my_simt is identical.
It is necessary to have some ships of class zst_t value my_simt were different.

How do I do?
I am not an expert C + +
Thank you.
 
as a result all ships of class zst_t value my_simt is identical.

They are not identical as a result of this. Class instances can of course store individual values for their members, so that's certainly not the problem here. How is the variable updated? the reason for the identical values probably lies there.

Also, I would recommend deriving your class from VESSEL3, as VESSEL2 is somewhat outdated.
 
With C++ the way you have it now, each my_simt is a different variable. So to have it be different between vessels, just assign it a value. If it was declared with the static keyword, it would have the same value across all classes. So you can just assign it within a method somewhere and it won't change for all vessels.

Also, welcome to the forum!
 
How is the variable updated?


Code:
void zst_t::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{		
	char *line;
	while (oapiReadScenario_nextline (scn, line)) {
	

		if (!_strnicmp (line, "MY_SIMTIME", 10)) 
			{
				double osmt;
				sscanf (line+10, "%lf", &osmt);
				[B]my_simt=osmt;[/B]
			};

			ParseScenarioLineEx (line, vs);
	}
}

have several ships of a class.
after loading the values from * scn of variables my_simt all ships are equal :(
 
Code:
sscanf (line+10, "%lf", &osmt);

I'm not sure you can do the "line + 10" thing. But I'm not fluent enough in old C functions to say for sure, but I can't find it used anywhere in this way.

One thing I can tell for sure is that "+10 is wrong. You don't just have the parameter name to skip, you also have a space to skip. So if anything, it should be +11, but as I said, I don't know if that is even allowed. Since a char string is actualy nothing but a simple array, I would say no.

I usually put the char into an std::string and then use its substring function, like this:

Code:
void zst_t::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{		
	char *line;
        string s = "";
	while (oapiReadScenario_nextline (scn, line)) {
	

		if (!_strnicmp (line, "MY_SIMTIME", 10)) 
			{
                                s = line;
				double osmt;
				sscanf (s.substr(11).data(), "%lf", &osmt);
				my_simt=osmt;
			};

			ParseScenarioLineEx (line, vs);
	}
}
 
One thing I can tell for sure is that "+10 is wrong. You don't just have the parameter name to skip, you also have a space to skip. So if anything, it should be +11, but as I said, I don't know if that is even allowed. Since a char string is actualy nothing but a simple array, I would say no.
The sscanf will actually skip the leading white space to read the double value. Adding a numeric value is allowed, since 'line' is a pointer to char, which is just an integer (address of memory location).

Those fancy C++ functions do similar things, but it's hidden from the coder, and making additional conversions between char[] and string one and another way makes larger and much slower code.
 
To jedidia: using (abusing?) sscanf and char*'s in that manner have not failed me yet. Whether it's proper or not, I don't know, but I do know that it works.

To naunau: are you sure the scenario file has each ship with different MY_SIMTIME values? That should work and looks virtually identical to how I load scenario file values.
 
To naunau: are you sure the scenario file has each ship with different MY_SIMTIME values? That should work and looks virtually identical to how I load scenario file values.
Values MY_SIMTIME in scn file some ships is different. It has been edited in notepad.

I experimented so. calculated the number of keystrokes "W".
Code:
class zst_t: public VESSEL2 {
public:
	zst_t (OBJHANDLE hVessel, int flightmodel): VESSEL2 (hVessel, flightmodel) 
	{
	}
	void clbkSetClassCaps (FILEHANDLE cfg);
	void clbkLoadStateEx (FILEHANDLE scn, void *vs);
	void clbkSaveState (FILEHANDLE scn);
	void clbkPreStep (double simt, double simdt, double mjd);
	int clbkConsumeBufferedKey (DWORD key, bool down, char *kstate);
	void clbkDrawHUD (int mode, const HUDPAINTSPEC* hps, HDC hdc);
private:

	[B]double my_var[/B];
};

void zst_t::clbkSetClassCaps (FILEHANDLE cfg)
{
.....
	[B]my_var[/B]=0;
....
};

int zst_t::clbkConsumeBufferedKey (DWORD key, bool down, char *kstate)
{
if (key == OAPI_KEY_W && down) 
	{
		[B]my_var[/B]++;
	};
};

void zst_t::clbkDrawHUD (int mode, const HUDPAINTSPEC* hps, HDC hdc)
{
	VESSEL2::clbkDrawHUD(mode,hps,hdc); 
	TCHAR szTime[30];
	WORD n=sprintf(szTime, "%f", [B]my_var[/B]);
	TextOutA(hdc,0,0, LPCSTR( szTime) ,n);
	
};

so everything is fine.
My calculations are in zst_cb :: clbkPreStep

seems to zst_cb :: clbkPreStep value of the variable are mixed.
there are some features in zst_cb :: clbkPreStep?

---------- Post added at 11:57 AM ---------- Previous post was at 04:10 AM ----------

in the clbkPreStep, I had the following code:

Code:
void zst_t::clbkPreStep (double simt, double simdt, double mjd)
{
...............
[B]	VESSEL *vessel = oapiGetFocusInterface(); // Get current vessel
[/B]	double as = ves->GetAirspeed();
	double K0 = ves->GetAtmTemperature();
	double CP = 2010;
	double [B]my_var[/B]=K0+(as*as)/CP;
...............
};

it turns out that the clbkPreStep is called for each ship
so my variables become the same.

I had to write it.
Code:
void zst_t::clbkPreStep (double simt, double simdt, double mjd)
{
...............
	double as = GetAirspeed();
	double K0 = GetAtmTemperature();
	double CP = 2010;
	double [B]my_var[/B]=K0+(as*as)/CP;
...............
};

where i can read the sequence of procedure calls (clbkPreStep and etc.), and conditions of procedure call?
 
Last edited:
Back
Top