Call

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

If your script has multiple classes that inherit Script, and you would like to call a method in one of the classes from another, you can use API.call to do so.

Syntax

API.call(string className, string methodName, params object[] arguments);

Required Arguments

  • className: The name of the class you are trying to call. Parameter input should be in string type.
  • methodName: The name of the method you are trying to call. Parameter input should be in string type.
  • arguments: List of arguments.

NOTE: This function returns null if the method return type is void.

Usage example(s)

// Script1.cs
public class Script1 : Script
{
    public void DoSomething(string argument1, int argument2) {
        // Do something
    }
}

// Script2.cs
public class Script2 : Script
{
    public void DoSomethingElse() {
        API.call("Script1", "DoSomething", "argument 1", 2);
    }
}

Please note, if you're compiling your resource to .DLL, it's recommended to use C#'s nameof() function. This makes it easier to refactor class and method names, as it will throw an error if used on a method or class name that doesn't exist. Example:

API.call(nameof(Script1), nameof(Script1.DoSomething), "argument 1", 2);

References