Messaging

From Oldunreal-Wiki
Jump to navigation Jump to search

Messages are so fundementally important to good persona games that no mod should ever be without. This simple tutorial covers the basics of sending messages to players in the game. As a note, please realise that there are other and more simpler ways to do the functions explained in this tutorial. The point of this tutorial is to examine objects in the game so that a good understanding of how things work is elicited. Some 'other' methods are discussed at the end.

There are several reasons you would want to send messages to people in the game. Firstly, chatting to others enhances the experience. Messages to everyone, messaging to your team in a CTF or TF game, or perhaps personal messages to individual players. Secondly, many events that happen in the game require other players to be notified. Capturing a flag, dying or camping may be some examples.

Whatever your particular requirements are, there are three modes of messaging; all, team & individual. By examing how these functions work, we can modify the code to suit any purpose.

Broadcasting

Broadcasting is sending a message to everyone in the current level. The code to do this (stolen from the actor class in the Unreal source) is :

function BroadcastMessage( coerce string[240] Msg, bool bBeep )
{
   local Pawn P;
   if ( Level.Game.AllowsBroadcast(self, Len(Msg)) )
   {  
       for( P=Level.PawnList; P!=None; P=P.nextPawn )
       if( P.bIsPlayer )
           P.ClientMessage( Msg );
   }
}

What this function actually does is accept a message string and a boolean variable. It then cycles trough all of the pawns in the level, as it comes across one which is actually a player it calls the method of the pawn to display a message on their screen.

The main body of the code starts with an IF statement. The if statement actually references the 'Level' object which in turn has a 'Game' object which in turn has a method which is called AllowsBroadcast(). The AllowsBroadcast takes two parameters, the object which is calling it, and the length of the message.

If the game does allow broadcasting, i.e. it returns true, the code starts it's 'for' cycle. The 'for' loop utilizes a 'level' object list called 'PawnList'. This maintains all current pawns in the game. Remember though, that all objects in the game are pawns, not just the human players, so we need some way of checking if the pawns we're referencing are human.

Well, we're in luck. Each pawn has a boolean property called 'bIsPlayer' which is set by the engine when a pawn is created. To cycle through the list, call the 'nextPawn' method of the current pawn. If the pawn is a player, then calling the pawn's 'ClientMessage' method will send the passed parameter (the message) to the actual client machine.


Messaging Individuals

Sending a message to individuals is similar to broadcasting. Sort of selective broadcasting you could say. Using the code :

function MessageIndividual( coerce string[240] Msg, bool bBeep, string[20] sName)
{
 local Pawn P;
 if ( Level.Game.AllowsBroadcast(self, Len(Msg)) )
     for( P=Level.PawnList; P!=None; P=P.nextPawn )
         if( P.bIsPlayer && P.playername == sName)
             P.ClientMessage( Msg );
}
The code to send individual messages differs only in adding an extra parameter to the function (which we have called by a different name) and adding an extra condition to the final IF statement. All we have done is added a reference to the name of the player, and ensuring that when we cycle through the pawns, we check that we we find a player, his or her name is the same one that is passed to the function.

If you're wondering how to get the name of the player you want to send the messages to, it all depends on how you want to call the function. For example, you could create a new console command of 'TELL', taking two parameters, the name of the player and the message. i.e.

'tell neural9 Your mod sucks, dude!'

Alternatively, you could attach the function to various events & other functions in the game code itself. For example, if you wanted to send the player who bumps into you a message, you could use the 'actor' objects' 'touch' function like this :

function Touch( actor Other )
{
   MessageIndividual( "Stop bumping into me you geek !", true, other.name );
}

You could use the same method of selective broadcasting to send messages to teams in capture the flag or team fortress games. When you create a team actor object, you would have to add a 'teamname' property. By modifying the MesssageIndividual function to accept a teamname rather than a playername, you could select players to send the message to.