text
stringlengths
0
692
Next, we see register_native.
const native[] - this is the name of the native to register. Pretty obvious.
const handler[] - this is the name of the function that will be called when this native is used. Remember, it must be public (as with almost all functions that are put in quotes or called by outside sources)
[ style = 0 ] - this is will be covered later
Now, as we look at the register_native call, we see the handler is _half_user_hp. So, we look down, and we see this function.
Code:
public _half_user_hp(iPlugin,iParams)
In all dynamic natives, unless registered with style=1, we see something to the effect of (id,params), which I decided to rename in the header. The first param is the plugin id - this is useful in case you want to send it back a callfunc or a CreateOneForward/ExecuteForward, or have an array that stores data for each plugin loaded. The next param is iParams. In the case of the above example, iParams must be 1, because the only param is 1. If it's anything different, someone decided to be stupid and modify the include file.
Next, inside the function we check if iParams == 1, and if it doesn't we stop there. Next, we get_param(1), which allows you to get the first parameter assuming it's a cell (which it is). We then check if id is a counting number, because we can't use 0 with get_user_health. The rest is pretty self explanatory.
In this example, I returned PLUGIN_CONTINUE because that evaluates to false, and PLUGIN_HANDLED because it evaluates to true. This allows you to do simple error checking, ie "if(!half_user_health(id)) log_amx("failed")".
Now that the handler is out of the way, we get down to the plugin that will use this dynamic native.
First, I'd like you to look at the include file (the 3rd example). The first thing we see is #pragma reqlib "dyn_test". This effectively tells AMXX "If dyn_test isn't loaded, error me right here." The register_library function in the first example is what lets you bypass this, because it tells AMXX that dyn_test does exist. Next, in this same file, we see native half_user_hp(id). This basically tells the compiler that half_user_hp will be handled in the VM layer (which is where all the C++ coding is), but the VM layer will pass it down to the function that handles the dynamic native. After it's handled, the VM layer will pass the result back to the calling function.
Onto the script itself, we see that we included this file through #include <dyn_test>. The rest is quite self explanatory.
As for the get_param section, the natives above (get_param_f, get_param_byref, etc.) can be used to replace this, depending upon what arguments are passed into the function.
Now, onto style = 1. This style basically forgets about the iPlugin,iParams part of the header, and then assumes the plugin will pass the parameters correctly. This means _half_user_hp would look like:
Code:
public _half_user_hp(id)
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <fakemeta_util>
#include <xs>
#pragma semicolon 1
#define PLUGIN_NAME "Tutorial Plugin"
#define PLUGIN_VERSION "1.0"
#define PLUGIN_AUTHOR "JoRoPiTo"
#define STR 32
#define LOG 128
#define TUT_PREFIX "[AMXX TUT]"
#define DIVISOR "======================================================="
static g_beamSprite;
static g_maxEntities;
static g_maxClients;
static g_msgSayText;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// Console commands
register_concmd("con_showents", "con_showents");
// Client commands
register_clcmd("tutorial1", "tut_FindEntityByString");
register_clcmd("tutorial2", "tut_FindEntityInSphere");
register_clcmd("tutorial3", "tut_FindClientInPVS");
register_clcmd("tutorial4", "tut_TraceLine");
register_clcmd("tutorial5", "tut_TraceModel");
g_maxEntities = global_get(glb_maxEntities);
g_maxClients = global_get(glb_maxClients);
g_msgSayText = get_user_msgid("SayText");
}
public plugin_precache()
{
g_beamSprite = precache_model("sprites/zbeam1.spr");
}
//------------------------------------------------------------------------
//Internal functions
//------------------------------------------------------------------------
stock fn_log_con(name[], msg[])
{
server_print("%s %s: %s", TUT_PREFIX, name, msg);
}
stock fn_log_cli(id, name[], msg[])
{
static text[LOG];
formatex(text, charsmax(text), "^x04%s %s:^x01 %s", TUT_PREFIX, name, msg);
if(id != 0)
{
message_begin(MSG_ONE, g_msgSayText, {0,0,0}, id);
write_byte(id);
write_string(text);
message_end();
}
else
{
client_print(0, print_chat, text);