Networking
Namespace:
DeadworksManaged.Api
The server and each player's game talk to each other with net messages. Your plugin can send its own, and read or block the ones already being sent.
See what messages exist
The protobuf database lists every net message in the game and the fields each one has.
Send a message to everyone
Create the message, fill in its fields, and send it.
var msg = new CCitadelUserMsg_HudGameAnnouncement
{
TitleLocstring = "GAME OVER",
DescriptionLocstring = "The match has ended"
};
NetMessages.Send(msg, RecipientFilter.All);
CCitadelUserMsg_HudGameAnnouncement shows a large announcement on the screen.
Send a message to one player
NetMessages.Send(msg, RecipientFilter.Single(controller.Slot));
Send a message to some players
Start with an empty RecipientFilter and add the players you want.
var filter = new RecipientFilter();
foreach (var controller in Players.GetAll())
{
if (controller.GetHeroPawn()?.TeamNum == 2)
filter.Add(controller.Slot);
}
NetMessages.Send(msg, filter);
Read a message the server is sending
Put [NetMessageHandler] on a method that takes an OutgoingMessageContext for the message you want.
[NetMessageHandler]
public HookResult OnChatMsg(OutgoingMessageContext<CCitadelUserMsg_ChatMsg> ctx)
{
Console.WriteLine(ctx.Message.Text);
return HookResult.Continue;
}
ctx.Message is the message. ctx.Recipients holds the players it is going to.
Block a message
Return HookResult.Stop and nobody receives it.
[NetMessageHandler]
public HookResult OnChatMsg(OutgoingMessageContext<CCitadelUserMsg_ChatMsg> ctx)
{
return HookResult.Stop;
}
To hide it from one player, remove them from the recipients:
var recipients = ctx.Recipients;
recipients.Remove(controller.Slot);
ctx.Recipients = recipients;
Changing ctx.Recipients in place has no effect. Copy it into a variable, change the copy, and assign it back, as above.
Read a message a player sends
Take an IncomingMessageContext instead.
[NetMessageHandler]
public HookResult OnChatMsgFromPlayer(IncomingMessageContext<CCitadelUserMsg_ChatMsg> ctx)
{
var sender = Players.FromSlot(ctx.SenderSlot);
return HookResult.Continue;
}
ctx.SenderSlot is the slot of the player who sent it.
To read chat, use OnChatMessage instead.
See also
- Chat: send and read chat messages.
- Protobuf database: every net message and its fields.