OnPlayerDeath

From GTA Network Wiki
Jump to: navigation, search

OnPlayerDeath
Shared Function

utilities-terminal.png Server-Side system-users.png Client-Side csharp.png C#

This type of event is used for handling code when someone dies.

NOTE: Death reason can be of either WeaponHash or DeathReason enum


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.PlayerDeath)]

Method Syntax

void OnPlayerDeath(Client player, Client killer, uint reason)

Required Parameters

  • Client: player who died
  • Client: killer (null if inexistent)
  • uint: reason of death

Usage example(s)

[ServerEvent(Event.PlayerDeath)]
public void OnPlayerDeath(Client player, Client killer, uint reason)
{
    if (!killer.IsNull)
    {
        NAPI.Notification.SendNotificationToAll($"{killer.Name} killed {player.Name}");
    }
    else
    {
        NAPI.Notification.SendNotificationToAll($"{player.Name} died");
    }
    // You could have used ternary operation there but this is just a newbie friendly example, if you're interested here's the one liner:
    // NAPI.Notification.SendNotificationToAll(killer.IsNull ? $"{player.Name} died" : $"{killer.Name} killed {player.Name}");
}

  • An example for a resource controlled player respawn on death

We're gonna create a new OnPlayerDeath method and assign an Event Attribute to it to take over control of the server player death event.

In order to disable the default respawn on death behavior and have full control over the player death event, add DisableDefaultOnDeathRespawn attribute on top of you player death event method.

Note: Disabling the default server respawn on death behaviour without a functional death event handler will result in a Death Simulator experience for your players

[DisableDefaultOnDeathRespawn]
[ServerEvent(Event.PlayerDeath)]
public void OnPlayerDeath(Client player, Client killer, uint reason)
{
    // Some code
}

  • Instant respawn at a specific location
[DisableDefaultOnDeathRespawn]
[ServerEvent(Event.PlayerDeath)]
public void OnPlayerDeath(Client player, Client killer, uint reason)
{
    NAPI.Player.SpawnPlayer(player, new Vector3(13, 11, 32)); // The player will now respawn at the given position instantly
}

  • Delayed respawn at a specific location

This will delay the resapwn of the player on death without blocking main thread. Read more about Task.

[DisableDefaultOnDeathRespawn]
[ServerEvent(Event.PlayerDeath)]
public void OnPlayerDeath(Client player, Client killer, uint reason)
{
    NAPI.Task.Run(() =>
    {
        NAPI.Player.SpawnPlayer(player, new Vector3(13, 11, 32));
        NAPI.Chat.SendChatMessageToPlayer(player, "Greetings! you have been respawned!");
    }, delayTime: 4000); // We delay the task invocation by 4 seconds
}


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.OnPlayerDeath += OnPlayerDeath;
}

Method Syntax

void OnPlayerDeath(Player player, uint reason, Player killer, CancelEventArgs cancel)

Required Parameters

  • player: the player that died, expects RAGE.Elements.Player type.
  • reason: the reason for death, expects System.UInt32 type.
  • killer: killer, expects RAGE.Elements.Player type.
  • cancel: cancel, expects RAGE.Events.CancelEventArgs type.

Example

The example below sends a chat message to play when they enter a vehicle, showing whether they are driver or passenger.

public void OnPlayerDeath(RAGE.Elements.Player player, uint reason, RAGE.Elements.Player killer, RAGE.Events.CancelEventArgs cancel)
{
    if (killer == null)
    {
        RAGE.Chat.Output($"{player.Name} just died ");
    }
    else if (reason == 539292904)
    {
        RAGE.Chat.Output($"{player.Name} was killed  by {killer.Name} in an explosion");
    }
    else
    {
        RAGE.Chat.Output($"{player.Name} was killed by {killer.Name} ");
    }
}

References