[How-to] Setting up C# and creating a basic resource

Indeed it’s the BasicResourceShared library. I’ve edited the tutorial to clarify this.

This is a good tutorial, thank you. I followed it and still could not get things working. After consulting with @SheppeR, we found the issue. The main issue, I was using the wrong Target Framework and Target Platform.

Set Build -> Target Platforms -> Any CPU, also use .Net4.5.2

May I suggest adding to your tutorial a check for Target Platform and Target Framework. It should be added to the Wiki as well.

Thanks

1 Like

:grinning: This is realy a great tutorial!

Has some one some more code snippets for more events? There is not a lot documentation yet.

I find this list of a few events but I don’t know how to use it :frowning:

I tried “onPlayerDied” on server side with this implementation in the constructor:

EventHandlers.Add("onPlayerDied", new Action<Player, string, Vector3, CallbackDelegate>(OnPlayerDied));

and this method:

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

I’ve done a lot of suicides but nothing happens.

I also tried to implement the playerSpawned event on client side with this line in the constructor:

EventHandlers.Add("playerSpawned", new Action<Player>(OnPlayerSpawned));

and this method:

private void OnPlayerSpawned([FromSource]Player player)
{            
    Debug.WriteLine("OnPlayerSpawned!!! "+player.Name); //this creates an error :(
    Debug.WriteLine("OnPlayerSpawned!!!"); //this works :) you got the line "OnPlayerSpawned!!!" at your client console (F8)
}

So I think there is no parameter of type Player in the event? In the
lua docu I got no more help, I don’t understand nothing of it :wink:

Where can i find out what parameters are used in the events?

Same here , please someone help us .

I got a little advantage and post it here

[    208875] RgscStub::m_13
[    208875] Exception thrown by a task: System.AggregateException: One or more errors occurred. ---> System.MethodAccessException: Error verifying BasicResourceClient.BasicResourceClient/<OnTick>d__1:MoveNext (): Method System.Array:Empty<object> () is not accessible at 0x0014

[    208875]   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at BasicResourceClient.BasicResourceClient.OnTick () [0x00026] in <ecc227357829482ab7dc92b9bb9a5ade>:0 

[    208875]   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at System.Threading.Tasks.Task.Execute () [0x00010] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]    --- End of inner exception stack trace ---

[    208875] ---> (Inner Exception #0) System.MethodAccessException: Error verifying BasicResourceClient.BasicResourceClient/<OnTick>d__1:MoveNext (): Method System.Array:Empty<object> () is not accessible at 0x0014

[    208875]   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at BasicResourceClient.BasicResourceClient.OnTick () [0x00026] in <ecc227357829482ab7dc92b9bb9a5ade>:0 

[    208875]   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at System.Threading.Tasks.Task.Execute () [0x00010] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 <---

[    208875] 
[    208875] Failed to run a tick for BasicResourceClient: System.MethodAccessException: Error verifying BasicResourceClient.BasicResourceClient/<OnTick>d__1:MoveNext (): Method System.Array:Empty<object> () is not accessible at 0x0014

[    208875]   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine] (TStateMachine& stateMachine) [0x0002c] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at BasicResourceClient.BasicResourceClient.OnTick () [0x00026] in <ecc227357829482ab7dc92b9bb9a5ade>:0 

[    208875]   at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

[    208875]   at System.Threading.Tasks.Task.Execute () [0x00010] in <a4e3abdd630b4c98a9d6f31a99197de6>:0 

Client script code:

        public BasicResourceClient()
        {
            //In the constructor we add
            Tick += OnTick;
        }

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

Thank you very much. I needed this Tut

c:

Can somebody Help me out with this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;

namespace LOMCoreServer
{
    public class LOMCoreServer : BaseScript
    {
        public static String MOTD = "Willkommen!";
        public static String prefix = "[LOM-Core // Server]";

        public LOMCoreServer()
        {
            EventHandlers.Add("chatMessage", new Action<int, string, string>(onPlayerText));
            EventHandlers.Add("playerConnecting", new Action<Player, string, CallbackDelegate>(OnPlayerConnecting));
        }

        private void onPlayerText([FromSource] int player, string playerName, string chatMessage) 
        {
            Debug.WriteLine($"{prefix} -CHAT- ({player}): {chatMessage}");
        }

        private void OnPlayerConnecting([FromSource] Player player, string playerName, CallbackDelegate kickReason)
        {
            TriggerClientEvent......
            Debug.WriteLine($"{prefix} {player.Name} versucht zu verbinden");
            
        }
    }
}

I dont know, but if i want to use the ‘TriggerClientEvent’ in the OnPlayerConnectingVoid Method, VS2017 says it is not availble.

I already double-checked my CitizenFX.Core.dll.

1 Like

The “Player” class has a function in it to trigger events. So, in your function I believe you would write player.TriggerEvent.

2 Likes

This doesnt want to work too. :frowning:

Hello All.
I am having the same issue as many others. Server code is fine, Client code is fine but Server to Client communication doesn’t work. I have even copy-pasted the code from this tut.
I have checked this thread and copy-pasted the code: Please post some c# code snippets
Even from there no Server to Client code works and “playerConnecting” seems to be the only Server Event that works.

For what do you need the “Shared” project?

Shared project are all the functions and data that are common between client and server. You can just have a server and client project without using a “shared” project.

2 Likes

Just a heads up, while the guide works for getting started someone actually following the guide will not yield a MOTD.

As mentioned before, player connecting is handled on initial server connection before chat box is available. Any attempts to replicate this code will not work because of this.

That being said, is there a way for the server to call an action on successful spawn? Or do i have to trigger a server event on spawn to get the MoTD, then trigger that message again on the client?

In the latter case, the mini guide should express that the user should request a server action, where the server action requests a user action providing the MoTD details.

For the client :

private bool FirstSpawn = true;

EventHandlers.Add("sendMotd", new Action<string>(ReceivedMotd));
EventHandlers.Add("playerSpawned", new Action<Vector3>(RequestMotd));

 private void ReceivedMotd(string motd)
        {
            TriggerEvent("chatMessage", "SYSTEM", new[] { 255, 0, 0 }, motd);
        }
 private void RequestMotd([FromSource]Vector3 pos)
        {
            Debug.WriteLine("PlayerSpawn");
            if (FirstSpawn)
            {
                FirstSpawn = false;
                TriggerServerEvent("requestMotd");
            }  
        }

Server:

 string motd = "Don't Trust OP, The cake is a truth.";

var requestMotd = new Action<Player>(RequestMotd);
EventHandlers.Add("requestMotd", requestMotd);

 private void RequestMotd([FromSource]Player Player)
        {
            TriggerClientEvent(Player, "sendMotd", motd);
            Debug.WriteLine($"{Player.Name} requested the motd");
        }
2 Likes

I realized that this issue occurs, but havent gotten around on re-writing the tutorial.

Once I have more time, I think I will write a “from-zero-to-hero” guide for C# developement.

This was a quick write up back then and I was also new to all of this (and the FiveM API it self).

2 Likes

No worries, might want to add a “Editors Note” at the top of the post :slight_smile: i just got here from google myself and was confused about what was happening.

I did a quick edit, so the code atleast works :smiley:

1 Like

Does playerSpawn fire from the server? I couldn’t get it to work myself, thus why I had it firing from the client.

The spawnmanager should trigger that event upon respawning.

1 Like