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

Thx very much for your fast answer, I tried that now:

SERVER SIDE

 private void OnPlayerConnecting(string playerName, CallbackDelegate kickReason)
      {
          TriggerClientEvent("sendMotd", 1, Motd);
          
           Debug.WriteLine($"{playerName} sending message to the client ");
        }

On the console of the server I see the message “sending message to the client”.

CLIENT SIDE

       public BasicResourceClient()
        {
            Tick += OnTick;
            EventHandlers["sendMotd"] += new Action<string>(ReceivedMotd);

        }

        private void ReceivedMotd(string motd)
        {
            Debug.WriteLine("Client received the message");   
        }

But in this case I can’t see inside the game console the message “Client received the message” … :confused:

I tried with and whidout the 1.

Hmm try using ID -1, This will broadcast it to all the clients.


nothing using -1 …


I tested also to see if the client is working and if I put that code:
public BasicResourceClient()
{
Debug.WriteLine(“Client received the message”);
}

The message appear in the console (work, soo the client is running).

C# is different with the triggerclientevents.

If follows these paremeters TriggerClientEvent(Player player, String eventName, Args...)

For it to trigger on all clients, just don’t fill in the Player, and do TriggerClientEvent(String eventName, Args...)

TriggerClientEvent("sendMotd", Motd);

The example I did is without the player and doesn’t work (using just String eventName, Args) … I don’t know why :/…
Tried also with the player… and doesn’t work also in this case…

Have they fully implemented this in C#? Or I’m doing something wrong?


I tried also to use something like that just to test :

      public BasicResourceClient()
        {

            Tick += OnTick;
            EventHandlers["sendMotd"] += new Action<string>(ReceivedMotd);
            TriggerEvent("sendMotd", "test");
        }

This triggerevent should call the EventHandlers itself right? But also this doesn’t work…
Now could be the EventHandlers in the client side the real problem?

Triggering events from the constructor won’t work.

Ok, but the problem remain why also that dosn’t work… :confused:

 private void OnPlayerConnecting(string playerName, CallbackDelegate kickReason)
      {
          TriggerClientEvent("sendMotd", Motd);
          
           Debug.WriteLine($"{playerName} sending message to the client ");
        }

P.S. I tested the script you posted and have the same problem…just for the server-client call.

On playerConnecting, the player won’t exist yet, so if you want to send an event to that specific player, you’ll need an event that executes a bit later.

Thx titanium, it’s working , tested with EventHandlers[“chatMessage”] += new Action<int, string, string>(OnPlayerText); .
Thx also to everyone that answered and Syntasu for the tutorial without I could never start scripting :)!

Now I will try to port my single player zombie mod, thx very much again guys.

Exactly the kind of guide I was waiting for! Thank you! I am used to C-Style languages like C++ and C# and really don’t like LUA scripting styles (To me it just looks like spaghetti).

Again, awesome guide. Thank you so much!

4 Likes

No kidding. I write C# code for a living, so it’s just what I’m used to.

There’s no documentation on the C# side of things, so threads like this are a huge help.

I agree with the above poster, thanks for the thread. Very helpful.

Where is the shared library? Is this “BasicResourceShared”? How do I add it?

1 Like

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);
        }