API Question Breaking VC off into seperate class

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
I'm not quite sure how to describe this so I'll just delve right in.

I'm trying to overhaul and clean up Spider's code.

A major factor in this effort has been the Virtual cockpit. It has many functions, sub functions, and variables associated with it and is the chief contributor to the code's current state of clutter and unreadability.

What I want to do is break all of the VCs associated items off into a seperate class that I can then call upon as needed.

Unfortunately I think I may have bitten off more than I can chew.

Mouse events are being processed but redraw events are not, displays and switch animations update when I exit and return to the VC but not while I am actually in it.

Has anyone here attempted something similar to this and if so how did you go about doing it?
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,627
Reaction score
2,345
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
We did so in SSU, you can look at the sources in the vc folder for inspiration. The VC code isn't great yet, but works well.
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
Thank you, looking at your panel code revealed the source of my problem.

Code:
bool LM::clbkVCMouseEvent (int id, int event, VECTOR3 &p)
{
vc->MouseEvent (id, event, p);
} // End "LM::clbkLoadVC"

needed to be...

Code:
bool LM::clbkVCMouseEvent (int id, int event, VECTOR3 &p)
{
	[COLOR="Red"]return [/COLOR]vc->MouseEvent (id, event, p);
} // End "LM::clbkLoadVC"


---------- Post added at 14:38 ---------- Previous post was at 10:23 ----------

ok new problem.

Loading the VC status from the scenario file. Initially I thought I could call a simple void as I did with clbkSaveState but it's not working and I'm not sure why.

VC's "Load state" function
// --------------------------------------------------------------
// Read status from scenario file
// --------------------------------------------------------------
void LM_Cockpit::LoadState (FILEHANDLE scn)
{
int i = 0;
char *cbuf

while (oapiReadScenario_nextline (scn, cbuf))
{
// Load Panel 1 switch positions
if (!_strnicmp( cbuf, "PANEL1_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P1_NSWITCH) && (*cbuf); i++) P1_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 2 switch positions
else if (!_strnicmp( cbuf, "PANEL2_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P2_NSWITCH) && (*cbuf); i++) P2_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 3 switch positions
else if (!_strnicmp( cbuf, "PANEL3_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P3_NSWITCH) && (*cbuf); i++) P3_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 4 switch positions
else if (!_strnicmp( cbuf, "PANEL4_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P4_NSWITCH) && (*cbuf); i++) P4_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 5 switch positions
else if (!_strnicmp( cbuf, "PANEL5_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P5_NSWITCH) && (*cbuf); i++) P5_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 8 switch positions
else if (!_strnicmp( cbuf, "PANEL8_SWITCHES", 15))
{
cbuf += 16;
for (i = 0; (i < P8_NSWITCH) && (*cbuf); i++) P8_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 12 switch positions
else if (!_strnicmp( cbuf, "PANEL12_SWITCHES", 16))
{
cbuf += 17;
for (i = 0; (i < P12_NSWITCH) && (*cbuf); i++) P12_Switch = (switchstate) (*cbuf++ - '0');
}

// Load Panel 14 switch positions
else if (!_strnicmp( cbuf, "PANEL14_SWITCHES", 16))
{
cbuf += 17;
for (i = 0; (i < P14_NSWITCH) && (*cbuf); i++) P14_Switch = (switchstate) (*cbuf++ - '0');
}
}
} // End "LM_Cockpit::LoadState"


function call in LM's clbkLoadStateEx
Code:
		else if (!_strnicmp (cbuf, "PANEL", 5))
		{
			vc->LoadState (scn);	
		}

		// Load default parameters
		else ParseScenarioLineEx (cbuf, status);

	} // End "while (oapiReadScenario_nextline (scn, cbuf))"
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
TLoading the VC status from the scenario file. Initially I thought I could call a simple void as I did with clbkSaveState but it's not working and I'm not sure why.
Is `vc` already initialized when the scenario is being loaded before simulation will start?
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
it's initialized in my vessel's class constructor.

but that does raise another possibility, does "clbkPostCreation" get called before or after "clbkLoadStateEx"?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
Ah yes :facepalm:

I just thought for a moment that my "saved state" might be getting overwritten by my "initial" (default) state.

---------- Post added 01-22-13 at 11:06 ---------- Previous post was 01-21-13 at 16:16 ----------

Wht I don't understand is that the code I used is a direct cut/past of the code that I used (and that worked) in Spider.

Can you think of any reason why putting it in a seperate void function as opposed to clbLoadStateEx it self would cause it to stop working?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Are PANEL1..PANEL14 lines defined after the PANEL line (a sub-scope)?

Something like this?:
Code:
PANEL
 PANEL1_SWITCHES 101010110001
 PANEL2_SWITCHES 011001001010
....
Or not?

In your code Orbiter reads a line that starts with PANEL and then in the LM_Cockpit::LoadState it reads the next line to check if it's PANEL[1-14]_SWITCHES, so in scenario it's like in the above example. But it also doesn't break the while loop, so it reads till the end of saved in the scenario vessel state, so other parameters could be read only when PANEL and then PANEL[1-14]_SWITCHES are at the end of the saved vessel state.

If it's the same line which holds the switch parameters, then you shouldn't read the next line in LM_Cockpit::LoadState, but instead of the file handle you should pass the pointer to already read line to it and parse it there.

If PANEL[1-14]_SWITCHES start in the next line defined by PANEL keyword sub-scope, then you should add another keyword which will end the sub-scope and break the while loop (with keeping the file handle parameter and reading next line in LM_Cockpit::LoadState).
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
Are PANEL1..PANEL14 lines defined after the PANEL line (a sub-scope)?

Something like this?:
Code:
PANEL
 PANEL1_SWITCHES 101010110001
 PANEL2_SWITCHES 011001001010
....
Or not?

Not.

I take it that's my problem?

I already tried deleting the read next line from LM_Cockpit::LoadState and passing cbuf to it as a a parameter but that didn't work either.

Code:
void LM_Cockpit::LoadState (FILEHANDLE scn, char *cbuf)
{
	int i = 0;
	//char *cbuf;

	//while (oapiReadScenario_nextline (scn, cbuf))
	//{
		// Load Panel 1 switch positions
		if (!_strnicmp (cbuf, "PANEL1_SWITCHES", 15)) 
		{
		cbuf += 16;
		for (i = 0; (i < P1_NSWITCH) && (*cbuf); i++) P1_Switch[i] = (switchstate) (*cbuf++ - '0');
		}

		// Load Panel 2 switch positions
		else if (!_strnicmp (cbuf, "PANEL2_SWITCHES", 15)) 
		{
		cbuf += 16;
		for (i = 0; (i < P2_NSWITCH) && (*cbuf); i++) P2_Switch[i] = (switchstate) (*cbuf++ - '0');
		}

etc...

In clbkLoadStateEx...

Code:
		// Load VC state
		else if (!_strnicmp (cbuf, "PANEL", 5))
		{
			vc->LoadState (scn, &cbuf);	
		}


This is propbably a monumentally stupid question but what's a sub-scope and how do I go about setting one up?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
I take it that's my problem?
If not this problem then another, which you'd find later.


I already tried deleting the read next line from LM_Cockpit::LoadState and passing cbuf to it as a a parameter but that didn't work either.
Does debugger stop in the function when you set a breakpoint at the first line in the function? If it does, can you step through every line and check whether conditions are correctly checked and variables are set?


This is propbably a monumentally stupid question but what's a sub-scope and how do I go about setting one up?

A sub-scope is a scope inside another scope. :p

It could be set for example something like this (multi level example):
Code:
BEGIN_SHIPS [color=red][i]; vessel list's scope start[/i][/color]
your_ship:your_class [color=red][i]; vessel's status scope - start of a sub-scope of vessel list[/i][/color]
    STATUS Landed Earth
    POS 0 0
    HEADING 0
    PANELS {[color=red][i]; panels' status scope - start of a sub-scope of vessel status[/i][/color]
        PANEL1 { [color=red][i]; panel1's scope - start of a sub-scope of panels' status[/i][/color]
            SWITCHES 01010011
            KNOBS 053103
        } [color=red][i]; end of panel1's scope[/i][/color]
        PANEL2 { [color=red][i]; panel2's scope - start of a sub-scope of panels' status[/i][/color]
            SWITCHES 110110
        } [color=red][i]; end of panel2's scope[/i][/color]
    } [color=red][i]; end of panels' status scope[/i][/color]
END [color=red][i]; end of vessel's status scope[/i][/color]
END_SHIPS [color=red][i]; end of vessel list's scope[/i][/color]
In this case you break the while loops inside every scope if the line starts with "}".
 

csanders

Addon Developer
Addon Developer
Joined
Jan 18, 2012
Messages
219
Reaction score
0
Points
0
Location
Plymouth
I notice the functions are iterating through "FILEHANDLE scn"

If one function iterated through "FILEHANDLE scn," and you use it again in another function, the handle may already be at the end, and it won't work.

Just thought I would throw that out as a common bug that comes up - the FILEHANDLE needs to be at the place in the file the function expects it.
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
Does debugger stop in the function when you set a breakpoint at the first line in the function? If it does, can you step through every line and check whether conditions are correctly checked and variables are set?

I'm not even sure what you're talking about.

There's a debugger module for orbiter?
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
There's debugger in Visual Studio / C++.

Correct, but it doesn't work with orbiter, or rather I've never been able to get it to work with orbiter.
 

csanders

Addon Developer
Addon Developer
Joined
Jan 18, 2012
Messages
219
Reaction score
0
Points
0
Location
Plymouth
I'm not even sure what you're talking about.

There's a debugger module for orbiter?

You can debug .dll you create in Visual Studios.

To do this, you need to have VS launch orbiter when you debug (F5). From there you can place break points in your .dll code, step through the code, check the values of variables, etc.

Using the debugger makes coding about 100X easier.

Go to Project->properties. It should look something like the following. This is for "WebMFD" which is in the [Orbiter root]\Orbitersdk\samples\WebMFD\ directory. Hence the ..\..\..\ puts the "Working Directory" to "[Orbiter root]" and the "Command" to "[Orbiter root]\orbiter.exe"
OrbiterDebug.png


PS: I would suggest using Orbiter in a "window" mode if possible. Otherwise when you hit a break point, Orbiter may get wonky.
 
Last edited:

marooder86

Donator
Donator
Joined
Dec 1, 2010
Messages
188
Reaction score
3
Points
33
Location
London
I'm not even sure what you're talking about.

There's a debugger module for orbiter?

Orb's talking about debugger program built in visual studio. That's what the Debug mode is for. When you compile your code in that mode you can then execute it through debbuger. This gives you some additional features like setting up the breakpoints, and watching variables values, call stack etc. Everywhere in code the breakpoint is set up the execution will halt, and you can then proceed in step mode, line by line, watching what's actually going on in the program.
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
Orb's talking about debugger program built in visual studio. That's what the Debug mode is for. When you compile your code in that mode you can then execute it through debbuger. This gives you some additional features like setting up the breakpoints, and watching variables values, call stack etc. Everywhere in code the breakpoint is set up the execution will halt, and you can then proceed in step mode, line by line, watching what's actually going on in the program.

I understand that, but as far as I can tell there's no way to run a vessel dll and have it load a scenario without running orbiter. and I haven't been able to get VS2010s debugger to run with orbiter.

---------- Post added at 15:24 ---------- Previous post was at 15:11 ----------

You can debug .dll you create in Visual Studios.

To do this, you need to have VS launch orbiter when you debug (F5). From there you can place break points in your .dll code, step through the code, check the values of variables, etc.

Using the debugger makes coding about 100X easier.

Go to Project->properties. It should look something like the following. This is for "WebMFD" which is in the [Orbiter root]\Orbitersdk\samples\WebMFD\ directory. Hence the ..\..\..\ puts the "Working Directory" to "[Orbiter root]" and the "Command" to "[Orbiter root]\orbiter.exe"
OrbiterDebug.png


PS: I would suggest using Orbiter in a "window" mode if possible. Otherwise when you hit a break point, Orbiter may get wonky.

Right but once VS2010 opens the blank orbiter launch pad window how do I get it to do anything?

Is there some sort of command line that I am unaware of?
 

marooder86

Donator
Donator
Joined
Dec 1, 2010
Messages
188
Reaction score
3
Points
33
Location
London
I understand that, but as far as I can tell there's no way to run a vessel dll and have it load a scenario without running orbiter.
That's true. In fact you can't do that with any other dlls as well, as they are not meant to be run as stand alone apps.
and I haven't been able to get VS2010s debugger to run with orbiter.
Try to follow csanders instructions.
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Right but once VS2010 opens the blank orbiter launch pad window how do I get it to do anything?
You need to set working directory to Orbiter's root folder.


(The best approach is of course to use Orbiter property sheets. :p)
 

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,870
Reaction score
3
Points
0
Location
San Diego
You need to set working directory to Orbiter's root folder.


(The best approach is of course to use Orbiter property sheets. :p)

I am using Orbiter's root folder as my working directory and I did use Orbiter property sheets.

When I run the debugger I get the following error... "Debug information binaries not available, would you like to continue any way?" I click yes and get a blank orbiter launch pad screen. Clicking on anything after that causes both orbiter and VS2010 to freeze.

That's what I mean when I say it "doesn't work".

But anyway we're getting side tracked.

The issue is not the debugger it's parsing the scenario file.

I know the values are being parsed because I'm also outputting them via oapidebugstring (my favorite function in orbiter bar none)

The question is why is moving the the function and switch variables from the LM vessel class to the LM_Cockpit class causing them to fail?
 
Last edited:
Top