Problem need help with two virtual cockpits coding

Mr Martian

Orbinaut/Addon dev/Donator
Addon Developer
Donator
Joined
Jun 6, 2012
Messages
313
Reaction score
121
Points
43
Location
Sydney, Australia, Earth, Sol
Website
www.orbithangar.com
hey, i am trying to code a vessel that has two cockpits. i have it set up so that the user can toggle between cockpits by pressing the usual F8. but i am having some trouble. this is what i have so far:

Code:
bool Pegasus::clbkLoadVC (int id)
{ 
switch (id) {


case 0: // CM camera
SetCameraOffset (_V(0, -21.74421, 68.68874));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, -1, 1);
break;

case 1: // cockpit
SetCameraOffset (_V(0, 21.74421, 92.60211));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, 0, 2);
break;

break;

default:
return false;
}
return true;
}

this seems to work fine for me, but i can't seem to be able to switch between the two cases, i can only see case 0. i anyone knows how to help, i would love to hear your ideas.
 
Last edited:

STS

Well-known member
Joined
Feb 1, 2009
Messages
532
Reaction score
274
Points
78
Location
Vigo
Website
orbisondas.es
hey, i am trying to code a vessel that has two
Code:
bool Pegasus::clbkLoadVC (int id)
{ 
switch (id) {


case 0: // CM camera
SetCameraOffset (_V(0, -21.74421, 68.68874));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, -1, 1);
break;

case 1: // cockpit
SetCameraOffset (_V(0, 21.74421, 92.60211));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, 0, 2);
break;

[B][U]break;
[/U][/B] 
default:
return false;
}
return true;
}

Why 2 breaks on the second case?
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Did you press Ctrl+[Up|Down]Arrows to try switching between those views? :shrug:

Also, will there be 3rd view defined having id=2?
 

agentgonzo

Grounded since '09
Addon Developer
Joined
Feb 8, 2008
Messages
1,649
Reaction score
4
Points
38
Location
Hampshire, UK
Website
orbiter.quorg.org
Also, don't use ints to represent states. Use Enums. that's what they're designed for and make your code much easier to read/write

Code:
[B]enum View
{
    cm,
    cockpit
};[/B]

bool Pegasus::clbkLoadVC ([B]View [/B]id)
{ 
switch (id) {


case [B]cm[/B]: // CM camera
SetCameraOffset (_V(0, -21.74421, 68.68874));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, -1, 1);
break;

case [B]cockpit[/B]: // cockpit
SetCameraOffset (_V(0, 21.74421, 92.60211));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, 0, 2);
break;

break;

default:
return false;
}
return true;
}
 

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Code:
bool Pegasus::clbkLoadVC ([B]View [/B]id)

It won't work as there's no overloaded virtual method defined this way. The "bool clbkLoadVC (View id)" won't be called by Orbiter, unless you will call it from "bool clbkLoadVC (int id)".

You need to use:
Code:
bool Pegasus::clbkLoadVC ([B]int [/B]id)
callback which will be called by Orbiter and then cast it to View.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,635
Reaction score
2,352
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
The C(++) enums are a bit limited though, it is not possible in a simple way to save them to a file. you can convert them without problems into an int, but recompiling the file on another compiler could change this mapping.

Better would be providing a function that does the mapping from enum to int or back, but that is some effort... it is simpler in Java where reflection makes it possible to simply get the name of the enum variable as String.
 

agentgonzo

Grounded since '09
Addon Developer
Joined
Feb 8, 2008
Messages
1,649
Reaction score
4
Points
38
Location
Hampshire, UK
Website
orbiter.quorg.org
The C(++) enums are a bit limited though, it is not possible in a simple way to save them to a file. you can convert them without problems into an int, but recompiling the file on another compiler could change this mapping.

Better would be providing a function that does the mapping from enum to int or back, but that is some effort... it is simpler in Java where reflection makes it possible to simply get the name of the enum variable as String.
C++ does have a handy workaround for this which guarantees a rigid enum <==> int mapping no matter which compiler has been used:
Code:
enum myEnum {
   one = 1,
   two = 2,
   three = 3
}

It will compile it such that the enum entry 'one' gets given the value 1 etc.
Then you can very happily cast between int and myEnum.

To answer orb's point about the virtualisation of clbkLoadVc (sorry, didn't reasise what was going on here) you'd just cast the id to a View enum, then do the switch on that.

thus:
Code:
enum View
{
    cm = 0,
    cockpit = 1
};

bool Pegasus::clbkLoadVC (int id)
{ 
switch ((View) id) {


case cm: // CM camera
SetCameraOffset (_V(0, -21.74421, 68.68874));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, -1, 1);
break;

case cockpit: // cockpit
SetCameraOffset (_V(0, 21.74421, 92.60211));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, 0, 2);
break;

break;

default:
return false;
}
return true;
}
 
Last edited:

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Code:
[B]enum View
{
    cm,
    cockpit
};[/B]

bool Pegasus::clbkLoadVC ([B]View [/B]id)
{ 
switch (id) {


case [B]cm[/B]: // CM camera
SetCameraOffset (_V(0, -21.74421, 68.68874));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, -1, 1);
break;

case [B]cockpit[/B]: // cockpit
SetCameraOffset (_V(0, 21.74421, 92.60211));
oapiCameraSetCockpitDir(0,0); 
oapiVCSetNeighbours (-1, -1, 0, 2);
break;

break;

default:
return false;
}
return true;
}

Here's a corrected version of the callback, which will be now actually called from Orbiter, and additionally the View enum is now used to define the cockpit views with oapiVCSetNeighbours. The "bool clbkLoadVC (View id)" method can be declared as private.
Code:
enum View {
	cm [b]= 0[/b],
	cockpit,
	[b]not_defined_yet[/b]
};


[b]bool Pegasus::clbkLoadVC (int id) {
	return clbkLoadVC ((View)id);
}[/b]


bool Pegasus::clbkLoadVC (View id) {
	switch (id) {
	case cm: // CM camera
		SetCameraOffset (_V(0, -21.74421, 68.68874));
		oapiCameraSetCockpitDir(0,0); 
		oapiVCSetNeighbours (-1, -1, -1, [b]cockpit[/b]);
		break;

	case cockpit: // cockpit
		SetCameraOffset (_V(0, 21.74421, 92.60211));
		oapiCameraSetCockpitDir(0,0); 
		oapiVCSetNeighbours (-1, -1, [b]cm[/b], [b]not_defined_yet[/b]);
		break;

[b]//	case not_defined_yet:
//		break;[/b]

	default:
		return false;

	}
	return true;
}
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,635
Reaction score
2,352
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
C++ does have a handy workaround for this which guarantees a rigid enum <==> int mapping no matter which compiler has been used:
Code:
enum myEnum {
   one = 1,
   two = 2,
   three = 3
}

It will compile it such that the enum entry 'one' gets given the value 1 etc.
Then you can very happily cast between int and myEnum.

Yes, but you then of course have to use it (many are too lazy to type numbers of course), and I really recommend this solution for anything that is meant to be used as a public interface.
 

Izack

Non sequitur
Addon Developer
Joined
Feb 4, 2010
Messages
6,665
Reaction score
13
Points
113
Location
The Wilderness, N.B.
Yes, but you then of course have to use it (many are too lazy to type numbers of course), and I really recommend this solution for anything that is meant to be used as a public interface.

Even if it's not intended as a public interface, I'd say such things are essential. After all you may have to go back one day and read your code, and chances are you won't remember what id 0, 1, 2, etc. relate to. I just ran into this problem yesterday coming back to an Orbiter project, where a reactor's modes were defined as integer read from scenario file. It wasted about five minutes of looking through code to find out which logical state meant what.
 
Top