Problem with Exports & Events (Callback) in C# (CSharp)

Hello,

I’m trying to learn C# and started writing C# based framework/gamemode from scratch, but now i’m stuck with FiveM Exports.
I’m trying to get a value from one server side resource to another using Exports, but for some reason when i register the export like so:
Method 1 (to return value):
Exports.Add("LoadCharacterSkin", new Func<int, int, byte, string>((handle, charId, gender) => LoadCharacterSkin(handle, charId, gender)));
It does not register or cant be called or whatever since i get an error in the console when i try to call it from another script via:
var skin = Exports["SNJRPPlayerCustomization"].LoadCharacterSkin(handle, id, (int)gender);

BUT
Now comes the funny part… When i use:
Method 2 (Without returning the value):
Exports.Add("LoadCharacterSkin", new Action(Test));
And call it from the other script with:
Exports["SNJRPPlayerCustomization"].Test();
Then the the export works…

Error when using Method 1:

I know this probably not the answer you want but I can attempt to find a fix for your exports problem. I usually call either an event from a separate script or build the PlayerCustomization into the script you are current working on again not an efficient method to do it but it’s something temporary until I find a fix.

1 Like

Little update! I managed to get the events working with callbacks, but so far it only works if i try to use it from server to server, when i try to trigger it from client → server to get a response from server, it just does nothing.

So currently my framework is triggering a server event from resource SNJRPPlayerCustomization and it gets a response like its supposed to with this:

Framework:

BaseScript.TriggerEvent("SNJRPPlayerCustomization:GetCharacterSkin", handle, charId, genderId, >new Action<dynamic>((callback) =>
{
   skin = callback.ToString();
}));

SNJRPPlayerCustomization:

private void GetCharacterSkin(int handle, int charId, byte gender, CallbackDelegate callback)
{
   callback.Invoke(skin.ToString());
}```

However, if i try to trigger “SNJRPPlayerCustomization:GetCharacterSkin” from SNJRPPlayerCustomization client script, then it gives the following error on server side:

When i tried to change CallbackDelegate to NetworkCallbackDelegate, then even the server->server triggering wont work and instead of “Could not cast event argument from NetworkCallbackDelegate to CallbackDelegate” it says “Could not cast event argument from CallbackDelegate to NetworkCallbackDelegate” -_-

Still havent figured out the exports yet.

interesting because I’m getting the same error. I’m not 100% sure why it’s happening as it would be the same thing for LUA but because of C# being C# its Exports

I only really use exports I use is mainly for the spawn manager

Exports[“spawnmanager”].setAutoSpawn(false);

Well yeah, like you can see from the first post, that kind of export works, but i need to exchange information between scripts and triggering 3 events to get one piece information is silly. At least i got the eventhandler working with the callback for now, gonna smoke my brains how to get information to the client script when i need to.

I’ll think of something

Okay, BIG update, i’ve figured out how to pass data from server → client, client → server and from client → client and server → server via Events.

So basically if you want to pass from:
client → client or server → server
you need to use CallbackDelegate like so:

Resource1/Server.cs

TriggerEvent("Resource2:ServerEventToGetData", param1, new Action<dynamic>((callback) =>
{
    Debug.WriteLine("Callback: " + callback.ToString());
}));

Resource2/Server.cs

[EventHandler("Resource2:ServerEventToGetData")]
private async void ServerEventToGetDataToClient(int param1, CallbackDelegate callback)
{
   callback(ToJson(someDataThatIGotFromThisResource));
}

NOW, if you want to pass information from:
client → server or server → client
you need to use NetworkCallbackDelegate like so:

Resource1/Client.cs

TriggerServerEvent("Resource2:ServerEventToGetDataToClient", param1, new Action<dynamic>((networkCB) =>
{
    Debug.WriteLine(ToJson(networkCB));
}));

Resource2/Server.cs

[EventHandler("Resource2:ServerEventToGetDataToClient")]
private void ServerEventToGetDataToClient([FromSource] Player player, int param1, NetworkCallbackDelegate networkCB)
{
    networkCB(ToJson(someDataThatIGotFromThisResource));
}

No update on exports yet.
Hard to learn something without examples/documentation available :smiley:

1 Like

Alright I did it with exports.

Resource1/ClientMain.cs

 public class ClientMain : BaseScript
 {
     [Command("test")]
     private void CommandTest()
     {
         Exports["test2"].ExportMessage(true);
     }
 }

Resource 2/ClientMain.cs

public ClientMain()
{
    Exports.Add("ExportMessage", ExportMessage);
}

public bool ExportMessage(bool isTrue)
{
    if (isTrue is true)
    {
        Debug.WriteLine("true");
    }
    else
    {
        Debug.WriteLine("false");
    }

    return isTrue;
}

Hey,

Ye i got exports working also, but from what i gather they only work from client → client and server → server? Cant call server event from client?

Sorry I didn’t see this until now. From my experience I don’t think you can. That’s why I suggested calling an event. Sorry.

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