vsTASKER 7 Tutorial
×
Menu
Index

Detect and Fire

 
Let's create a basic task which will use the line of sight capability of the VBS-IG so that to automatically aim and fire at target whenever this one is visible from the tank.
For that we will open the Tank Logic and add some objects for this new behavior.
 
In the Tank Logic, let's add a new Task (right-click on the logic background):
 
 
Let's rename it: detection.
Then let's add one Entry Point and one Exit Point. Click on the green needle then click on the Task to add the EPoint then click on the red needle then click on the Task to add the XPoint.
Now, double-click each of them and add the b_B event as the reaction. This event is triggered by the gamepad when the B (red) button if depressed.
Do not forget the make the EPoint persistent so that to reactivate it when the task is terminated:
 
 
Now, open the detection task and add the following code in each section:
 
Declaration:
private:
  int req, req_id, resp;
  Entity* target;
 
Initialization:
case RESET: {
     req = resp = 0;
     target = S:findEntity("stingerL");
 
Runtime:
if (!req) {  // ask for a line of sight
   req_id = RANDOM(1,100);
   cigi.LoSsegmentReq->request(ent(), target, req_id);
   resp = 0;
   req = 1;
}
else { // wait for response
   if (cigi.RespLoS->hasResponse(req_id)) {
      if (cigi.RespLoS->isVisible(target, req_id)) {
         // we have line of sight !!! Inform the Logic
         logk()->raiseEvent("LoS", target);
      }
      req = 0;  // ready for a new request
   }
}
 
 
 
Create a new Task named autofire and add a new EPoint with the following settings:
 
Now, open the autofire task and add the following code in each section:
 
Declaration:
private:
    Entity* target;
    VBS_MainGun* mg;
 
Initialization:
case RESET: {
    target = (Entity*) edata();
    mg = E:findVBS_MainGun();
    mg->aimAt(target);
 
Runtime:
if (mg->ready()) {
   // fire
   mg->fireMainGun();
}
 
Compile and run.
Move the tank to a location where the stinger will eventually be seen, activate the detection with the B button and wait.