OnClientEventTrigger

From GT-MP
Jump to: navigation, search
OnClientEventTrigger
Server-Side Function
Utilities-terminal.png Server-Side Csharp.png C#

This type of event is used to communicate with Client-side scripts, it is activated when the (client-side) script requests an event that cannot be accomplished on client side. (Client-side scripts reference)

Requirements

First of all, it should be defined in your Main constructor for it to be called, otherwise it wouldn't be initiated at all!

   public Main()
   {
      API.onClientEventTrigger += OnClientEvent;
   }

Usage example(s)

Be advised, using large variable types can allocate more than the required memory, which is a waste of memory. Please refer to: Integral Types Table (C# Reference)

   public void OnClientEvent(Client player, string eventName, params object[] arguments) //arguments param can contain multiple params
   {
      if (eventName == "Foo") //an eventname with no params that was triggered from the Client-side script
      {
         API.kickPlayer(player, "FOOs don't belong here!"); //code can be anything that does not exist or cannot be called inside the client-side script
      }

      else if (eventName == "heretakemysecret") //an eventname with a single param
      {
         string secret = arguments[0].ToString(); //Since the param came as an object, it should be converted to a string for it to be used as a string
         API.consoleOutput("Wow! He told me his secret which was: " + secret);
      }

      else if (eventName == "my3cents") //an eventname with multiple params
      {
         byte number1 = (byte)arguments[0]; //Or "byte number1 = Convert.ToByte(argument[0]);" both work the same way, the first is more practical and easier
         byte number2 = (byte)arguments[1]; //params can be of an infinite number but that will not be necessary
         API.consoleOutput("Well, the first result is: " + number1 + ", and the second is: " + number2);
      }
   }

References