OnChatMessage

From GTA Network Wiki
Jump to: navigation, search

OnChatMessage
Server-Side Function

utilities-terminal.png Server-Side csharp.png C#

Handling server chat.



Server-side

Subscribing

Note: the Attribute should be declared above the method to be invoked, it's alternative to subscribing the method to a delegate, introduced for performance reasons.

[ServerEvent(Event.ChatMessage)]

Method Syntax

void OnChatMessage(Client player, string message)

Required Parameters

  • Client: the player sending the message.
  • string: the message.

Example

  • An example for filtering bad words

We're gonna create a new OnChatMessage method and assign an Event Attribute to it to take over control of the server chat.

Note: DisableDefaultChat Attribute disables the default server chat behaviour which allows you to take over full control over the server chat. Has not been implemented yet (0.3.X), awaiting 0.4 release.

[DisableDefaultChat]
[ServerEvent(Event.ChatMessage)]
public void OnChatMessage(Client player, string message)
{
    if (message.Contains("Foo"))
    {
        NAPI.Chat.SendChatMessageToPlayer(player, "Prohibited word!");
        return;
    }

    NAPI.Chat.SendChatMessageToAll(player.Name + ": " + message);
}


Client-side

Subscribing

Note: the method should be subscribed to the respective event delegate in your Main constructor/Entry point for it to be invoked.

public Main() 
{
    Events.OnPlayerChat += OnPlayerChat;
}

Method Syntax

void OnPlayerChat(string text, RAGE.Events.CancelEventArgs cancel)

Required Parameters

  • text: text, expects System.String type.
  • cancel: cancel, expects RAGE.Events.CancelEventArgs type.

Example

Note: ternary operator ?: was used, for more info visit the Microsoft C# Reference docs

public void OnPlayerChat(string text, RAGE.Events.CancelEventArgs cancel)
{
    RAGE.Chat.Output(text.Contains("fuck") ? text.Replace("fuck", "f*ck") : text);
}

References