Hello,
I have been trying to invoke a server event from a client using C#.
So from the client code it looks like this:
public class SynCoreClient : BaseScript
{
public SynCoreClient()
{
Tick += OnTick;
}
private async Task OnTick()
{
await Delay(1000);
//Does nothing?
TriggerEvent("test");
//Throws error (because client doesn't have this native).
//TriggerClientEvent("test");
Debug.WriteLine("Invoking test from client");
}
}
And my server code looks like this:
public class SynCoreServer : BaseScript
{
public SynCoreServer()
{
EventHandlers.Add("test", new Action(() =>
{
Debug.WriteLine("This is a test");
}));
Debug.WriteLine("[SYN-CORE] Server library loaded");
}
}
And my __resource.lua:
client_script {
"SynCore.net.dll", -- Shared library
"SynCoreClient.net.dll",
}
server_script {
"SynCore.net.dll", -- Shared library
"SynCoreServer.net.dll",
}
I don’t see why it doesn’t get fire on the server except that there is no-way for the client to invoke a server event.
I can confirm the dll’s are loaded on both client and server.
So i’ve have looked into the implementation on the FiveM github and came to the conclusion that invoking from client to server seems impossible, unless I misunderstood what the actual native does.
TriggerEvent has 2 arguments:
(code from FiveM repo)
public static void TriggerEvent(string eventName, params object[] args)
it calls the TriggerEventInternal, which has 3 arguments:
private static void TriggerEventInternal(string eventName, byte[] argsSerialized, bool isRemote)
TriggerEventInternal seems to have a “isRemote” and what that triggers is that the hash being invoked is changed from TRIGGER_EVENT_INTERNAL to TRIGGER_SERVER_EVENT_INTERNAL.
By setting isRemote “should” invoke the server event. I noticed that there is a preprocessor directive stating that if you’re use FXServer that this hash should not be changed to TRIGGER_SERVER_EVENT_INTERNAL.
So I don’t know if TRIGGER_EVENT_INTERNAL is suppose to invoke on client AND server.
Or it’s not working as it should
Or my code is plain wrong?
TL;DR: How does one invoke a event from client to server?