CancelEventArgs

From GTA Network Wiki
Jump to: navigation, search

CancelEventArgs

Intoduction

This tutorial will hopefully demonstrate how the CancelEventArgs are useful and how should they be used to avoid confusion.

Overriding server controlled spawn

PlayerConnected


Cancelling player connection

This will kick the player

void OnPlayerConnected(Client player, CancelEventArgs e)
{
    // Somewhere it is declared that 'PlayerBanned' is true
    if (PlayerBanned)
    {
        player.SendChatMessage("You are banned bro, sry.");
        e.Cancel = true; // Will kick the player
        return; // It is very important to return so that the rest of the code is not executed
    } 
    // ..
    // Some code that would run if the previous condition is not true
    // ..
}

Changing default spawn location

This part is unrelated to CancelEventArgs and is here to demonstrate the functionality as an alternative to e.Spawn, it involves the usage of SetDefaultSpawnLocation, useful if you just want to change the default global spawn location

NOTE: Requires e.Spawn in OnPlayerConnected/OnPlayerDeath events to be set to true, which by default is set to true

TIP: Use this once at resource start

void OnResourceStart()
{
    // This will simply change the default global player spawn location. NOTE: the X, Y, Z and rotation are random values. :kappa:
    NAPI.Server.SetDefaultSpawnLocation(new Vector3(1, 2, 3), 3f);
}

Handling spawning manually

If you would like to handle spawning players on your own.. this is useful if in case you would like to randomise the spawn location

void OnPlayerConnected(Client player, CancelEventArgs e)
{
    e.Spawn = false; // This will stop the execution of the default server handled spawning
    NAPI.Player.SpawnPlayer(player, new Vector3(0, 0, 0)); // Will spawn the player at the given position
}

PlayerDeath


Instant respawn

This will respawn the player instantly after death

void OnPlayerDeath(Client player, NetHandle entityKiller, uint weapon, CancelEventArgs e)
{
    e.Spawn = false; // Will stop the execution of the default respawning
    // The player will lie on the ground forever if we do nothing about it, basically a 'Death Simulator' Multiplayer

    // To avoid that, the following API call is required
    NAPI.Player.SpawnPlayer(player, new Vector3()); // The player will now respawn at the given position instantly
}

Delayed respawn

If you would like to delay the resapwn of the player after his death without blocking other code

Read more about Task, to understand the differences between await and the NAPI.Task.Run((Func), delayTime);

void OnPlayerDeath(Client player, NetHandle entityKiller, uint weapon, CancelEventArgs e)
{
    e.Spawn = false;

    // The following function will run asynchronously, meaning that it will not freeze everything just to wait for the player respawn
    Task.Run(async () =>
    {
        NAPI.Task.Run(() =>
        {
            NAPI.Player.SpawnPlayer(player, new Vector3());
            NAPI.Chat.SendChatMessageToPlayer(player, "Greetings! you have been respawned!");
        }, 4000); // We delay the task invocation by 4 seconds while executing the rest of the asynchronous code bellow

        // .. some code I want to execute while this asynchronous task is on-going
    });

    // ..
    // some other code to be ran while the player is respawning
    // ..
}