Orbiter-Forum  

Go Back   Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter SDK
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

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

Reply
 
Thread Tools
Old 08-06-2012, 05:16 PM   #1
naunau
Orbinaut
Default different values of variable for several ships of same class?

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:

	double my_simt;
};
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.
naunau is offline   Reply With Quote
Old 08-06-2012, 06:21 PM   #2
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

Quote:
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.
jedidia is offline   Reply With Quote
Old 08-06-2012, 06:22 PM   #3
Zatnikitelman
Wandering lonely as a cloud
 
Zatnikitelman's Avatar
Default

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!
Zatnikitelman is offline   Reply With Quote
Old 08-06-2012, 06:38 PM   #4
naunau
Orbinaut
Default

Quote:
Originally Posted by jedidia View Post
 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);
				my_simt=osmt;
			};

			ParseScenarioLineEx (line, vs);
	}
}
have several ships of a class.
after loading the values from * scn of variables my_simt all ships are equal
naunau is offline   Reply With Quote
Old 08-06-2012, 08:17 PM   #5
jedidia
shoemaker without legs
 
jedidia's Avatar
Default

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);
	}
}
jedidia is offline   Reply With Quote
Old 08-06-2012, 08:47 PM   #6
orb
O-F Administrator
Ninja
 
orb's Avatar

Default

Quote:
Originally Posted by jedidia View Post
 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.
orb is offline   Reply With Quote
Thanked by:
Old 08-06-2012, 08:59 PM   #7
Zatnikitelman
Wandering lonely as a cloud
 
Zatnikitelman's Avatar
Default

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.
Zatnikitelman is offline   Reply With Quote
Old 08-07-2012, 11:57 AM   #8
naunau
Orbinaut
Default

Quote:
Originally Posted by Zatnikitelman View Post
 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:

	double my_var;
};

void zst_t::clbkSetClassCaps (FILEHANDLE cfg)
{
.....
	my_var=0;
....
};

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

void zst_t::clbkDrawHUD (int mode, const HUDPAINTSPEC* hps, HDC hdc)
{
	VESSEL2::clbkDrawHUD(mode,hps,hdc); 
	TCHAR szTime[30];
	WORD n=sprintf(szTime, "%f", my_var);
	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)
{
...............
	VESSEL *vessel = oapiGetFocusInterface(); // Get current vessel
	double as = ves->GetAirspeed();
	double K0 = ves->GetAtmTemperature();
	double CP = 2010;
	double my_var=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 my_var=K0+(as*as)/CP;
...............
};
where i can read the sequence of procedure calls (clbkPreStep and etc.), and conditions of procedure call?

Last edited by naunau; 08-07-2012 at 06:44 AM.
naunau is offline   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


All times are GMT. The time now is 04:42 AM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2007 - 2012, Orbiter-Forum.com. All rights reserved.