C# Mono V2 NUI Callback/TriggerClientEvent

Hi,

I’d like to start by saying I’ve been following mono_v2_get_started and Examples.md and I have searched Google and CFX forums to no avail. So I heard about the new Mono V2 runtime that’s available as a prerelease and wanted to try it out. I am currently in the process of converting my project from Mono V1 to Mono V2.

  1. My first problem is the CallbackDelegate type that was available in V1 isn’t available in V2.

This is what I had used previously:

RegisterNuiCallbackType("Example");
EventHandlers["__cfx_nui:Example"] += new Action<dynamic, CallbackDelegate>(Example);

private void Example(dynamic data, CallbackDelegate callback)
{
    SetNuiFocus(false, false); callback("OK");
}

I believe the EventHandler is still fine but can now also be replaced with:

RegisterNuiCallbackType("Example");

[EventHandlers("__cfx_nui:Example", Binding.Local)]
private void Example(dynamic data, CallbackDelegate callback)
{
    SetNuiFocus(false, false); callback("OK");
}

The exception being CallbackDelegate. When I first switched to C# I used nui-callbacks and I also used CallbackDelegate outside of NUI callbacks as well (wrongfully I suspect). I’ve switch some of my code to using Action instead of CallbackDelegate but what would be the replacement for NUI callbacks?

  1. My next question isn’t really a problem but I’m curious if there’s a similar way to the old of sending an event to every client.

Old:

TriggerClientEvent("Example", data);

New:

PlayerList players = new PlayerList();

foreach (Player player in players)
{
    TriggerClientEvent("Example", player, data);
}

What happens if you use RegisterNuiCallback instead?

The callback delegate type is now just called Callback, you can always check types by using object as the parameter type and then Debug.WriteLine(typeof(callback)), will get you the information quick.

Sending an event to all clients: that’s just TriggerAllClientsEvent, you can also find it at Events.TriggerAllClientsEvent(...)

1 Like