Getting Started with Remote Events

From GTA Network Wiki
Jump to: navigation, search

Getting Started with Remote Events

Basic Usage

Remote Events are events that are triggered by client packages. Previously, these were all forwarded to a single event but now they work similarly to commands.

C#:

[RemoteEvent("HelloEvents")]
public void HelloEvents(Client player, params object[] arguments)
{
    player.SendChatMessage("Hello World!");
}

Client JS:

//.. somewhere in your client packages
mp.events.callRemote("HelloEvents");

It will output Hello World! in the player's chat.

Side Note:

Event names can be specified and the function name will be ignored. However, you can set it to use the function name by simply doing as shown:

C#:

[RemoteEvent]
public void MyEventName(Client player, params object[] arguments)
{
    player.SendChatMessage("Hello World!");
}

Arguments

Basic Arguments

To get data from the client, you can use the provided arguments parameter.

C#:

[RemoteEvent("ArgumentEvents")]
public void ArgumentEvents(Client player, params object[] arguments)
{
    // you can check arguments.Length to verify how many you are expecting.
    bool isCool = (bool)arguments[0];
    int coolNumber = (int)arguments[1];
    
    if(isCool) 
    { 
        player.SendChatMessage($"You are {coolNumber} cool.");
    }
    else
    { 
       player.Kick("not cool");
    }
}


Client JS:

//.. somewhere in your client packages
mp.events.callRemote("ArgumentEvents", false, -99999);

Specified Arguments

As of 0.3.3 you can use remote events to expect certain data types (including multiplayer objects). Below is an example...

C#:

[RemoteEvent("ArgumentTypes")]
public void ArgumentTypes(Client player, bool isCool, int coolNumber, Client anotherClient)
{
    anotherClient.SendChatMessage("hey cool, client types");
    
    if(isCool) 
    { 
        player.SendChatMessage($"You are {coolNumber} cool.");
    }
    else
    { 
       player.Kick("not cool");
    }
}


Client JS:

//.. somewhere in your client packages
mp.events.callRemote("ArgumentTypes", false, -99999, somePlayerHandle);