[Tutorial] How to make a simple plugin? (with images)
#1

Hello all.
Intro
To coding heavy projects (i mean samp servers) you have to write your own functions or use includes or plugins.
for that people want to wriite their own functions: some of functions can't be write in PAWN. Need be native function.

This is a tut (my first tut) for creating simple function with plugin using Visual Studio 2012.
Anywhere of the whole tut, if any bad words you see, tell me via PM. also Sorry for the bad english.

Quote:

VS 2012 = Visual Studio 2012



Let's start it!
1. You need have a C++ compiler. I use VS 2012* in this tutorial.

2. Open up the VS 2012.

3. Click on the New Project button/link. (on the File > new > project ...) or CTRL+SHIFT+N.

You must see this : (if you installed the VS 2012 correctly)

http://i.gyazo.com/7ff496f075da74b13808105dc63dd184.png

4. Click on the Visual c++.

5. Select 'Win32 Console App'.

http://i.gyazo.com/3c3bb31293cfd7eccebe03b96f60a939.png

5. go to the Name section and type a suitable name.

http://i.gyazo.com/6d40a091aa7414f8ed8c4542fe8acb8d.png

I leave it the ConsoleApplication5. Tap 'OK'.

You see this :

http://i.gyazo.com/d7683f6d9472c3c1ac55cd49cd486aba.png

6. Click 'next >'.

You see Application Settings.

Set the options like me. (in the below image

i.gyazo.com/e04f73fd5f2129f9662a9d0e44542bd0.png

You are done. Click 'finish'.

Wait project loads.

Before start development. You need the SA:MP Plugin SDK, download from here :

Download

Go to the project location selected in New pROJECT. In my case :
Код:
C:\Users\i Farbod\Documents\Visual Studio 2012\Projects\ConsoleApplication5
Extract the SDK you downloaded there. like this :

http://i.gyazo.com/b2da9267ae41aacca3ff9914b816dc48.png

7. Go to you Solution Explorer.

i.gyazo.com/ecddbf8f6e36024dc9145591742b9e00.png

8. Right click on the project name. in my case ConsoleApplication5.

9. Add > New Filter. Name it SDK.

i.gyazo.com/af1a1a2204193048126b62d54cd65d4b.png

9. Go on SDK. Right Click > 'Add' > 'Existing Item ...'.

You see this :

i.gyazo.com/79412fcee76846d48cfc0f7326bd0110.png

Open 'SDK' then : ADD amxplugin.cpp.

Repeat this and ADD plugincommon.h.

OK, That all. you get started. let's DEV!

Development

10. Right click on Source Files. then 'ADD', new item.

i.gyazo.com/956a8829da7d727991534c8beb6c1219.png

Add a New Module Definition File (.def). we need this for build our plugin.

Name it. I leave it 'Source.def'. Click aDD.

A new tab open with this .def file.

put this code in it :

Код:
LIBRARY "ConsoleApplication5"
EXPORTS
	Supports
	Load
	Unload
	AmxLoad
	AmxUnload
Save the project, Ctrl+S.

i.gyazo.com/390533cbd5dd77992d20fd3c02f47217.png

We need to create a cpp file holds the natives and data of the Plugin.

11. Right click again on Source files. ADD NEW ITEM. .cpp file and name it. i leave it 'Source.cpp'.

12. Add it. then go :
SLN (Solution Explorer) > ConsoleApplication5 > [Right Click] > Properties.

Go Linker > input .

http://i.gyazo.com/6b42164f0b0e3671381b6d6c1e4bc87e.png

Locate your Module Definition File like me.

Click ok.

13. Go to the Source.cpp. Paste this into this :

Код:
#include "..\SDK\amx\amx.h"
#include "..\SDK\plugincommon.h"


typedef void (*logprintf_t)(char* format, ...);


logprintf_t logprintf;
extern void *pAMXFunctions;


cell AMX_NATIVE_CALL OurNative(AMX* amx, cell* params)
{
    logprintf("TesT!!");
    return 1;
}

PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports() 
{
    return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}

PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData) 
{
    pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
    logprintf = (logprintf_t) ppData[PLUGIN_DATA_LOGPRINTF];

    logprintf("// Our Plugin loaded. Whoa!");
    return true;
}

PLUGIN_EXPORT void PLUGIN_CALL Unload()
{
    logprintf("$$$ plugin UnLoAdEd!!! $$$");
}

AMX_NATIVE_INFO PluginNatives[] =
{
    {"OurNative", OurNative},
    {0, 0}
};

PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx ) 
{
    return amx_Register(amx, PluginNatives, -1);
}


PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx ) 
{
    return AMX_ERR_NONE;
}
Save it.

We WAnt have two natives. SumOf(x, y); and DivideOf(x, y);

First is for adding param 1 to param 2. (Sum)
Second for divide.

Go to here :

Код:
cell AMX_NATIVE_CALL OurNative(AMX* amx, cell* params)
{
    logprintf("TesT!!");
    return 1;
}
replace it with this :

Код:
cell AMX_NATIVE_CALL SumOf(AMX* amx, cell* params)
{
    return params[1] + params[2];
}

cell AMX_NATIVE_CALL DivideOf(AMX* amx, cell* params)
{
    return params[1] / params[2];
}
Then go here :

Код:
AMX_NATIVE_INFO PluginNatives[] =
{
    {"OurNative", OurNative},
    {0, 0}
};
Replace it with this :

Код:
AMX_NATIVE_INFO PluginNatives[] =
{
    {"SumOf", SumOf},
	{"DivideOf", DivideOf},
    {0, 0}
};
press F6 to build.

Go to :

C:\Users\i Farbod\Documents\Visual Studio 2012\Projects\ConsoleApplication5\Debug

Grab the DLL. Copy it to the 'plugins' in your server.

14. Go to server.cfg and add this :

Код:
plugins ConsoleApplication5
15, Save it and open up the samp-server.exe.

You Should see this :

Код:
// Our Plugin loaded. Whoa!
http://i.gyazo.com/9b6606f7cb3195ec79aa5247fbca3251.png

16. Our plugin loaded. Let's go start using natives.

Using Natives

We want to create a Include for our plugin.

17. Go to the /pawno/include. new .inc file.

18. Rename it to whatever. 'CA5' in my case.

http://i.gyazo.com/4527a9684f56fc655f98c4e42f45756c.png

19. Open it. paste the code init:

Код:
native SumOf(x, y);
native DivideOf(x, y);
Save it.

Paste this in a script (GM).

Код:
#include <a_samp>
#include <CA5>

main()
{
	printf("Sum of 1 and 2 : %d", SumOf(1, 2));
	printf("Divide of 64 and 2 : %d", DivideOf(64, 2));
}
Compile it, load it. Open up the samp-server.exe.

You Should see this :

i.gyazo.com/8fee788e5507cd18f394698f000d72c4.png

Код:
[01:00:20] Sum of 1 and 2 : 3
[01:00:20] Divide of 64 and 2 : 32
Pretty easy? No?

You build more powerful plugins. not like this, this was easy!

like SSCANF, MySQL for SA:MP, Streamer , and more.

Conclusion
Thanks for read this.
You can find another useful resources in C/C++ dev sites.

This tutorial is good for learning to create plugins.

Thanks!!

DoWnLoAd
Project, Plugin DLL, Include :

SolidFiles
MediaFire
DropBox
Reply
#2

For images use [img][/img] tags. Anyway good turorial
Reply
#3

Quote:
Originally Posted by QuaTTrO
Посмотреть сообщение
For images use [img][/img] tags. Anyway good turorial
My Net Connection is some slow but Ok i edit it
Reply
#4

Project, Compiled Plugin DLL, Include Added!

Also Give a Rep if useful!
Reply
#5

Nice turorial
Reply
#6

nice job iFarbod.
Reply
#7

Thanks
Reply
#8

Quote:
Originally Posted by iFarbod
Посмотреть сообщение
[Tutorial] How to make a simple plugin? (with images)
You can make plugin with images?????
Reply
#9

Quote:
Originally Posted by ic3cr3am
Посмотреть сообщение
You can make plugin with images??
with pictures, edited (sorry)
Reply
#10

Nice Tutorial For Begginers But You Know Our New Scripters Should Have Some Knowledge
For C++
Good One Farbod
And Can You Add My Skype Or My Yahoo ID
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)