NetworkProcess
void NetworkProcess (ConsoleEvent e)
Usage
Detects when network events are sent. Network events can either be sent by a player from the console using the netevent eventname
command, where eventname is the actual name of the event, or when EventHandler.SendNetworkEvent("eventname")
is called in ZScript.
Both methods support up to 3 numeric arguments, such as:
netevent MyNetworkEvent 15 2
— calls the event "MyNetworkEvent" with the first argument set to 15, and the second argument set to 2.EventHandler.SentNetworkEvent("MyNetworkEvent", 15, 2)
— does the same from ZScript.
One common usage for network events is to communicate between ui and play scopes, such as sending a command from the UI that modifies something on the gameplay level, since SendNetworkEvent
can be called from any scope.
For the opposite case (modifying UI from play scope) see InterfaceProcess.
![]() |
Warning: When overriding NetworkProcess, ALWAYS check e.name first to make sure your code is only triggered by the event you want to call. If you don't, your override will be triggered by all network events, regardless of their name!
|
Passed values
The following values are passed from the ConsoleEvent struct to this event, and can be read if preceded with the e.
prefix:
- int player
- The number of the player who activated the event, beginning with 0 for Player #1.
- String Name
- The name of the event as a string. This supports string formatting methods.
- int Args[3]
- The arguments passed to the event.
- bool IsManual
- This will be
true
if the event was activated by a player using thenetevent
console command. Otherwise this will befalse
.
Examples
This override is designed to catch the event named "GiveClip". If it's detected, it'll give the calling player Clip in the amount equal to the first argument of the event. It can be called both from the console, and with SendNetworkEvent. For example, typing netevent GiveClip 400
will give the player 400 Clip:
class ClipOnDemand : EventHandler
{
override void NetworkProcess (ConsoleEvent e)
{
// Check the name of the event in a case-insensitive manner:
if (e.name ~== "GiveClip")
{
// Make sure this player is valid
// and has a PlayerPawn:
if (!PlayerInGame[e.Player])
{
return;
}
let ppawn = players[e.Player].mo;
if (!ppawn)
{
return;
}
// all checks passed - give this PlayerPawn Clip ammo:
ppawn.A_GiveInventory('Clip', e.args[0]);
}
}
}
This is an alternative version that will give the player any item as long as it's a valid item and its name is placed in the event's name after a colon. For example, EventHandler.SendNetworkEvent("GiveItem:Clip", 400)
will give the player 400 Clip. This is achieved with string formatting methods, by splitting the name of the event at the :
symbol, and then casting the second part to a class<Inventory> type variable to check if it resolves to a valid class. Additionally, this event will not work if the player calls it from the console directly.
class ItemOnDemand : EventHandler
{
override void NetworkProcess (ConsoleEvent e)
{
// Make sure "GiveItem" is a part of the event's name
// and the event was NOT called from the console:
if (e.name.IndexOf("GiveItem") >= 0 && !e.IsManual)
{
// Make sure this player is valid
// and has a PlayerPawn:
if (!PlayerInGame[e.Player])
{
return;
}
let ppawn = players[e.Player].mo;
if (!ppawn)
{
return;
}
Array<string> command;
e.Name.Split (command, ":");
if(command.Size() == 2)
{
// Cast the part of the event's name after ":"
// as an Inventory class:
class<Inventory> cls = command[1];
// If that class exists, give the player this item:
if (cls)
{
ppawn.GiveInventory (cls, e.args[0]);
}
}
}
}
}