Please post some c# code snippets

Hello,

im done with the setting up tutorial and now I’m really hot to play around with it :wink: But now I do not come forward any more.
If you are using c# too, please be so nice and share some snippets. Especially how to implement the events and some basic stuff like adding weapons, spawn vehicles and so on.
I just need something more stuff to understand how it works.

THANK YOU :slight_smile:

1 Like

Give a weapon on Spawning
Following snippet gives a weapon to the player when he spawns. I think the Event is not setuped correctly with the parameter “pos” because it is always 0,0,0 but there is no error.

Client side

//In the constructor we add
EventHandlers.Add("playerSpawned", new Action<Vector3>(OnPlayerSpawned));
//And the method in the same class
private void OnPlayerSpawned([FromSource]Vector3 pos)
{
     //Press F8 in game to open the console, there will apears any Debug.Write... messages
    Debug.WriteLine("SPAWNED!");             
    Debug.WriteLine(pos.ToString());//the position is always 0,0,0 but no error !?
                        
    //giving a weapon
    LocalPlayer.Character.Weapons.Give(WeaponHash.Grenade, 20, true, true);
    //localplayer is a propertie that always gives a reference to the player
    //the first parameter is easily to fill with the Weaponhash enum (Yeah C# :))
}

List of all Players
This snippet gives you a List of all players, it works on server and client, but the Player class at the server has not the same members than the client Player.

PlayerList allplayers = new PlayerList();
    foreach (Player player in allplayers)
        Debug.WriteLine(player.Name);

Spawning a Vehicle near the player
For spawning a vehicle put in any client side event the following line:

World.CreateVehicle(VehicleHash.Infernus, LocalPlayer.Character.Position + new Vector3(0, 10, 0));

But I don’t know what happens to other player and I can’t test it because I got only one PC. I think this must be done at serverside!?
But the server lib as now Vehicle class. Verry confusing.

@Kerosini

Can you give more examples of EventHandlers from server-side ? https://wiki.fivem.net/wiki/Server_events

I’m trying to get information when the player gets into a vehicle.

EventHandlers.Add("enteredVehicle", new Action<object, int, string, CallbackDelegate>(OnEnteredVehicle));

 private void OnEnteredVehicle(object vehicle, int number, string displayName, CallbackDelegate wtf)
        {
            Debug.WriteLine("This work");
        }

but I did not receive a reply. :frowning:

Sorry, but I’ve the same problem. The only event on the server that works is the “playerConnecting”. Everything else makes no error or effect.

Yeah I got one more :slight_smile:
playerDropped
(Serverside)
when the player left the game

In constructor you have to add:

EventHandlers.Add("playerDropped", new Action<Player, string>(OnPlayerDropped));

And the Method is:

private void OnPlayerDropped([FromSource]Player player, string reason)
{            
    Debug.WriteLine("Player " + player.Name + " Dropped " + reason);
}
public BasicResourceServer()
        {
            EventHandlers.Add("chatMessage", new Action<int, string, string>(OnPlayerText));
            EventHandlers.Add("onResourceStart" , new Action<string>(OnResourceStart));
            EventHandlers.Add("playerConnecting", new Action<Player, string, CallbackDelegate>(OnPlayerConnecting));
            EventHandlers.Add("playerDropped", new Action<Player, string>(OnPlayerDisconnect));
            EventHandlers.Add("baseevents:onPlayerKilled", new Action<Player, int>(OnPlayerKilled));
            EventHandlers.Add("onPlayerDied", new Action<Player, string, Vector3, CallbackDelegate>(OnPlayerDied));
            EventHandlers.Add("baseevents:enteredVehicle", new Action<Player, object, int, string>(OnPlayerEnteredVehicle));
            EventHandlers.Add("baseevents:leftVehicle", new Action<Player, object, int, string>(OnPlayerLeftVehicle));
            EventHandlers.Add("baseevents:enteringVehicle", new Action<Player, int, int, string>(OnPlayerEnteringVehicle));
        }

        private void OnPlayerText([FromSource] int player, string playerName, string chatMessage)
        {
            //TriggerEvent("chatMessage", playerName + ":" + chatMessage);
            Debug.WriteLine($"{playerName} ({player}): {chatMessage}");
        }

        private void OnResourceStart(string resourceName)
        {
            Debug.WriteLine($"Resource was succesfully started {resourceName}");
        }

        private void OnPlayerConnecting([FromSource] Player player, string playerName, CallbackDelegate kickCallback)
        {
            Debug.WriteLine($"{player.Name} trying to connect with IP of = {player.EndPoint.ToString()}");
        }

        public void OnPlayerDisconnect([FromSource] Player player, string reason)
        {
            Debug.WriteLine($"{player.Name} disconnected (reason: {reason})");
        }

        public void OnPlayerKilled([FromSource] Player victim, int killerID)
        {
            Player killer = Players[killerID];
            Debug.WriteLine($"{victim.Name} was killed by {killer.Name}");
        }

        private void OnPlayerDied([FromSource]Player player, string deathReason, Vector3 position, CallbackDelegate wtf)
        {
            Debug.WriteLine("OnPlayerDied! " + player.Name + " reason = " + deathReason + " at position: " + position.ToString() + " wtf is = " + wtf);
        }

        private void OnPlayerEnteredVehicle([FromSource]Player player, object vehicle, int seat, string displayName)
        {
            Debug.WriteLine($"Player {player.Name} entered {displayName} {vehicle} to seat {seat}");
        }

        private void OnPlayerLeftVehicle([FromSource]Player player, object vehicle, int seat, string displayName)
        {
            Debug.WriteLine($"Player {player.Name} left {displayName} {vehicle} from seat {seat}");
        }

        private void OnPlayerEnteringVehicle([FromSource]Player player, int vehicleHash, int seat, string displayName)
        {
            Debug.WriteLine($"Player {player.Name} left {displayName} {vehicleHash} from seat {seat}");
        }

All Events are working except OnPlayerDied

And i get an exception in client console with this client script:

 public BasicResourceClient()
        {
            //In the constructor we add
            EventHandlers.Add("playerSpawned", new Action<Vector3>(OnPlayerSpawned));
        }

        private async Task OnTick()
        {
            Debug.WriteLine("One second passed!");
            await Delay(1000);
        }

        private void OnPlayerSpawned([FromSource] Vector3 pos)
        {
            //Press F8 in game to open the console, there will apears any Debug.Write... messages
            Debug.WriteLine("SPAWNED!");
            Debug.WriteLine(pos.ToString());//the position is always 0,0,0 but no error !?

            //giving a weapon
            LocalPlayer.Character.Weapons.Give(WeaponHash.Grenade, 20, true, true);
            //localplayer is a propertie that always gives a reference to the player
            //the first parameter is easily to fill with the Weaponhash enum (Yeah C# :))
        }
    }

Guys , anyone that tries to script with c# please join me on discord https://discord.gg/Jzw7GU5 i will help with whatever i’ll find and i will expect same from you.Please join , this will be faster.Because everyone is coding in Lua so it’s hard to find c# scripters , please join.

Oh nice, (nearly) the last missing Server events. I will check this out now. Thank you so much.

For your crashing client script you can use:

private void OnPlayerSpawned([FromSource]Vector3 pos)
{
    Vector3 whateveryouneeditfor = LocalPlayer.Character.Position;

There comes no position with this event, so “pos” is null and thats the cause of the crash.

Mrm :face_with_monocle: I tested your code, but all these baseevents do not response.
I do not understand what is wrong.

Add baseevents rescource in serve.cfg

You need to start the “baseevents” resource :wink:

After some testing i found that Vehicle events doesn’t return seat properly.
Kerosini provided client script still crashes.
OnPlayerDied doesn’t respond nor gives any error.

What I told is wrong… the pos parameter is not null, but its not filled. But a crash is not happend to me.
this code

private void OnPlayerSpawned([FromSource]Vector3 pos)
{
    if (pos == null)
        Debug.WriteLine("Yes it is NULL");
    else
    {
        Debug.WriteLine("Oh it works, players pos is: " + pos.ToString());   //<---             
    }

    Debug.WriteLine("try to acces localplayers pos...");
    try
    {                
        Debug.WriteLine("it is:  " + LocalPlayer.Character.Position.ToString()); //<---
    }
    catch
    {
        Debug.WriteLine("Oh it crashed :(");
    }

creates this output:

Maybe it causes a crash after I add the baseevents to my cfg :slight_smile: thanks for that hint I will try it now.

YEAH GOOD NEWS
After adding to start these baseevents into the server config I got all events :slight_smile:

And I got onPlayerDied two :slight_smile: :slight_smile: :slight_smile:

It is also a baseevent aand it comes with the following parameters

//in constructor
EventHandlers.Add("baseevents:onPlayerDied", new Action<Player, string>(OnPlayerDied));
private void OnPlayerDied([FromSource]Player player, string deathReason)
{
    Debug.WriteLine("OnPlayerDied! " + player.Name + " reason = " + deathReason );
}

Now everthing is possible :slight_smile:

1 Like

Ohhhhhhhhhhh ! Very nice bro ! Ty !

Can someone explain how NativeUI or NUI with C# works?

Thanks! c:

If you look at my code in this thread you can see just how it works from the NUI folder :smiley:

ELS+ | fic

Thank you… c:

#morethan20characters