SDK Question Setting VESSELSTATUS of an external ship.

escapetomsfate

OBSP Developer
Addon Developer
Joined
Jun 21, 2008
Messages
282
Reaction score
0
Points
0
Location
GB
How would I, say, set the altitude of a vessel that isn't the one in focus? The only thing I can think of is to use SetVessel, and the DefSetState, but that seems to be for initial initialisation.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,216
Reaction score
1,562
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
You can use GetStatus and DefSetState to modify a vessel's state directly. Note, however, that there is an Orbiter core bug (now fixed in the Orbiter beta version) with GetStatus that you will need to work around: DefSetState uses (i.e., examines) internal flag settings in VESSELSTATUS.flag[0], but GetStatus doesn't initialize that field. So if you just do this:

Code:
[SIZE=2][SIZE=2]VESSELSTATUS status;[/SIZE]
[SIZE=2][SIZE=2]pOtherVessel->GetStatus(status);[/SIZE]
[SIZE=2]// ... change status ...[/SIZE]
[SIZE=2]pOtherVessel->[SIZE=2]DefSetState(&status); [COLOR=red]// unsafe!![/COLOR][/SIZE][/SIZE][/SIZE][/SIZE]

...you can get intermittent CTDs or other weird behavior because the state of status.flag[0] is undefined (its value will depend on whatever happens to be on the stack at that location at runtime). As a workaround until the next Orbiter release, what you need to do is initialize the VESSELSTATUS structure to zeros before you invoke GetStatus:

Code:
[SIZE=2][SIZE=2]VESSELSTATUS status;[/SIZE][/SIZE]
[SIZE=2][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// core bug workaround: initialize entire structure to zero before invoking the read from the core.[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2]memset(&status, 0, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]sizeof[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](status));[/SIZE]
[SIZE=2][SIZE=2]pOtherVessel->GetStatus(status);   [/SIZE][/SIZE]
[SIZE=2][SIZE=2]// ... change status ...[/SIZE]
[SIZE=2]pOtherVessel->[SIZE=2]DefSetState(&status); [COLOR=green]// this is safe now[/COLOR][/SIZE][/SIZE][/SIZE][/SIZE]

 

escapetomsfate

OBSP Developer
Addon Developer
Joined
Jun 21, 2008
Messages
282
Reaction score
0
Points
0
Location
GB
Thank you. this works.

Just one more question;
How would I get the VESSEL handle from an OBJHANDLE? Can't find a relevant API.

EDIT: Of course, the vessel class constructor.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,216
Reaction score
1,562
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
Here's an example of how to iterate through all vessels in the sim:

Code:
for (unsigned int i=0; i < oapiGetVesselCount(); i++)
{
    const OBJHANDLE hVessel = oapiGetVesselByIndex(i);  // handle will always be valid here since we retrieved the handle from the core in this timestep
    const VESSEL *pVessel = oapiGetVesselInterface(hVessel);
    // If vessel is *us*, skip it!
    if (hVessel == GetHandle())
        continue;
    // ... use pVessel as desired here ...
}

Something to watch out for: if you ever save a vessel handle across timesteps in a member variable so it can be accessed the next timestep, be sure to always test the saved handle with oapiIsVessel before acccessing it in the new timestep. Otherwise, if the vessel is deleted and you try to access it, the handle (which is a pointer) will be invalid and you will get a CTD. That's why some add-ons don't work with the scenario editor (or with the XR's payload editor): they save vessel handles across timesteps but don't re-check them each timestep to make sure they haven't been deleted.
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
EDIT: Of course, the vessel class constructor.

No, if you do that you'll make another vessel.

To get the VESSEL pointer from an OBJHANDLE use:
oapiGetVesselInterface(OBJHANDLE);
 

escapetomsfate

OBSP Developer
Addon Developer
Joined
Jun 21, 2008
Messages
282
Reaction score
0
Points
0
Location
GB
No, if you do that you'll make another vessel.

To get the VESSEL pointer from an OBJHANDLE use:
oapiGetVesselInterface(OBJHANDLE);

Then why is there an OBJHANDLE in the constructor?
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
Then why is there an OBJHANDLE in the constructor?

Because Orbiter informs the constructor of the vessel's OBJHANDLE on creation?

Constructors construct things. Basic C++ knowledge. If you call the constructor of an object, by definition you are creating a new instance of that object.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,216
Reaction score
1,562
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
Hielor is correct; oapiGetVesselInterface is what you need use to obtain another vessel's VESSEL2 object from its Orbiter handle (see the code snipped posted earlier for details). Yes, each vessel already knows about its own OBJHANDLE (either from saving it in its constructor or by invoking GetHandle()), but the question here was how to retrieve a different vessel's VESSEL2 object -- constructors have nothing to do with that. If you want to use your own vessel's VESSEL2 object, there is nothing to "retrieve" -- just use the 'this' pointer (which, actually, is performed by the compiler automatically whenever you access a member variable or invoke a member method).
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
Bug:
I'm having a problem with the message not apearing when my ship explodes. I checked the config file and by what I saw in the file, the message should apear. Well, I tried firing the nuke and once it explodes, the thing crashes to the desktop to spite the fact that I was far from the explosion.

Well, to spite the little bug, the thing works quite well. And I can't seem to get that Martian shuttle in one of the challanges.

Is this supposed to be in the Orbiter Combat Dev thread?
 

escapetomsfate

OBSP Developer
Addon Developer
Joined
Jun 21, 2008
Messages
282
Reaction score
0
Points
0
Location
GB
Bug:
I'm having a problem with the message not apearing when my ship explodes. I checked the config file and by what I saw in the file, the message should apear. Well, I tried firing the nuke and once it explodes, the thing crashes to the desktop to spite the fact that I was far from the explosion.

Well, to spite the little bug, the thing works quite well. And I can't seem to get that Martian shuttle in one of the challanges.

For the message to appear, make sure that -

Damage is true
Msgondeath us true
Deadtext has a value

And try exploding a nuke near your ship - you should die and the annotation will appear.
As for catching the shuttle, that will take skill without guided missiles, which OBSP doesn't have yet.

And btw, could you post bug reports/questions like this in the development thread in future please?
 
Top