[C#] Question regarding resource usage

I’m trying to learn C# (completely new to it), but how come resource usage is this high even when i have no loop or anything running that should cause this from what i understand, either way i’m really new, and i want to know the benefits of switching from LUA to C#, it seems to be a better developer experience but does it take more resource usage or is there something i’m missing?

Client Side:

using CitizenFX.Core.UI;
using System.Threading.Tasks;
using static CitizenFX.Core.Native.API;


namespace v_learning.Client
{
    public class Client_Coroner

    {
        private static Vehicle cVanEntity;

        private static int cVanBlip;

        private static Ped cVanPed1;

        private static Ped cVanPed2;

        private static bool eventSpawned;

        public async static void Summon()
        {
            Ped player_char = Game.Player.Character;
            Screen.ShowNotification("Coroner is on it's way!", true);


            Vector3 spawnLocation = new Vector3();
            float spawnHeading = 0F;
            int unusedVar = 0;
            GetNthClosestVehicleNodeWithHeading(player_char.Position.X, player_char.Position.Y, player_char.Position.Z, 77, ref spawnLocation, ref spawnHeading, ref unusedVar, 9, 3.0F, 2.5F);
            await LoadModel((uint)VehicleHash.Adder);
            cVanEntity = await World.CreateVehicle(VehicleHash.Adder, spawnLocation, spawnHeading);

            cVanEntity.Mods.PrimaryColor = VehicleColor.MetallicBlack;
            cVanEntity.Mods.LicensePlate = "V";
            cVanEntity.Mods.LicensePlateStyle = LicensePlateStyle.YellowOnBlack;

            cVanBlip = AddBlipForEntity(cVanEntity.Handle);

            SetBlipColour(cVanBlip, 40);
            BeginTextCommandSetBlipName("STRING");
            AddTextComponentString("Coroner");
            EndTextCommandSetBlipName(cVanBlip);

            await LoadModel((uint) PedHash.Doctor01SMM);

            cVanPed1 = await World.CreatePed(PedHash.Doctor01SMM, spawnLocation, spawnHeading);
            cVanPed1.SetIntoVehicle(cVanEntity, VehicleSeat.Driver);

            cVanPed1.CanBeTargetted = false;

            await LoadModel((uint)PedHash.Scientist01SMM);

            cVanPed2 = await World.CreatePed(PedHash.Scientist01SMM, spawnLocation, spawnHeading);

            cVanPed2.CanBeTargetted = false;

            Vector3 targetLocation = new Vector3();
            float targetHeading = 0F;
            GetClosestVehicleNodeWithHeading(player_char.Position.X, player_char.Position.Y, player_char.Position.Z, ref targetLocation, ref targetHeading, 1, 3.0F, 0);


            cVanPed1.Task.DriveTo(cVanEntity, targetLocation, 10F, 20F, 262972);

            eventSpawned = true;

        }

        public static async Task<bool> LoadModel(uint model)
        {
            if (model == 0 || !IsModelInCdimage(model))
            {
                Debug.WriteLine("Invalid Model!");
                return false;
            }

            RequestModel(model);
            while (!HasModelLoaded(model))
            {

                Debug.WriteLine($"Loading model {model}");
                await BaseScript.Delay(100);
            }
            return true;
        }

    }
}

Server Side:

using System.Threading.Tasks;
using CitizenFX.Core;

namespace v_learning.Server
{
    public class ServerMain : BaseScript
    {
        public ServerMain()
        {
            Debug.WriteLine("Hi from v_learning.Server!");
        }

        [Command("hello_server")]
        public void HelloServer()
        {
            Debug.WriteLine("Sure, hello.");
        }
    }
}

High resource usage is caused by the current C# runtime being used. Even empty scripts run at 0.10ms on client side and like 1.x ms on server side.

However C# scales much much better than lua in terms of performance. So while an empty resource might look like it has a lot, consider a giant lua framework vs c#. C# will outperform it by a mile.

Also thorium is working on a new runtime GitHub - thorium-cfx/mono_v2_get_started: Mono v2 runtime for FiveM/RedM that is still in beta though.
It basically doesn’t have any overhead.

1 Like

Thank you, i ended up having to rollback on my PC due to a driver issue which lead to me reinstalling VS, but now when i attempt to create a new cfx-resource it’s not loading it due to this error.

“Severity Code Description Project File Line Suppression State Error NU1202 Package CitizenFX.Core.Server 1.0.6335 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package CitizenFX.Core.Server 1.0.6335 does not support any target frameworks. learning_cs.Server C:\Users\V\Documents\learning_cs\Server\learning_cs.Server.csproj 1”

You need to target .net framework 4.5.2 as far as I remember.

1 Like

Gotcha, thank you! sorry to bother but any other tips for C# that i should know, still confused on allot of parts but learning.

Try using the wrapper classes where ever you can.

And see it all the way through :stuck_out_tongue:

1 Like