Looking for Help Setting Up a C# RedM RP Server (Client & Server)

Server Artifact Version: latest 13458
Operating System (Windows/Linux): Windows
Language : C# (CSharp)
Lib: try to import with Nuget or just with (dotnet new cfx-resource)

Hi everyone!

I’m currently working on creating a RedM RP server and I want to build it entirely in C#, both on the client and server side.

I’ve already set up a basic vanilla RedM server to test things out. The setup goes well, I install the resource with:

dotnet new cfx-resource

The server launches fine, and the client spawns correctly — so far, so good!

But then, on the client side, I run into errors during the ticks. I get this message in the console:

MainThrd/ Failed to run a tick for ClientMain: System.BadImageFormatException: Error verifying dev.Client.ClientMain/<WorldControlLoop>d__5:MoveNext (): Cannot load method from token 0x0a00003a for call at 0x0075

For example, I have this simple code running after spawn:

// when i spawn -> i run this 
Tick += WorldControlLoop;

private async Task WorldControlLoop()
{
    try
    {
        Debug.WriteLine("[DEBUG] WorldControlLoop ok !");

        SetPedDensityMultiplierThisFrame(0.0f);
        SetScenarioPedDensityMultiplierThisFrame(0.0f, 0.0f);

        // Clearing the area around the player (commented out for now)
        // var ped = PlayerPedId();
        // var coords = GetEntityCoords(ped, true);
        // ClearAreaOfPeds(coords.X, coords.Y, coords.Z, 200.0f, 0);
    }
    catch (Exception e)
    {
        Debug.WriteLine($"[WorldControlLoop] Exception : {e.Message}");
    }

    await Delay(100);

}

I’m thinking it might be an issue with outdated libraries or some mismatch, so I tried using another installation method.

I found here:
:point_right: mono_v2_get_started/Setup.md at main · thorium-cfx/mono_v2_get_started · GitHub
or i try to follow this with mono
:point_right: C# mono v2

But, unfortunately, I couldn’t get it to work either.

I’m pretty stuck right now and going in circles. Does anyone have a working base setup or any advice on how to properly set up a **C# RedM server **?

Any help would be greatly appreciated! :pray:

Thanks in advance!

1 Like

I’m taking a guess that’s it’s more than likely the same issue mentioned here: C# Client side issues issue was solved check the solution hopefully this helps.

Thank you so much for your help Kronus!

I’ve decided to abandon the idea for now this environment is still in beta, and it’s just not stable enough yet.

I don’t feel like constantly struggling with the documentation or the instability.

It’s sad to see the C# version isn’t more mature and that the community isn’t as big and active as it is with Lua.

I ended up doing it this way.

dotnet new cfx-resource

It works at least and i wait mono v2 stable …
What do you think about it?

private async Task CleanerPnj()
{
    // Disable Dispatch services (still on the client side)
    for (int i = 1; i <= 15; i++)
    {
        API.EnableDispatchService(i, false);
    }

    try
    {
        // Clean up NPCs
        var peds = GetGamePool("CPed");

        foreach (var pedHandle in peds)
        {
            int ped = Convert.ToInt32(pedHandle);

            // If it's a player, skip
            if (IsPedAPlayer(ped))
                continue;

            // If it's a protected NPC, skip
            if (DecorExistOn(ped, "DontDelete") && DecorGetInt(ped, "DontDelete") == 1)
                continue;

            // Otherwise, delete it
            DeleteEntity(ref ped);
        }

        // Clean up vehicles (including wagons)
        var vehicles = GetGamePool("CVehicle");

        foreach (var vehHandle in vehicles)
        {
            int veh = Convert.ToInt32(vehHandle);

            // Exclude if vehicle is protected
            if (DecorExistOn(veh, "DontDelete") && DecorGetInt(veh, "DontDelete") == 1)
                continue;

            // Delete the wagon
            DeleteEntity(ref veh);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine($"[C# ERROR] Error in CleanWorld loop: {ex.Message}");
    }

    await Delay(500); 
}

I agree it is sad, I’ve always enjoyed working with C# in general, the one thing I will say is that from what I can recall is a lot of the natives for RedM are not yet implemented so a lot of the time you will have to call them via their native hash however I just ended up making an external library that served as wrapper functions for that case.

It looks fine there’s some minor stuff you could do syntax wise to make it a little more readable however if it functions it functions. Are you running this on a constant tick within the client? This could get expensive over time.

Finnaly, I ended up starting a project using Mono v2 recently, and I’ve been having a lot of fun with it so far.

That said, I’d be super interested in checking out your external library with all the native hashes wrapped—sounds like it would save a ton of time! Would you be open to sharing it? :slight_smile:

I haven’t found how to disable the automatic generation of npc by the client. But I’m looking into how else to do it!

If you want to do it client side there is various natives to do so they need to be ran on a tick however. If you check the native database its a long the lines of SetPedDensity there’s various ones

Sure send me a message I’ll share with you what I still have

Hello, I have a new issue: sometimes when I try to catch a key press, the frame doesn’t register it.

I also tried using RegisterKeyMapping (0xD7664FD1), but it doesn’t exist with Mono v2.
I also tried calling it by address, but it’s impossible!

private async Coroutine scanKeyboard()
{
    if (ControlManager.isJustPressed((uint)Controls.PageUp))
    {
        Debug.WriteLine("Press");
        ToggleMenu();
        await Wait(250);
    }

    await WaitUntilNextFrame();
}

Plus, other problem if i use SetNuiFocus(true, true) it’s impossible to close menu with no keybinding …

For information Mono V2 work well with 4.8.1 Net Framewok !