Nui wrapper for C#

I don’t know if this belongs to Discussion or Releases sections… but many people asked me how do i handle Nui in c# and how to make it object-oriented friendly.
Here is a simple wrapper i made to work with Nui

The gist has 2 classes in it:

  • TypeCache (thanks to ApocDev and his NetworkMethod):
    is used to check the data and filter simple types and complex types.
  • NuiManager is the class used to handle Nui. Its usage is pretty simple:

This is a simple example with a callback that doesn’t return anything to Nui.

public NuiManager NuiHandler = new();

NuiHandler.RegisterCallback("chars:new", new Action<NewChar>(AddNewChar));

private static async void AddNewChar(NewChar data)
{
	Screen.Fading.FadeOut(800);
	While(Screen.Fading.IsFadingOut) await BaseScript.Delay(1000);
	GuiEnabled = false;
	ToggleMenu(false);
	await Creator.CharCreationMenu(data);
}

In this example instead, we can handle data received from Nui and give back something else in a synchronous method:

NuiHandler.RegisterCallback("chars:getCharById", new Func<int, Char>(GetCharData));

private Char GetCharData(int id)
{
	return MyPlayer.Chars[id];
}

it can be awaitable too:

NuiHandler.RegisterCallback("chars:getCharById", new Func<int, Task<Char>>(GetCharData));

private async Task<Char> GetCharData(int id)
{
    Char char = await Client.Events.Callback("GetCharById", id);
    return char;
}

The wrapper is taken from my own gamemode so a couple of simple editing (for Json conversion and debug printing) might be necessary for it to work.

Feel free to use it and edit it for your projects, i only ask to be credited if you use it for a public project :slight_smile:

2 Likes