[C#] TriggerEvent with callback

Hello,
I’m trying to trigger a server event with a callback but I just don’t know how to do.
During my research, I’ve found this (lua):
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
This is what I want to achieve in C#, tell me if I’m wrong but in this example, we trigger an event “getsomething” with a function where obj is returned by the event and then assigned to the ESX local.

This is what I’ve tried:

// Server:
 public MyServerConstructor()
        {
             EventHandlers.Add("GetMyString", new Action<Player, NetworkCallbackDelegate>(TestEventCB));
        }
public void GetMyString([FromSource] Player player, NetworkCallbackDelegate cb)
        {
            string val = "blablabla";
            //cb.Invoke(val);
              cb(val);
            Debug.WriteLine("CALLED");
        }
// Client:
TriggerServerEvent("GetMyString", new Action<dynamic>(s =>
                {
                    Debug.WriteLine($"result : {s}");
                }));

(this is just a simple example)

It just print server side (“CALLED”) but nothing client side. I know I can just trigger an other client event from my Server event GetMyString, but I think the “callback” approach is more efficient

This post provides an example on how to use the NetworkCallbackDelegate.

You can’t transfer a callback in C#(in lua you have some ways to it) from server to client or backwards. You can transfer callback from client to client and from server to server.

1 Like