Editing KeyComm

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
Orbiter 2010: There is a new Vessel function, SendBufferedKey, which should be used instead of calling clbkConsumeBufferedKey directly.
 
 
 
From http://orbit.m6.net/Forum/default.aspx?g=posts&t=17569
 
From http://orbit.m6.net/Forum/default.aspx?g=posts&t=17569
  
 
We needed a way for a ship (or the Targetting MFD we made) to communicate with turret to tell it to pick up a target, start firing, stop firing, etc. The initial plan was to obtain the vessel pointer, cast it to a Turret, and call a method specifically. However, this has several undesired properties, such as requiring the calling MFD or ship code to include the turret header, etc, and thus isn't very extensible. Additionally, if we attached something other than a Turret (say for a fighter-type craft, where you have a missile directly attached to the ship), the object wouldn't cast nicely to a Turret.
 
We needed a way for a ship (or the Targetting MFD we made) to communicate with turret to tell it to pick up a target, start firing, stop firing, etc. The initial plan was to obtain the vessel pointer, cast it to a Turret, and call a method specifically. However, this has several undesired properties, such as requiring the calling MFD or ship code to include the turret header, etc, and thus isn't very extensible. Additionally, if we attached something other than a Turret (say for a fighter-type craft, where you have a missile directly attached to the ship), the object wouldn't cast nicely to a Turret.
Thus, we needed a function that's part of the normal [[VESSEL]] or [[VESSEL2]] class that we can overload to serve as our messaging function. clbkConsumeBufferedKey works very well for this purpose--it takes as parameters a DWORD, a boolean, and a char*, and returns an int. Normally, the DWORD parameter will not be greater than about 0xDF (the highest OAPI_KEY_xxx constant), and since there's 32 bits in a DWORD, that gives us only about 4.3 billion possible messages we can send to this function that won't be misinterpreted as keyboard input. More concretely:
+
Thus, we needed a function that's part of the normal VESSEL or VESSEL2 class that we can overload to serve as our messaging function. clbkConsumeBufferedKey works very well for this purpose--it takes as parameters a DWORD, a boolean, and a char*, and returns an int. Normally, the DWORD parameter will not be greater than about 0xDF (the highest OAPI_KEY_xxx constant), and since there's 64 bits in a DWORD, that gives us about 18,446,744,073,709,551,000 possible messages we can send to this function that won't be misinterpreted as keyboard input. More concretely:
 
The MFD and the turret both define a constant COMM_SET_TARGET to be some number greater than 0xFF. The MFD asks the user to input the name of the ship to target, obtains the turret's VESSEL pointer, casts it to a VESSEL2*, then calls
 
The MFD and the turret both define a constant COMM_SET_TARGET to be some number greater than 0xFF. The MFD asks the user to input the name of the ship to target, obtains the turret's VESSEL pointer, casts it to a VESSEL2*, then calls
  
Line 37: Line 35:
 
Code to send the message:
 
Code to send the message:
 
Code:
 
Code:
    const DWORD COMM_SET_TARGET = 257;//defined in header, 256 is max defined key value, so this must be higher than that
+
const DWORD COMM_SET_TARGET = 257;//defined in header, 256 is max defined key value, so this must be higher than that
  
    OBJHANDLE tarVessel = oapiGetVesselByName("messageTarget");
+
OBJHANDLE tarVessel = oapiGetVesselByName("messageTarget");
    char *str = "target";
+
char *str = "target";
    VESSEL2* ves = (VESSEL2*)oapiGetVesselInterface(tarVessel);
+
VESSEL2* ves = (VESSEL2*)oapiGetVesselInterface(tarVessel);
    bool validTargetRecieved = ves->clbkConsumeBufferedKey(COM_SET_TARGET,false,str);
+
bool validTargetRecieved = ves->clbkConsumeBufferedKey(COM_SET_TARGET,false,str);
  
  
 
This would be the corresponding code to receive this message:
 
This would be the corresponding code to receive this message:
 
Code:
 
Code:
    const DWORD COMM_SET_TARGET = 257;//defined in header
+
const DWORD COMM_SET_TARGET = 257;//defined in header
  
    bool Turret::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
+
bool Turret::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
        if (key == COMM_SET_TARGET) {
+
    if (key == COMM_SET_TARGET) {
            OBJHANDLE tarVessel = oapiGetVesselByName(kstate);
+
        OBJHANDLE tarVessel = oapiGetVesselByName(kstate);
            if(tarVessel == NULL)
+
        if(tarVessel == NULL)
                return 0;
+
            return 0;
            return 1;
+
        return 1;
        }
+
    }
 
     //the normal key handling code could go here
 
     //the normal key handling code could go here
  
Line 67: Line 65:
 
Code to send the message: (I apologize for the difficulty of reading it, this is how w3asel writes code. sigh.)
 
Code to send the message: (I apologize for the difficulty of reading it, this is how w3asel writes code. sigh.)
 
Code:
 
Code:
    const DWORD COMM_GET_TARGET = 259; //defined in header
+
const DWORD COMM_GET_TARGET = 259; //defined in header
  
    char Buffer[100];
+
char Buffer[100];
    Buffer[0] = 100;
+
Buffer[0] = 100;
    int hasTar = ((VESSEL2*)oapiGetVesselInterface(curVessel->GetAttachmentStatus(attachPoints[i])))->clbkConsumeBufferedKey(COMM_GET_TARGET, false, Buffer);
+
int hasTar = ((VESSEL2*)oapiGetVesselInterface(curVessel->GetAttachmentStatus(attachPoints[i])))->clbkConsumeBufferedKey(COMM_GET_TARGET, false, Buffer);
    TextOut(hDC, 190, 60 + i * 25, Buffer, strlen(Buffer));
+
TextOut(hDC, 190, 60 + i * 25, Buffer, strlen(Buffer));
  
  
 
This would be the corresponding code to receive this message:
 
This would be the corresponding code to receive this message:
 
Code:
 
Code:
    const DWORD COMM_GET_TARGET = 259;//defined in header
+
const DWORD COMM_GET_TARGET = 259;//defined in header
  
    bool Turret::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
+
bool Turret::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate) {
        if (key == COMM_GET_TARGET)
+
    if (key == COMM_GET_TARGET)
        {
+
    {
            if (tvessel == NULL)
+
        if (tvessel == NULL)
                return 0;
+
            return 0;
            char length = kstate[0];
+
        char length = kstate[0];
            oapiGetObjectName(tvessel,kstate,(int)length);
+
        oapiGetObjectName(tvessel,kstate,(int)length);
            return 1;
+
        return 1;
        }
+
    }
        //the normal key handling code could go here
+
    //the normal key handling code could go here
  
  
 
Note that there is still no prefix handled here, but this demonstrates how the "payload" can be used to return a value other than an int to the sending ship.
 
Note that there is still no prefix handled here, but this demonstrates how the "payload" can be used to return a value other than an int to the sending ship.
 
[[Category: Articles]]
 
[[Category: Tutorials]]
 

Please note that all contributions to OrbiterWiki are considered to be released under the GNU Free Documentation License 1.2 (see OrbiterWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

To protect the wiki against automated edit spam, we kindly ask you to solve the following hCaptcha:

Cancel Editing help (opens in new window)