10.12.2011, 18:34
(
Последний раз редактировалось T0pAz; 06.02.2013 в 15:31.
)
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:
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:
Scripting it on Visual Studio
Now, that we have implement the Dotnet Script API let's begun scripting it. The steps are listed below:
Note
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
Pawn Speed
C# Code
Pawn Code
Advantages
Disadvantages
Script Link
PasteBin: http://pastebin.com/LWUG9bJ2
Credits
Change Log
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:
- Downloading: CLICK HERE and download the latest version which is written at the end of the file name and should be the top one.
- 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.
- 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
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:
- 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.
- 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. - Creating a simple script: Write
pawn Код:using Samp.API;
using Samp.Scripts;
using Samp.Client;
pawn Код:using System.Text;
pawn Код:class test : ScriptBase /* Inheriting the class ScriptBase */
{
}
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
}
} - 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
Код:
It took 300 ms to execute 1001 ClientMessages on OnPlayerCommandText
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 Код:
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