[TUT] Plugin API
Before you get into this, please note that this tutorial assumes you are intermediate level in Pawn or higher. Beginners should avoid this as it may confuse them or not make sense.
API stands for application programming interface. But what is an API? Basically it's an interface that allows multiple programs/scripts to trade data and perform the same functions without having direct access to each other. There is API functionality in AMXX plugins, implemented through a set of natives. The following natives, built into the core, allow API capabilities:
But what are they used for? Well, let's take a look at an example and then pull it apart to figure out how it works. Please keep in mind that an API is designed for multiple plugins, so every example will be 2 individual scripts instead of 1 (although it's possible to put them both in one) Code:
Code:
Now, let's pick these two apart. In the first script, the important part starts at fnDoForward. What we see is: Code:
CreateMultiForward and CreateOneForward (which will be addressed later) return handles that can be used for ExecuteForward and DestroyForward.
But as for the parameters: const name[] - this is the name of the public function you want to call from whatever plugin(s) specified stop_type - this is what the plugin should obey in terms of stopping. For example, if you register a client command "kill" and then return PLUGIN_HANDLED, it'll stop right there. This parameter allows you to specify whether or not it will stop there, and whether or not the return value from plugins called matters. The possibilities are here: Code:
In this case, we use ET_IGNORE because plugin_init never stops calling, despite what plugins return (which is why it's useless to return PLUGIN_CONTINUE/_HANDLED in plugin_init), and we want to duplicate that functionality. ... - This is where the parameters of the header of the function called should be specified. In the above example, FP_FLOAT was specified. This is to let the AMXX core know that we want to send a floating point int to the functions called. Here are the possibilities for the previous section (I will explain how to pass an array/string later): Code:
Next we check if iForward > 0, or if it's not we stop there and inform the server console. As said in the funcwiki, "Results will be > 0 for success.". Next, we execute the forward using ExecuteForward. Picking this function apart: forward_handle - this is the forward to call. It's the handle returned from CreateMultiForward / CreateOneForward. &ret - this is the return value, passed by reference. It is effectively the value that the function being called returns (ex. return PLUGIN_HANDLED -> &ret == 1) It usually is effected by the stop_type in CreateMultiForward. ... - this is the param(s) where you can input the data that will be passed onto the function header for the function being called. In the example above, g_flDelay is passed and in the second plugin, the plugin_init_delay function recieves it in the header as Float:flDelay. NOTE: You can theoretically have infinite parameters, but they must match with the types passed into CreateOneForward / CreateMultiForward. If ExecuteForward returns 0 (false) then we stop there and inform the server of this error. Otherwise, we continue onward to DestroyForward. The next part we find is DestroyForward. The functionality for this is quite obvious, and can be used on any forward but should be used by the time plugin_end is called (or the FM forward for server shutting down) otherwise memory leaks can occur. Now, what was the point of that? Not really much, that was pretty damn useless. Here's something a little more useful: Code:
Code:
We just allowed a plugin to use client_PreThink using fakemeta without having to even create the fakemeta forward in the other plugin. On top of this, if we added another plugin to the API, it would call client_PreThink for that one too. Be careful though, it'll call it twice if you have engine included. But one thing remains unanswered: how do you pass an array/string? A special native has been implemented for this, PrepareArray. Here's an example of how to use it: Code:
Code:
Regardless of the fact we could use 2 cells instead of an array, this allows client_PreThink to have a random value from 0-5 packed into the parameters (which is quite useless, again this is just an example). Now, to pick this apart: We created an array and then packed it with data. You should know how to already do this. We then use: Code:
The usage of PrepareArray is as follows: array[] - this is the array to be prepared size - this is the amount of cells in the array (the same amount as when declaring it, not the highest cell) copyback=0 - this is whether or not changing the array will result in the calling function's array being changed as well. This defaults to 0. PrepareArray returns a handle as well that can be passed into ExecuteForward under the param of FP_ARRAY or FP_STRING. The difference between these two is that FP_STRING stops reading at the null terminator (and does not need to be prepared using PrepareArray), but FP_ARRAY must be prepared and only stops reading at the "size" cell. Well, there you have it, if you have any questions or think a statement here is wrong, please post. |
Re: Plugin API
Your very resourcefull Hawk552 :). Thanks for the tutoral.
|
Re: Plugin API
Quote:
|
Re: Plugin API
1. Your banned again so this post may be pointless :lol:
2. I think your thinking of v3x |
Re: Plugin API
Speaking of v3x I havn't seen him in a long while. How about you guys?
[EDIT] Yep, hes been missing for over a month now.... Quote:
|
Re: Plugin API
Quote:
|
Re: Plugin API
Quote:
Why not just use a name for a cvar and then a stock to check whether it's on or off? I suppose you could make a native that other plugins can use to check if your plugin is off, but if it has a cvar it's really better to avoid API / native functions. |
Re: Plugin API
Quote:
Code:
Then in another plugin Code:
Your question is kinda unclear, but I think that's what you want. You can do the same thing with any cvar like that. |
Re: Plugin API
Quote:
|
Re: Plugin API
uhh
|
All times are GMT -4. The time now is 20:00. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.