[Tutorial] Write Gamemodes in .NET [SampSharp (S#)]
#1

Beta Tester Jay_ suggested I post this here, so I am ^.^

Updated for SA-MP 0.3.7-R2.
For this to work we will use the
SampSharp - Write gamemodes in .NET plugin created by ikkentim.

For questions:


Please tell me how you think of these instructions by rating the thread and commenting, negative and positive feedback accepted!

What you can possibly accomplish using S#!


Detailed Step-By-Step Instructions (with Screenshots):
Ubuntu users! Scroll down to see the Ubuntu instructions.
I am on Windows 10 64-bit, if you are on a different version of Windows some things may be different.
http://image.prntscr.com/image/8204b...fc01b6e60d.png

1. Make sure your Windows updates are up-to-date. (Control Panel > System & Security > Updates)
http://image.prntscr.com/image/23acb...dd3fd4920d.png

2. Make sure .NET Framework 3.5 and 4.6 are activated from the "Turn Windows features on or off" menu. (Programs and Features > Turn Windows features on or off)
http://image.prntscr.com/image/13826...1c7ae814fe.png

3. Make sure to have the latest Visual C++ Redistributable installed.
< No Screenshot because of update. >

4. Download and install Visual Studio 2017 RC. (You can use any .NET Framework IDE you want, suggested VS 2010+).
http://image.prntscr.com/image/75316...af9920ec97.png

5. Download the SA-MP 0.3.7-R2 Windows Server files. (Source: www.sa-mp.com/download.php)
http://image.prntscr.com/image/1d61e...cbe5a474eb.png

6. Download the SampSharp-0.7.6221.37952.zip files. (Source: https://github.com/ikkentim/SampSharp/releases/)
http://image.prntscr.com/image/ce063...e9acabc795.png

7. Download the mono-4.6 files. (Source: http://sampsharp.timpotze.nl/installation)
http://image.prntscr.com/image/b961b...5b87810e39.png

8. Make 'C:\MyGamemode', 'C:\MyGamemode\env', and 'C:\MyGamemode\src' folders.
http://image.prntscr.com/image/607c2...d3cce57a9f.png

9. Extract SA-MP files into the env folder so that the SA-MP executable is located 'C:\MyGamemode\env\samp-server.exe'.
http://image.prntscr.com/image/d1ee3...57a0837a62.png

10. Extract Mono files into the same env folder so that the mono-2.0.dll is located 'C:\MyGamemode\env\mono-2.0.dll'
http://image.prntscr.com/image/3120b...e7ae0ddb0e.png

11. Extract SampSharp files into the same env folder so that SampSharp.dll is located 'C:\MyGamemode\env\plugins\SampSharp.dll'
http://image.prntscr.com/image/d07b0...7b7eacaeab.png

12. Your env folder should look like the below screenshot.
http://image.prntscr.com/image/d07b0...7b7eacaeab.png

13. Now I would open 'C:\MyGamemode\env\server.cfg' and edit it to look similar to the below quote:
http://image.prntscr.com/image/58a5b...547645d5ec.png
Quote:

port 7777
hostname SA-MP 0.3 Server
weburl www.sa-mp.com
rcon_password changed

maxplayers 1000
maxnpc 1000

lanmode 1
announce 1
query 1

onfoot_rate 40
incar_rate 40
weapon_rate 40
stream_distance 300.0
stream_rate 1000

chatlogging 0
logtimeformat [%H:%M:%S]

#
# SA-MP#
#
gamemode0 empty 1
gamemode MyGamemode:GameMode

plugins SampSharp.dll
# plugins libSampSharp.so

14. Now open your IDE, I used Visual Studio 2017 RC, which is similar to Visual Studio 2010+.
http://image.prntscr.com/image/562c3...3e53cab7c8.png

15. File > New > Project
http://image.prntscr.com/image/cbac3...316657f6da.png

16. Visual C# > Class Library (.NET Framework), name it 'MyGamemode', set directory towards 'C:\MyGamemode\src', untick 'Create directory for solution', click on 'Ok'.
http://image.prntscr.com/image/ee959...f7a0e93e54.png

17. Right-click on Properties from the Solution Explorer, click on Open.
http://image.prntscr.com/image/3351b...c7c66aab33.png

18. Click on the Build tab from Properties, drop-down Configuration and select 'All Configurations'.
http://image.prntscr.com/image/6a5d6...c41b223431.png

19. Still in the Build tab, change Output Path to 'C:\MyGamemode\env\gamemode\'.
http://image.prntscr.com/image/f4c90...56d2fa8f76.png

20. Switch to the Debug tab, tick Start External Program and select 'C:\MyGamemode\env\samp-server.exe'.
http://image.prntscr.com/image/062f1...40705dfc9e.png

21. Still on the Debug tab, set the Working Directory to 'C:\MyGamemode\env\', press CTRL+S to save the Properties.
http://image.prntscr.com/image/ceb18...7e1f1fe1a9.png

22. Right-click References from the Solution Explorer, and select Manage NuGet Packages...
http://image.prntscr.com/image/f72c4...2e6da7da88.png

23. Tick 'Include pre-release' from the NuGet menu.
http://image.prntscr.com/image/f1243...0c2f1a7443.png

24. There's a hard to see cogwheel icon for the settings next to Package source: nuget.org, click that.
http://image.prntscr.com/image/08d6a...63992ccb71.png (Picture of red arrow pointing towards cogwheel.)
http://image.prntscr.com/image/83a5b...9ead143101.png

25. Click on the green + and add name 'SampSharp' and source 'http://nuget.timpotze.nl/api/v2/', click Ok.
http://image.prntscr.com/image/0598c...2a05adc28f.png

26. Drop-down Package source from the NuGet menu and select 'SampSharp'.
http://image.prntscr.com/image/2cce8...91ceb1ac27.png

27. Click on Browse from the NuGet menu and type SampSharp into the search.
http://image.prntscr.com/image/c49ae...733064bdee.png

28. Click install for SampSharp.GameMode
(Optional: SampSharp.Streamer if you have the Streamer Plugin by Incognito.)
http://image.prntscr.com/image/6aeb8...a069667530.png

29. Rename Class1.cs to GameMode.cs in the Solution Explorer.
http://image.prntscr.com/image/18809...7f5e2f8476.png

30. Replace the contents of GameMode.cs to:
http://image.prntscr.com/image/edb27...8639a6b30e.png
Код:
using System;
using System.Collections.Generic;
using System.Linq;

using SampSharp.GameMode; // Contains BaseMode class
using SampSharp.GameMode.Controllers; // Contains ControllerCollection class

namespace MyGamemode 
{
    public class GameMode : BaseMode
    {
        #region Overrides of BaseMode

        protected override void OnInitialized(EventArgs e)
        {
            Console.WriteLine("\n----------------------------------");
            Console.WriteLine(" Blank Gamemode by your name here");
            Console.WriteLine("----------------------------------\n");

            /*
             * TODO: Do your initialisation and loading of data here.
             */
            base.OnInitialized(e);
        }

        protected override void LoadControllers(ControllerCollection controllers)
        {
            base.LoadControllers(controllers);

            /*
             * TODO: Load or unload controllers here.
             */
        }

        #endregion
    }
}
31. Click on the green arrow that says Start at the top.
http://image.prntscr.com/image/0af05...c1253d1dc0.png

32. CONGRATULATIONS, it should have worked like it did for me.
http://image.prntscr.com/image/5784f...2434f55d02.png

33. Scroll down to see TIPS or if you're new to C# .NET programming.

Detailed Step-By-Step Instructions on Ubuntu 32-Bit:
I am on Ubuntu 16.10 32-bit (should work for 64-bit too).
http://image.prntscr.com/image/cc4bb...097b46a95c.png
I created a fresh install of Ubuntu 16.10 just for this tutorial

1. Download and install MonoDevelop from the Software Center. (You can use any .NET Framework IDE you want, suggested Xamarin or MonoDevelop)
http://image.prntscr.com/image/0ec15...cd7958944f.png

2. Download the SA-MP 0.3.7-R2 Linux Server (x86) files. (Source: http://www.sa-mp.com/download.php)
http://image.prntscr.com/image/497e3...6df153482c.png

3. Download the libSampSharp.so.
(If you want to build the plugin yourself, download the source and follow these instructions)
http://image.prntscr.com/image/e2c71...d538132777.png

4. If you didn't download the SampSharp source on Step 3, download it now. (Don't worry you don't have to do any C/C++ compiling)
http://image.prntscr.com/image/0b38b...e52a8d481f.png

5. I'm going to put the 'MyGamemode' folder on the Desktop ('Home/Desktop/MyGamemode')
(NOTE: File and folder names on Linux are case sensitive!)
Within the 'Home/Desktop/MyGamemode' also create 'Home/Desktop/MyGamemode/env' and 'Home/Desktop/MyGamemode/src'
http://image.prntscr.com/image/1c358...90595b0f59.png

6. Extract the SA-MP files into the 'Home/Desktop/MyGamemode/env' folder.
http://image.prntscr.com/image/9a01b...9854f65df6.png

7. Move (or copy&paste) the libSampSharp.so file from your downloads to the 'Home/Desktop/MyGamemode/env/plugins' folder.
http://image.prntscr.com/image/92485...fcfe4f85d8.png

8. Open the SampSharp files and navigate to 'SampSharp-master/env', extract all the files into 'Home/Desktop/MyGamemode/env'.
http://image.prntscr.com/image/753d0...c17dec6496.png

9. In Ubuntu, open your Terminal and follow these instructions to install Mono on your machine.
Quote:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install mono-complete

After following the instructions use the Terminal command 'mono --version' to check your version, it should match mine or be a higher version (v4.6.2+).
https://files.gitter.im/ikkentim/Sam...dZiK/image.png

10. Open 'Home/Desktop/MyGamemode/env/server.cfg' and edit it to look similar to below:
http://image.prntscr.com/image/9e153...2264140430.png
Quote:

port 7777
hostname SA-MP 0.3 Server
weburl www.sa-mp.com
rcon_password changed

maxplayers 1000
maxnpc 1000

lanmode 1
announce 1
query 1

onfoot_rate 40
incar_rate 40
weapon_rate 40
stream_distance 300.0
stream_rate 1000

chatlogging 0
logtimeformat [%H:%M:%S]

#
# SA-MP#
#
gamemode0 empty 1
gamemode MyGamemode:GameMode

# plugins SampSharp.dll
plugins libSampSharp.so

11. Now open your IDE, I'm using MonoDevelop.
http://image.prntscr.com/image/4b4f1...a85cf8a45e.png

12. File > New > Solution
http://image.prntscr.com/image/35f57...a494e8aa18.png

13. Other > .NET > Library, click on Next.
http://image.prntscr.com/image/4ca41...860be3d658.png

14. Name the Project 'MyGamemode', set directory to 'Home/Desktop/MyGamemode/src', untick Create a project within the solution directory, and click on Create.
http://image.prntscr.com/image/b3834...0a7fcc0096.png

15. Right-click the second MyGamemode in the Solution menu and select Options.
http://image.prntscr.com/image/e1ccb...c3ad6dc345.png

16. Go to Build > Custom Commands, in the drop down select After Build, and match the screenshot's options.
http://image.prntscr.com/image/5d419...826b5756ea.png

17. Go to Build > Output, change the output path, navigate to 'Home/Desktop/MyGamemode/env/gamemode', and click on Ok.
< Forgot to take screenshot, sorry! >

18. In the Solution menu, right-click the second MyGamemode > Add > Add NuGet packages...
http://image.prntscr.com/image/6f35b...256cd1a16d.png

19. Tick Show pre-release packages.
http://image.prntscr.com/image/e1452...6e14b24c9a.png

20. Drop-down Official NuGet Gallery > Configure sources...
http://image.prntscr.com/image/1570e...719b91f478.png

21. Click Add, Name 'SampSharp', URL 'http://nuget.timpotze.nl/api/v2/', click Add Source, and click Ok.
http://image.prntscr.com/image/526d0...22d2e857be.png

22. Drop-down Official NuGet Gallery select SampSharp.
http://image.prntscr.com/image/d3509...dcd96711c9.png

23. Select SampSharp Framework and click Add Package. (Make sure Show pre-release packages is ticked!)
(Optional: If you are using Streamer Plugin by Incognito install SampSharp.Streamer extension for SA-MP# too)
http://image.prntscr.com/image/12074...dcbb4937b2.png

24. Rename 'MyClass.cs' to 'GameMode.cs'.
http://image.prntscr.com/image/83258...f8404ddb91.png

25. Replace the contents with the same contents from the Windows instructions [Step 29]. (I'm not double copying the code)
http://image.prntscr.com/image/5e967...c7e18c26a9.png

26. Press F7 on your Keyboard to build the project. CONGRATULATIONS it should have worked.
http://image.prntscr.com/image/071cd...df0b39d59c.png
(You can verify if it worked by going into 'Home/Desktop/MyGamemode/env/server_log.txt' where you will see the server output.)

27. Scroll down to see TIPS or if you're new to C# .NET programming.

OPTIONAL:
This option may only work on Windows, have not tried Ubuntu.
To make the Start button more useful in your code you can modify the OnInitialized override:
http://image.prntscr.com/image/57f7f...4ef2b69150.png
This will start the SA-MP client for the localhost every time samp-server.exe executes.
http://image.prntscr.com/image/2ec45...23f1591425.png
Код:
using System.Diagnostics;
Код:
        protected override void OnInitialized(EventArgs e)
        {
            Console.WriteLine("\n----------------------------------");
            Console.WriteLine(" Blank Gamemode by your name here");
            Console.WriteLine("----------------------------------\n");

            /*
             * TODO: Do your initialisation and loading of data here.
             */
            base.OnInitialized(e);

            /*
             *   Start the client for localhost.
             */
            Console.WriteLine("Attempting to open the SA-MP client...");
            
            try
            {
                Process.Start(@"samp:\\127.0.0.1:7777");
                
                Console.WriteLine("SA-MP Client opened.");
            }
            catch(Exception ex)
            {
                Console.WriteLine("SA-MP Client did not open: {0}", ex.Message);
            }
            return;
        }
TIPS:
IntelliSense is available on Visual Studio and MonoDevelop The examples are in Visual Studio though.

Use IntelliSense (Visual Studio) to see what you can override ("callbacks"):
While in between the brackets {} of the GameMode : BaseMode class, type 'override ' and this will pop-up,
http://image.prntscr.com/image/8d183...9abe73b6b3.png

Use IntelliSense (Visual Studio) to see class methods (functions):
Type the instance of the class I.E. player and a period, 'player.', like so,
http://image.prntscr.com/image/0b1f4...4300ab4cc3.png
http://image.prntscr.com/image/542f9...51c7f16ad8.png (I used player.SendClientMessage)
http://image.prntscr.com/image/2e7eb...e01341e43c.png (In-game screenshot)

IntelliSense (Visual Studio) to customize OnPlayerDisconnect example:
http://image.prntscr.com/image/9d2be...b5640b040a.png (OnPlayerDisconnect)
http://image.prntscr.com/image/4c882...20bdbcbaba.png (Console)

Microsoft Academy C# Beginner's course (free):
https://mva.microsoft.com/en-US/trai...eginners-16169

Another C# .NET Beginner's course (Udemy Instructor):
https://www.youtube.com/watch?v=gfkTfcpWqAY

Best ******* C# Cheatsheet:
https://www.youtube.com/watch?v=lisiwUZJXqQ
Reply
#2

Nicely Done i rly liked it so much
Reply
#3

The Ubuntu instructions are now operational
Reply
#4

Updated the download links for the latest stable release of SampSharp.
Reply
#5

Great tutorial! C# and SA:MP are a great combination.
Reply
#6

There's a new place to ask questions: Slack.
Reply
#7

Added a step (#3) to the windows instruction based on a newly found requirement. (Visual C++ Redistributable)
Reply
#8

Wow. 0.0
Awesome man.. +2.rep
Reply
#9

nice
Reply
#10

Amazing, would like to switch to this right away
Reply
#11

How install this to use .NET Core?
Reply
#12

Great tutorial!
Can you please, update the screenshots somehow?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)