[ESX] FiveM C# Wrapper

No idea if there is anything like this already

Hello,
I could vomit from Lua and decided to code a wrapper for the ESX framework to make it somewhat “easier” to develop things.

This is for Developers, not for cp skids who just download everything and lets see.

  • YOU CAN’T USE THIS ALONE, NOT A RESOURCE
  • ONLY TESTED IT WITH ESX V1 FINAL BUT IT PROBABLY WORKS WITH OLDER VERSIONS

So what is this, and.. what can I do with this?

Probably you can do anything that you were able to do in the past, just in C#.

Nexd.ESX.Client contains everything that is available on clientside using ESX (And some extra stuff)
Nexd.ESX.Server contains everything that is available on serverside using ESX (And some extra stuff)

Nexd.ESX.Shared contains (get accessors only)

  • Job class (job related properties)
  • Weapon class (use Shared.Weapon on clientside)
  • InventoryItem class
  • Account class (Bank, Money, Black)

Examples (Assume that you’ve set the references already and got the basic things)

ESX.RegisterServerCallback and ESX.TriggerServerCallback

Serverside script:

ESX.RegisterServerCallback("esx_testScript:testCallBack", new Action<int, CallbackDelegate, dynamic>(TestCallback));

private void TestCallback(int source, CallbackDelegate cb, dynamic args)
{
     //You may do the same things here as in Lua, like prechecks etc.. then return something
     var data = args; //parsed from the client (needless line tho)
     Debug.WriteLine($"SERVER.TESTCALLBACK {source} {data.cucc[0]} {data.cucc[1]}"); //client parsed 'dynamic args' that contains 'cucc', lets write its value

     //random things to test that every kind of sh!t can be passed to the callback
     List<string> tempData = new List<string>();
     tempData.Add("a");
     tempData.Add("b");
     tempData.Add("c");

     //Lets call the client callback and pass whatever we want to
     cb.Invoke(new //this will be a new dynamic value that will be parsed to the client
     {
          test = new[] { "testData", "128asua" }, //dynamic param.test
          other = 15, //dynamic param.other
          tempData //dynamic param.tempData[0] or param.tempData.Count etc.. its still a List<> so you can do whatever you can with a list
          //and so on..
     });
}

Clientside script:

private void TestTrigger(int source, List<object> args, string raw)
{
      ESX.TriggerServerCallback("esx_testScript:testCallBack", new Action<dynamic>((param) =>//param is parsed from the server
      {
            Debug.WriteLine($"CLIENT.TESTCALLBACK {param.test[0]} {param.test[1]} {param.other} {param.tempData.Count}"); //test is the 'string[]', other is the 'int' and the tempData is the List that we've parsed
      }), new {
            cucc = new[] { "asd", "sad" } //data that we've parsed to the server (data.cucc[0], data.cucc[1])
      });
}
xPlayer, xPlayer.TriggerEvent and Job

Serverside script:

private void JobInfo(int source, List<object> args, string raw)
{
    xPlayer xPlayer = ESX.GetPlayerFromId(source);
    Job Job = xPlayer.GetJob();
    xPlayer.TriggerEvent("chat:addMessage", new
    {
        color = new[] { 255, 0, 0 },
        args = new[] { "[JobInfo]", $"Your Job is {Job.name}. Your Grade Is {Job.grade_name}. Your Salary is {Job.grade_salary}" }
    });
}
DialogMenu
private void DialogMenuTest(int source, List<object> args, string raw)
{
        ESX.UI.Menu.CloseAll();

        ESX.UI.Menu.Open("dialog", GetCurrentResourceName(), "dialog_menu", new ESX.UI.MenuData
        {
                title = "Dialog Menu Title"
        }, new Action<dynamic, dynamic>((data, menu) => { //Submit
                ESX.ShowNotification($"{data.value}");
        }), new Action<dynamic, dynamic>((data, menu) => { //Cancel
                menu.close();
        }));
}

More clientside example
More serverside example

I’m not planning on creating any doc for this project because you can use the Lua version to understand what is what, unless its needed

8 Likes

Although I mainly stay with LUA it’s good to see this, good job, keep it up.

1 Like

I just was talking with my colleges about make a C# Wrapper to make our life easier this morning and now you appear with this , Sometimes the universe timing is wonderful. :rofl:

2 Likes

The developer I’m working with and I literally did this last night for our C#, crazy timing yup!

2 Likes

Ever consider making a JS wrapper?

I seriously wanna learn C# rn :sob:
Good release

1 Like

Thanks!
Uploaded 1.0.1 to GitHub with the following changelog:
-Removed Obsolete attribute from methods that use VehicleProperties (Thought its useless but its not!)
-Fixed ESX.GetPlayers() that returned the player ids instead of xPlayer

1.0.2 changes:
Changed the default value of ‘args’ to null in xPlayer.TriggerEvent() in case if you don’t want to pass anything to the handler (You could pass null tho)
Added overloads for SetCoords, UpdateCoords, GetCoords to support both lua table & Vector3 (xPlayer class)
Added xPlayer.GetHeading() (not available in lua)
Added xPlayer.Index to get the player index (You could achieve the same using xPlayer.Raw.source)

Gonna release some resource using this to show more & more examples

1 Like

Didn’t you measure whether the performance was higher or there was no difference?

mmmmm i already saw this day 1 u had it on github… :heart:
I love the love for C#

1 Like

I don’t think so

I don’t think there is any difference, but afaik C# scripts runs at higher ms than lua by default

I made a topic on my docs of how to use ESX with JS

https://docs.adren.xyz/guides-1/using-esx-functions-with-fivem-js

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.