Examples

From Project X Wiki
Jump to: navigation, search

Creating functions

int DistBetweenObjs(CGObject* ObA, CGObject* ObB)
{
 dx = ObA->PosX - ObB->PosX;
 dy = ObA->PosY - ObB->PosY;
 r = sqrt(dx * dx + dy * dy);
 return r;
};

Let's analyze this:

  • Create a function named DistBetweenObjs which returns an integer (int) which takes the arguments:
  1. Pointer to a CGObject (expressed by the *)
  2. Pointer to a CGObject
  • Calculate the X distance between the two objects (simple mathematics here)
  • Calculate the Y distance between the two objects
  • Calculate the square root of the X distance squared plus the Y distance squared
  • Return the square root.
  • End the function

As you can see, all the lines end with ";".

Overriding functions

override void CMine::Message(CObject* sender, EMType Type, int MSize, CMessageData* MData)
{
 super;
 if (Type != M_FRAME) return;
 
 for (local i = 1; i < Env->Objs.Count - 1; i+=1)
 {
   local obj = Env->Objs.Objs[i];
   
   if (obj == NullObj) continue;
   if (obj->ClType != OC_Worm) continue;
   
   local worm = CWorm(obj);
   if (ShootData.Team == worm->WormTeam) continue;
   
   
   
   if (DistBetweenObjs(obj, this) < 65)
   {
     PrintMessage(111);
     Active = 1;
     nPulses = 1;
     break;
   }; 
 };
};
  • Override the function Message in CMine which takes the arguments:
  1. A pointer to a CObject
  2. An EMType
  3. An integer which is the size of the message
  4. A pointer to a CMessageData which is almost unused.
  • super; -> Call the original function with exactly the same arguments we have (the game might want to draw or calculate the physics on us)
  • Check, if it isn't message for calculations, exit.
  • Iterate through every single object
    • If the object is a null object, continue (next object)
    • If the object's type isn't OC_Worm (not a worm), continue (next object)
    • Define a local variable (only usable inside this function) named "worm" which is a CWorm object.
    • If ShootData (CShootDesc)'s team (basically, the team of who fired this weapon) is the same team as our worm, continue (next object)
    • If the distance between the two objects (function above defined) is less than 65, then
      • Print the number 111 in a panel at the top (debugging)
      • Activate the mine
      • Without this line, mine wouldn't pulse correctly.
      • End the loop
      • End of distance comparison
    • End of loop
  • End of function

Please note that it is not needed to include ; after }