C# - Invoking server event from client?

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?

TriggerServerEvent() not TriggerEvent. TriggerEvent will trigger another event on the client if you call it from the client.

1 Like

It’s very strange how triggering server events from the client works. For whatever reason, for me at least, it doesn’t like to work without any parameters, but it doesn’t like to work with them either. The only way I’ve ever gotten my builds to trigger server events from my client has been to add a single empty string parameter to my TriggerServerEvent. I’ve yet to figure out the cause of this though.

Thats the point, TriggerServerEvent is removed as of the deprecation of CFX :confused:

The cause is that the they removed the TRIGGER_SERVER_EVENT_INTERNAL native call from TriggerEvent function if you are running a FXServer.

No… its still there. You misunderstand that code there. Im assuming the client and server run that same script.

What #if !IS_FXSERVER is saying, “If this script is not running on FXserver, include this”. Its running on the client, not the server, so the function is included.

1 Like

And this depends on us using the correct dll for the client right?

You should always use the proper .dll to not screw things up. I don’t know if its officially documented anywhere, but the server and client .dlls you need were posted here by @zombieguy

But the that event is not included?

#if !IS_FXSERVER states

IS_FXSERVER = true
Negate that and you get false.

If preprocessor directive is false that it won’t get included. And that seems to apply to my case:
http://puu.sh/xk4Ad/0d11b36639.png
http://puu.sh/xk4AZ/e11c66b9c4.png

No TriggerServerEvent in basescript not auto-complete?

@Syntasu You are using the wrong .dll then for your client script. Look at my post I just made up above for the proper .dlls

I assure you… its there :wink:

1 Like

Aaaaaah it are different DLLs!

Gotcha now, too bad that the documentation on C# is nearly non-existant

Is there any possibility you know why TriggerServerEvent might be causing any issues? For me, at least, it tends to throw some errors when I use multiple parameters/no parameters and says something along the lines of not being able to register a tick or something.

@Khovanet I assume this is your issue? Or something else? [C#] TriggerServerEvent

@Briglair That would be the topic!