Difference between revisions of "Message"

From Project X Wiki
Jump to: navigation, search
(Undo revision 1192 by DonaldTurner (talk) - Spam, Vandalism)
m (Reverted edits by DonaldTurner (talk) to last revision by StepS)
 
(One intermediate revision by one other user not shown)
(No difference)

Latest revision as of 09:36, 10 August 2012

Up one category:
CObject
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)
{
 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;
   }; 
 };
};
  • 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.