SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 26/04/2016, 06:19 PM   #1
Crystallize
High-roller
 
Join Date: Aug 2013
Posts: 1,553
Reputation: 293
Default NPC Plugins

Introduction
Technically, PAWN supports plugins natively, but there was never an SDK for them released for SA:MP. Back before the main SA:MP plugin SDK was released someone was working on it but they never finished and I had no clue what was going on. Since then, however, I have learnt a vast amount more and discovered that using these plugins wasn't actually that hard - so I wrote this library to use them. Well, I more combined existing bits of code from the SA:MP plugin SDK and the open-source implementation of PAWN 3.2.3664 found here.

Use
There are some minor differences between using this system and the original SA:MP plugin SDK for the main server, and I'll detail them all here. Note that this is mostly for development on WINDOWS - I've not made a Linux version yet but doing so won't be hard.
  • Naming
I've simplified dll naming. Previously, you had to edit "helloworld" in a couple of places to change the name of the plugin - now everything is tied to the Visual Studio PROJECT name (note that this may be the same as the SOLUTION name, but that doesn't matter in the slightest):


In this example the plugin is called "npcdll" (for fairly obvious reasons), and the project is set up assuming that your plugin's directory is in "npcmodes/". This will create a file called "../../amxnpcdll.dll" (PAWN requires "amx<plugin name>.dll"), which should put it in the server root directory. I.e, my layout is:
Code:
<root>
├───filterscripts
├───gamemodes
├───include
├───npcmodes
│   ├───npcdll
│   │   └───SDK
│   │       └───amx
├───pawno
├───plugins
├───scriptfiles
<etc>
If you put the plugin's project folder somewhere else, make sure you move the resulting .dll file to the server root (the same directory as "samp-npc.exe").
  • Includes
The trick to this system is "#pragma library". Some of you may have seen this in "a_samp.inc" and other files, but its use in there is actually wrong. What that directive does is to instruct PAWN to load that library. Since our library is called "npcdll" and provides just one native, this is our include file:
Code:
#pragma library npcdll

native HelloWorld();
  • Natives
There is one small difference between SA:MP plugin natives and NPC plugin natives - "const":

SA:MP:
Code:
static cell AMX_NATIVE_CALL
    n_HelloWorld(AMX * amx, cell * params)
NPC:
Code:
static cell AMX_NATIVE_CALL
    n_HelloWorld(AMX * amx, cell const * params)
Notice the second one is "cell const" instead of "cell". Practically this makes no difference since you don't actually change those values anyway, even with reference parameters (since then the value of "params" is a pointer not the value itself), but it does make copying code awkward.
  • Load Functions
For compatability with SA:MP plugins, there are two other functions in these plugins:
Code:
PLUGIN_EXPORT int PLUGIN_CALL
    AmxLoad(AMX * amx);

PLUGIN_EXPORT int PLUGIN_CALL
    AmxUnload(AMX * amx);
You do standard setup such as registering natives in here as normal. The main difference here is that the functions "Supports", "Load", "Unload", and "ProcessTick" don't exist. The first one isn't needed, the next two can be easily simulated with "DllMain" (standard in windows dlls), and I'm sure there's a solution for the final one but I don't know it yet.
  • Printing
"printf" does nothing, neither does the non-existant "logprintf". If you want to output debug information from your plugin, use "OutputDebugString" as such (note the use of "L"):
Code:
OutputDebugString(L"Hello from this NPC plugin!\n");
This outputs a Windows debug string that can be read using a program such as DebugView.
  • Finally
That's it. If you use the solution in the download everything should work just fine. To make a new one just copy the whole solution directory. If you want to make your own solution then I suggest you read the "Advanced" section below...


Download
Get the basic project for Visual Studio here:

http://dl.dropbox.com/u/21683085/npcdll.rar

Advanced
As I said, this is a collection of several systems combined together to make it look as much as possible like standard SA:MP plugins, but it isn't really:
  • Normal plugins are given handles to the PAWN internal functions such as "amx_GetPublic" (which is how the SA:MP GDK works); however, these plugins are NOT given these handles so instead have copies of the entire code, making the whole plugin much larger.
  • The functions "AmxLoad" and "AmxUnload" are not actually called that - macros internal to Visual Studio rename them to their internal names of "amx_<project name>Init" and "amx_<project name>Cleanup". This is done just so that you don't need to rename the functions for every new project, instead everything is controlled centrally.
  • In addition to the previous point, the ".def" file is still required due to incompatibilities between "__declspec(dllexport)" and "__stdcall" in Visual Studio. This again is generated at compile time with the correct function names based on the project name (see "Build Events").
  • While the "SDK" includes in the project look the same as the standard SA:MP ones, they are not. "plugincommon.h" has been cut down, "amx.h" and "amx.c" are from the PAWN toolkit directly (hence the change to "const"), "osdefs.h" has been added, everything else is the same.
All of those things are trivial to do from a makefile on Linux, but I've not done so yet.

Credits
Full credits to Y_Less.

Last edited by Crystallize; 28/09/2017 at 03:15 PM.
Crystallize is offline   Reply With Quote
Old 26/04/2016, 06:44 PM   #2
Swedky
Banned
 
Join Date: Jun 2013
Location: Argentina.
Posts: 2,100
Reputation: 827
Default Respuesta: NPC Plugins

This looks nice, I will try make a simple NPC for test

+6
Swedky is offline   Reply With Quote
Old 29/04/2016, 01:09 PM   #3
maksie23
Little Clucker
 
Join Date: Dec 2011
Posts: 4
Reputation: 0
Default Re: NPC Plugins

Looks cool.
maksie23 is offline   Reply With Quote
Old 07/07/2016, 12:04 AM   #4
Crystallize
High-roller
 
Join Date: Aug 2013
Posts: 1,553
Reputation: 293
Default Re: NPC Plugins

Bumping this since it's really useful.
Crystallize is offline   Reply With Quote
Old 07/07/2016, 06:28 AM   #5
Ritzy
High-roller
 
Ritzy's Avatar
 
Join Date: Jun 2014
Posts: 1,492
Reputation: 112
Default Re: NPC Plugins

Good job!
Ritzy is offline   Reply With Quote
Old 27/07/2016, 12:08 AM   #6
Crystallize
High-roller
 
Join Date: Aug 2013
Posts: 1,553
Reputation: 293
Default Re: NPC Plugins

Bumping this up once again
Crystallize is offline   Reply With Quote
Old 29/01/2017, 02:06 PM   #7
Crystallize
High-roller
 
Join Date: Aug 2013
Posts: 1,553
Reputation: 293
Default Re: NPC Plugins

One more bump since it has vanished lol.
Crystallize is offline   Reply With Quote
Old 29/01/2017, 02:11 PM   #8
RyderX
Banned
 
Join Date: Oct 2016
Location: Lebanon,Beirut Rank: SA-MP Scripter
Posts: 603
Reputation: 53
Default Re: NPC Plugins

What the heck i was searching for Howto make a simple fucked npc thx crystal
RyderX is offline   Reply With Quote
Old 29/01/2017, 02:14 PM   #9
oMa37
High-roller
 
oMa37's Avatar
 
Join Date: Feb 2016
Posts: 1,034
Reputation: 152
Default Re: NPC Plugins

Quote:
Originally Posted by RyderX View Post
What the heck i was searching for Howto make a simple fucked npc thx crystal
But this tutorial is talking about how to write plugins for NPC scripts ...
oMa37 is offline   Reply With Quote
Old 29/01/2017, 02:20 PM   #10
RyderX
Banned
 
Join Date: Oct 2016
Location: Lebanon,Beirut Rank: SA-MP Scripter
Posts: 603
Reputation: 53
Default Re: NPC Plugins

Quote:
Originally Posted by oMa37 View Post
But this tutorial is talking about how to write plugins for NPC scripts ...
Damn i thought it for creating an npc sorry if i being nub
RyderX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Plugins] Runtime error, plugins don't seem to load. bradleyofniet Server Support 2 08/04/2016 05:25 PM
Plugins DeathKing Scripting Help 3 21/05/2014 07:25 AM
Plugins SPA Scripting Help 9 16/05/2014 04:56 PM
[Ajuda] Ajuda com objetos sem plugins... (Ajuda com plugins streamer também) Sonik_Paintballer Português/Portuguese 3 12/10/2012 02:54 PM
plugins geerdinho8 Server Support 0 17/07/2011 02:26 AM


All times are GMT. The time now is 11:57 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.