text
stringlengths
0
692
if (status == MSGLOG_STARTED)
{
if (!g_msgRegistered[i])
{
register_message(i, "message_forward")
g_msgRegistered[i] = true
}
}
}
}
}
/* Writes messages to log file depending on value of amx_ml_logmode */
log_msgf(const fmt[], {Float,Sql,Result,_}:...)
{
static buffer[512]
vformat(buffer, 511, fmt, 2)
if (get_pcvar_num(g_cvarLogMode))
log_to_file(LOGFILE_NAME, buffer)
else
log_amx(buffer)
}
Dynamic / Fake Natives (I will refer to them as dynamic from here onward, I don't like the word fake when they actually do something) allow you to have the functionality of a module (through natives) by having a plugin that acts like a module running. In theory, it is possible to port entire modules to fakemeta + plugins using this principle (fun has already been done, engine can be done with more difficulty). From my experiences, dynamic natives go hand and hand with API design as shown in my "Plugin API" tutorial.
Here are the natives that are used along for dynamic natives:
register_native
register_library
get_param
get_param_byref
get_param_f
get_string
get_array
get_array_f
set_array
set_array_f
set_string
param_convert
Please keep in mind that dynamic natives are pretty useless when not dealing with more than one plugin. As such, the following examples will always be at least 2 scripts, plus one include file.
Here is a simple example to show the basics of dynamic natives:
Code:
#include <amxmodx>
#include <fun>
public plugin_init()
register_plugin("Dynamic Native Test - Handler","1.0","Hawk552")
public plugin_natives()
{
register_library("dyn_test")
register_native("half_user_hp","_half_user_hp")
}
public _half_user_hp(iPlugin,iParams)
{
if(iParams != 1)
return PLUGIN_CONTINUE
new id = get_param(1)
if(!id)
return PLUGIN_CONTINUE
set_user_health(id,get_user_health(id) / 2)
return PLUGIN_HANDLED
}
Code:
#include <amxmodx>
#include <dyn_test>
public plugin_init()
register_plugin("Dynamic Native Test - Caller","1.0","Hawk552")
public client_putinserver(id)
if(is_user_alive(id))
half_user_hp(id)
Code:
// NOTE: This file MUST be called dyn_test.inc (or you have to rename the #include <dyn_test> in script 2)
#pragma reqlib "dyn_test"
native half_user_hp(id)
Now, what is this doing? Basically, on client_putinserver, if the user is alive (which is only true in mods like sven coop where you spawn right away) then it will slice their HP in half. Let's pick apart each thing.
Script 1
At first we look at the plugin_init, and we notice nothing special. We then look down and see "plugin_natives", a forward most people here have probably never seen before. This is where natives and libraries must be registered. DO NOT try to put them in other forwards, like plugin_init or plugin_precache.
Now, let's look at the first part of plugin_natives, which is register_library.
What does this do? It basically tells AMXX that this plugin provides native functionality to other plugins, and aids with "native not found" errors to check if the plugin is loaded before trying to call the dynamic native. In this case, we're calling our library "dyn_test" for "Dynamic Native Test". There are 2 types of libraries in AMXX core, as of 1.75: libraries and classes. A module is a library, while a plugin is a class. This information is useful for LibraryExists, which will be covered later.