[Tutorial] How to make a C# Script in SA-MP
#1

Introduction
In this tutorial, I will show you how to create a basic .NET Script on SA-MP.












Getting The Necessary Things

You cannot start right away. You need few things. The things are:
  • Dotnet Script API
  • Visual Studio 2010













Installing Visual Studio 2010
[ame]http://www.youtube.com/watch?v=9TIiIOtle64[/ame]













Implementing Dotnet Script API


It's not really hard to implement it. The steps are listed below:
  1. Downloading: CLICK HERE and download the latest version which is written at the end of the file name and should be the top one.
  2. Copying it on right directory: Extract it and copy all the files from the DotnetClient to your server directory(The place where you will put the .NET Scripts). Do the same for DotnetServer.
  3. Setting up the configuration: Open up the server.cfg from your server directory and add DotnetServer on your plugins section. For e.j.
    Код:
    plugins DotnetServer streamer sscanf
    After you have done that, open DotnetClient.xml using any text editor and change the Address to your IP Address, Port to your server port and AuthKey to anything you like(Note: It should be treated as a password)
    No go to your plugins directory on your server directory and open DotnetServer.ini with any text editor. Now change the AuthKey to the AuthKey you wrote on the DotnetClient.xml and port to the port you wrote on the DotnetClient.xml.













Scripting it on Visual Studio

Now, that we have implement the Dotnet Script API let's begun scripting it. The steps are listed below:
  1. Creating the project: Open up your Visual Studio 2010 and go to File->New and click Project. Expand Visual C# and choose Windows. Choose Class Library, give it a name and click OK.
  2. Setting it up: On the Solution Explorer right click the Class1.cs which has been created by Visual Studio and click Delete and click OK. Right-Click the project and
    Add->New Class. Give it a name and click Add. Right click the project name again on Solution Explorer and click Add References. Go to your server directory and and select DotnetClient.exe and click OK. Select Apply to all items and click Yes.
  3. Creating a simple script: Write
    pawn Код:
    using Samp.API;
    using Samp.Scripts;
    using Samp.Client;
    underneath the
    pawn Код:
    using System.Text;
    It will reference our script to the Dotnet API so that we can use classes and methods of it. Now write ScriptBase after your class name. For e.j.
    pawn Код:
    class test : ScriptBase /* Inheriting the class ScriptBase */
    {
           
    }
    Now put these inside the class test
    pawn Код:
    public override void OnLoad() // When the script loads
            {
                /* Add Event Handlers In Here*/
                Player.OnPlayerCommandText += new EventHandler<Player.OnPlayerCommandTextEventArgs>(Player_OnPlayerCommandText); // Adding the OnPlayerCommandText Event Handler
            }

            public override void OnUnload() // When the script unloads
            {
                /* Deconstruct Event Handlers In Here [Must]*/
                Player.OnPlayerCommandText -= new EventHandler<Player.OnPlayerCommandTextEventArgs>(Player_OnPlayerCommandText); // Removing the OnPlayerCommandText Event Handler
            }

            private void Player_OnPlayerCommandText(object sender, Player.OnPlayerCommandTextEventArgs e) // Declaring the function which will handle OnPlayerCommandText Event
            {
                string[] cmd = e.text.Split(' '); //Declaring the string array which will split the command, the player wrote on console into words
                if (String.Compare(cmd[0], "/test") == 0) // Comparing the if the first word of the command is /test or not
                {
                    int playerid = e.player.ID; // Get the player id who wrote the message
                    //If so then it will trigger everything in here
                    NativeFunctionRequestor.RequestFunction("SendClientMessage", playerid, -1, @"I love {00FF00} C#!"); // Requesting the function which is SendClientMessage with it's specific parameters
                }
            }
    (Note: Everything is explained on the comment)
  4. The finale: The last thing to do, before compiling the Class Library is to right click the projectname and click Properties. Now this is important, the Target Framework should be always 4.0. If not change it now by selecting it. Then right click the projectname again and click Open Folder in Windows Explorer. Open bin->Debug and copy the dll file and paste it on your serverdirectory->Scripts file. Now open your server by clicking samp-server.exe and then when it loaded open the DotnetClient.exe and go in game, test the script by writing /test on chat console.












Note
  • The tutorial is made for Windows so it may/may not work in other Operating System
  • You can also make a .cs file and also use VB.NET
  • You can use any Visual C# editor and even notepad
  • To make it work on Linux, you need to use the source file which ends with .cs.






F.A.Q

Q1. Why should I use C# instead of PAWN?
Ans: Simply because of it's functionality. C# is mainly used in programming whereas PAWN in scripting. There is much difference between programming and scripting. Not only C# has functionality, but you can have external libraries on it as well. You can make any type script with C# and in less time. The downside of it is the speed. But there is not much difference as I have tested many thing's with it.

Q2. Can it be run on Linux?
Ans: Yes it can. But this tutorial is mainly made for Windows PC. But not worry, I will make it as soon as possible.

Q3. Can I use other .NET Language such as Visual Basic?
Ans: Yes you can. I will make a tutorial of it as soon as possible.

Q4. Is there any other downsides?
Ans: Yes, it cannot return values from native functions.














Speed Comparison


C# Speed
Код:
It took 450 ms to execute 1001 ClientMessages on OnPlayerCommandText
Pawn Speed
Код:
It took 300 ms to execute 1001 ClientMessages on OnPlayerCommandText
C# Code
pawn Код:
using System;
using System.Collections.Generic;
using System.Text;
using Samp.API;
using Samp;
using Samp.Scripts;
using System.Diagnostics;
using Samp.Client;

namespace test
{
    public class test : ScriptBase
    {
        public override void OnLoad()
        {
            Player.OnPlayerCommandText += new EventHandler<Player.OnPlayerCommandTextEventArgs>(Player_OnPlayerCommandText);
        }
        public override void OnUnload()
        {
            Player.OnPlayerCommandText -= Player_OnPlayerCommandText;
        }

        private void Player_OnPlayerCommandText(object sender, Player.OnPlayerCommandTextEventArgs e)
        {
            string[] cmd = e.text.Split(' ');
            if (string.Compare(cmd[0], "/test") == 0)
            {
                Stopwatch sw = new Stopwatch();

                sw.Start();
                for (int i = 0; i <= 1000; i++)
                {
                    e.player.ClientMessage(-1, "Hey now brown cow!");
                }
                sw.Stop();
                e.player.ClientMessage(-1, "Time took " + sw.ElapsedMilliseconds + " ms");
            }
        }
    }
}
Pawn Code
pawn Код:
command(test, playerid, params[])
{
    new TickStart;
    new TickEnd;
    TickStart = GetTickCount();
        for (new i = 0; i <= 1000; i++)
    {
        SendClientMessage(playerid, -1, "Hey now brown cow!");
    }
    TickEnd = GetTickCount();
    printf("Time took %i MS", TickEnd - TickStart);
}












Advantages
  • Multi Client Connection - You can have many clients at the same time which means that another person can load a script, and you can load another script. (Useful for people who is on a team)
  • Functionality - SEE IT HERE
  • Usability - It's now widely use for making Windows Application and also Linux Application with mono tech.
  • Simpleness - The language itself is easier than PAWN language(Trust me). It doesn't uses arrays for strings and it has it's own keyword for that.
  • Object-Oriented - Yes, it supports object-oriented programming.
  • Remote connection - You can remote connect to your server. Useful for making a remote administration interface.
  • Cross Capability - It works well in both Linux and Windows.















Disadvantages
  • Speed - The speed is slightly slower than pawn. It would not matter because it's just a split millisecond.
  • Security - When accepting connections from the interwebs there is always a possibility of security breach(Always use strong password).






















Script Link

PasteBin: http://pastebin.com/LWUG9bJ2

















Credits






Change Log
Код:
-> 14/11/2011
-----------------
Added Advantages 
Added Disadvantages

-> 13/11/2011
-----------------
Added Speed Comparison

-> 12/11/2011
-----------------
Added F.A.Q

-> 11/11/2011
-----------------
Created the Thread
Reply
#2

Very nice. Can I make a gamemode with that?
Reply
#3

Good job

Xtra - hahahahahaha you kiddin' me? HAHAHA
Reply
#4

Quote:
Originally Posted by Xtra Softs
Посмотреть сообщение
Very nice. Can I make a gamemode with that?
Ofcourse you can. It has everything to make a huge gamemode and it's execution speed is amazing. I will switch to it because I can use external libraries in my script.
Reply
#5

Quote:
Originally Posted by T0pAz
Посмотреть сообщение
Ofcourse you can. It has everything to make a huge gamemode and it's execution speed is amazing. I will switch to it because I can use external libraries in my script.
ya i will switch and also i am not good in C type of language and i am good on C#
Reply
#6

UPDATED: Added F.A.Q
Reply
#7

Nice, but you should make some speed tests.
Reply
#8

Quote:
Originally Posted by wups
Посмотреть сообщение
Nice, but you should make some speed tests.
thanks and yes I will do it.
Reply
#9

Updated: Added Speed Comparison
Reply
#10

Nice tutorial but i have one question. How come C# is slower than PAWN? If it's slow why we should use C#?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)