Sending Custom Messages

From NetGore Wiki

Jump to: navigation, search
NetGore Tutorial
Difficulty 1/5
Topics Game content,Networking
(view all tutorials)

This tutorial is for people who want to categorise the way the chat messages are handled and shown in the chat box. What I mean by this is that the server won't just be accepting 'Messages' from the client, but will have the ability to see messages as:

..and any other type of message you add into your game.


So what can you do with this? Well, you can have different fonts, colours and many other various formats that you want, to make your NetGore game more user-friendly and customised to your liking.

This is an example tutorial on how to do the above for Group Messages!

Contents

ServerPacketID

First we need to add the new message type.

Go To: DemoGame.Server.ServerPacketID

Just After:

SendMessage,

Add:

SendGroupMessage,

ServerPacket

We need the server to send the new type instead of a generic message.

Go To: DemoGame.Server.ServerPacket

Just After:

public static PacketWriter SendMessage(GameMessage gameMessage, params object[] p)
{
    var pw = GetWriter(ServerPacketID.SendMessage);
    pw.Write(gameMessage, p);
    return pw;
}

Add:

public static PacketWriter SendGroupMessage(GameMessage gameMessage, params object[] p)
{
    var pw = GetWriter(ServerPacketID.SendGroupMessage);
    pw.Write(gameMessage, p);
    return pw;
}

ClientPacketHandler

The client now needs to be able to receive and correctly handle the message.

Go To: DemoGame.Client.ClientPacketHandler

Just After:

[MessageHandler((uint)ServerPacketID.SendMessage)]
void RecvSendMessage(IIPSocket conn, BitStream r)
{
    var message = r.ReadGameMessage(GameMessageCollection.CurrentLanguage);
 
    if (string.IsNullOrEmpty(message))
    {
        const string errmsg = "Received empty or null GameMessage.";
        if (log.IsErrorEnabled)
            log.Error(errmsg);
        return;
    }
 
    GameplayScreen.AppendToChatOutput(message, Color.Black);
}

Add:

[MessageHandler((uint)ServerPacketID.SendGroupMessage)]
void RecvSendGroupMessage(IIPSocket conn, BitStream r)
{
    var message = r.ReadGameMessage(GameMessageCollection.CurrentLanguage);
 
    if (string.IsNullOrEmpty(message))
    {
        const string errmsg = "Received empty or null GameMessage.";
        if (log.IsErrorEnabled)
            log.Error(errmsg);
        return;
    }
 
    GameplayScreen.AppendToChatOutput(message, Color.Blue);
}

User

Now the server needs to actually send the message to the appropriate users.

Go To: DemoGame.Server.User

Just After:

/// <summary>
/// Sends data to the client. This method is thread-safe.
/// </summary>
/// <param name="message">GameMessage to send to the User.</param>
/// <param name="messageType">The <see cref="ServerMessageType"/> to use for sending the <paramref name="message"/>.</param>
/// <param name="parameters">Message parameters.</param>
public void Send(GameMessage message, ServerMessageType messageType, params object[] parameters)
{
    using (var pw = ServerPacket.SendMessage(message, parameters))
    {
        Send(pw, messageType);
    }
}

Add:

/// <summary>
/// Sends group data to the client. This method is thread-safe.
/// </summary>
/// <param name="message">GameMessage to send to the User.</param>
/// <param name="messageType">The <see cref="ServerMessageType"/> to use for sending the <paramref name="message"/>.</param>
/// <param name="parameters">Message parameters.</param>
public void SendToGroup(GameMessage message, ServerMessageType messageType, params object[] parameters)
{
    using (var pw = ServerPacket.SendGroupMessage(message, parameters))
    {
        Send(pw, messageType);
    }
}

SayHandlerCommands.Group

Finally we want to use the new message type's sending method instead of the generic one.

Go To: DemoGame.Server.SayHandlerCommands

Find:

/// <summary>
/// Sends a message to all group members.
/// </summary>
/// <param name="message">The message to send.</param>
[SayHandlerCommand("GroupSay")]
public void PartySay(string message)
{
    if (!RequireInGroup())
        return;
 
    foreach (var groupMember in User.Group.Members.OfType<User>())
    {
        groupMember.Send(GameMessage.GroupSay, ServerMessageType.GUIChat, User.Name, message);
    }
}

Change To:

/// <summary>
/// Sends a message to all group members.
/// </summary>
/// <param name="message">The message to send.</param>
[SayHandlerCommand("GroupSay")]
public void PartySay(string message)
{
    if (!RequireInGroup())
        return;
 
    foreach (var groupMember in User.Group.Members.OfType<User>())
    {
        groupMember.SendToGroup(GameMessage.GroupSay, ServerMessageType.GUIChat, User.Name, message);
    }
}

See also

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox