Please post some c# code snippets

Hi fellow C# developpers !

Anyone has a piece of idea about my problem posted here : [C#] TriggerClientEvent

Thanks in advance !

Hello Guys,

i’am trying to iterate through an object in c# which i got from an Event in LUA.
some code:
LUA Code

RegisterNetEvent('esx_dmvschool:loadLicenses')
AddEventHandler('esx_dmvschool:loadLicenses', function(licenses)
  Licenses = licenses
end)

Client Side C#

public NoLicenseDrivingClient()
        {
            EventHandlers.Add("esx_dmvschool:loadLicenses", new Action<dynamic>(LoadLicenses));
            Tick += OnTick;
        }

private void LoadLicenses(dynamic obj)
        {
            Licenses = obj;
            if(Licenses == null)
            {
                Debug.WriteLine("Licenses == null");
            }
            else
            {
                Debug.WriteLine("Not Null");
                Debug.Write("Size : " + Licenses.Count);
                foreach(Object o in Licenses)
                {
                    Debug.WriteLine(o);
                }

            }
        }

Maybe someone can help me.

A couple of commands

// Give player a pistol
RegisterCommand("pistol", new Action<int, List<object>, string>((source, args, raw) =>
{
    LocalPlayer.Character.Weapons.Give(WeaponHash.Pistol, 0, false, false);
}), false);

// Give pistol ammo only if player has a pistol
RegisterCommand("ammo", new Action<int, List<object>, string>((source, args, raw) =>
{
    if (LocalPlayer.Character.Weapons.HasWeapon(WeaponHash.Pistol))
    {
        GiveWeaponToPed(GetPlayerPed(-1), (uint)WeaponHash.Pistol, 12, false, false);
    }
}), false);

This class will sync game time with the system clock on the client side.

    public class SyncTime : BaseScript
    {
        private byte hour;
        private byte minute;
        private Thread clock;

        public SyncTime()
        {
            this.hour = 0;
            this.minute = 0;
            clock = new Thread(new ThreadStart(ClockTick));
            clock.Start();
            Tick += OnSlowTick;
        }

        private async Task OnSlowTick()
        {
            await Delay(0);
            NetworkOverrideClockTime(this.hour, this.minute, 0);
        }

        private void ClockTick()
        {
            while (true)
            {
                Thread.Sleep(1000);
                this.hour = (byte)System.DateTime.Now.Hour;
                this.minute = (byte)System.DateTime.Now.Minute;
            }
        }
    }
1 Like

Forget about that crap, I just learned how to deal with Tasks, this should do the trick way better:

public class Realtime : BaseScript
    {
        private const int MILISECONDS_FOR_GAME_MINUTE = 60000;
        private const int GTAV_CLOCK_TICK = 2000;

        private bool serverSync;
        private byte hour;
        private byte minute;
        private int mark;

        public Realtime()
        {
            SetServerSync(false); // TODO: Get from config
            Sync();
            mark = GetGameTimer();
            Tick += Update;
        }

        private async Task Update()
        {
            await Delay(GTAV_CLOCK_TICK);
            if (GetGameTimer() - mark >= MILISECONDS_FOR_GAME_MINUTE)
            {
                minute++;
                mark = GetGameTimer();
                if (minute >= 60)
                {
                    minute = 0;
                    hour++;
                    if (hour >= 24)
                    {
                        hour = 0;
                    }
                }
            }
            NetworkOverrideClockTime(hour, minute, 0);
        }

        private void Sync()
        {
            if (serverSync)
            {
                // TODO: Sync with server
            }
            else
            {
                hour = (byte)System.DateTime.Now.Hour;
                minute = (byte)System.DateTime.Now.Minute;
            }
        }

        public void SetServerSync(bool set)
        {
            serverSync = set;
            Sync();
        }
    }

Some constant to have in hand while managing weather

        public const string CLEAR = "CLEAR";
        public const string EXTRASUNNY = "EXTRASUNNY";
        public const string CLOUDS = "CLOUDS";
        public const string OVERCAST = "OVERCAST";
        public const string RAIN = "RAIN";
        public const string CLEARING = "CLEARING";
        public const string THUNDER = "THUNDER";
        public const string SMOG = "SMOG";
        public const string FOGGY = "FOGGY";
        public const string XMAS = "XMAS";
        public const string SNOWLIGHT = "SNOWLIGHT";
        public const string BLIZZARD = "BLIZZARD";

Does anyone have a code snippet about drawing text? Can’t seem to figure it out.

Not sure if you mean simple Text like that

Text text = new Text(“Text you want to display.”, new PointF(posx , posY) , 0.3F);
text.Draw();

Simple text. I can’t seem to figure it out