From Project X Wiki
Jump to: navigation, search
Up one category:
void CObject::Message(CObject *sender, EMType Type, int MSize, CMessageData *MData);

The Message function is one key element in the process of scripting.

This function is invoked for every CObject class and derivate objects each time an "event" occurs in the game, the nature of which is determined by variable EMType Type.

Overriding this function allows you to program objects and general game behaviour when certain "events" take place.

In the next example we override the function Message of class CMine, to make a "friendly" mine:

override void CMine::Message(CObject* sender, EMType Type, int MSize, CMessageData* MData)
 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)
     Active = 1;
     nPulses = 1;
  • In this override, we first execute the original function, then we check if Type is equal to M_FRAME, such message is sent to every CObject once per physics step, for other message types, see EMType.
  • If the condition is not true, then we call operator return, because we only want to override the function when we receive a M_FRAME type message.
  • Next we iterate through every CObject to find every worm, excluding the ones of the team that fired the mine.
  • In the last part we check the distance between the mine and each non-friendly worm, and if its close enough, activate the mine.