I had a doubt, since pcvars are faster than cvars and efficient too. Can you please explain me what's the difference between pcvars and cvars? I've understood this --->
Quote:
Originally Posted by schmurgel1983
(Post 1026696)
pcvar:
PHP Code:
new pcvar
pcvar = register_cvar("my_pcvar", "1")
cvar:
PHP Code:
register_cvar("my_cvar", "1")
But how does it make pcvars more efficient?
About inc files, here's hamsandwich module's inc file.
Spoiler
PHP Code:
/**
* Ham Sandwich module include file.
* (c) 2007, The AMX Mod X Development Team
*
* -
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
/**
* Ham Sandwich is a module that is used to hook and call virtual functions of
* entities.
* Virtual functions are mod-specific functions. This means that in order
* for this to work on a mod, it needs to be configured with the hamdata.ini
* file.
* Be very careful with parameter passing to these functions.
*/
#if defined _hamsandwich_included
#endinput
#endif
#define _hamsandwich_included
/**
* Hooks the virtual table for the specified entity class.
* An example would be: RegisterHam(Ham_TakeDamage, "player", "player_hurt");
* Look at the Ham enum for parameter lists.
*
* @param function The function to hook.
* @param EntityClass The entity classname to hook.
* @param callback The forward to call.
* @param post Whether or not to forward this in post.
* @return Returns a handle to the forward. Use EnableHamForward/DisableHamForward to toggle the forward on or off.
*/
native HamHook:RegisterHam(Ham:function, const EntityClass[], const Callback[], Post=0);
/**
* Hooks the virtual table for the specified entity's class.
* An example would be: RegisterHam(Ham_TakeDamage, id, "player_hurt");
* Look at the Ham enum for parameter lists.
* Note: This will cause hooks for the entire internal class that the entity is
* not exclusively for the provided entity.
*
* @param function The function to hook.
* @param EntityId The entity classname to hook.
* @param callback The forward to call.
* @param post Whether or not to forward this in post.
* @return Returns a handle to the forward. Use EnableHamForward/DisableHamForward to toggle the forward on or off.
*/
native HamHook:RegisterHamFromEntity(Ham:function, EntityId, const Callback[], Post=0);
/**
* Stops a ham forward from triggering.
* Use the return value from RegisterHam as the parameter here!
*
* @param fwd The forward to stop.
*/
native DisableHamForward(HamHook:fwd);
/**
* Starts a ham forward back up.
* Use the return value from RegisterHam as the parameter here!
*
* @param fwd The forward to re-enable.
*/
native EnableHamForward(HamHook:fwd);
/**
* Executes the virtual function on the entity.
* Look at the Ham enum for parameter lists.
*
* @param function The function to call.
* @param id The id of the entity to execute it on.
*/
native ExecuteHam(Ham:function, this, any:...);
/**
* Executes the virtual function on the entity, this will trigger all hooks on that function.
* Be very careful about recursion!
* Look at the Ham enum for parameter lists.
*
* @param function The function to call.
* @param id The id of the entity to execute it on.
*/
native ExecuteHamB(Ham:function, this, any:...);
/**
* Gets the return status of the current hook.
* This is useful to determine what return natives to use.
*
* @return The current status of the hook (such as HAM_SUPERCEDE).
*/
native GetHamReturnStatus();
/**
* Gets the return value of a hook for hooks that return integers or booleans.
*
* @param output The variable to store the value in.
*/
native GetHamReturnInteger(&output);
/**
* Gets the return value of a hook for hooks that return float.
*
* @param output The variable to store the value in.
*/
native GetHamReturnFloat(&Float:output);
/**
* Gets the return value of a hook for hooks that return Vectors.
*
* @param output The variable to store the value in.
*/
native GetHamReturnVector(Float:output[3]);
/**
* Gets the return value of a hook for hooks that return entities.
*
* @param output The variable to store the value in. Will be -1 on null.
*/
native GetHamReturnEntity(&output);
/**
* Gets the return value of a hook for hooks that return strings.
*
* @param output The buffer to store the string in.
* @param size The string size of the buffer.
*/
native GetHamReturnString(output[], size);
/**
* Gets the original return value of a hook for hooks that return integers or booleans.
*
* @param output The variable to store the value in.
*/
native GetOrigHamReturnInteger(&output);
/**
* Gets the original return value of a hook for hooks that return floats.
*
* @param output The variable to store the value in.
*/
native GetOrigHamReturnFloat(&Float:output);
/**
* Gets the original return value of a hook for hooks that return Vectors.
*
* @param output The variable to store the value in.
*/
native GetOrigHamReturnVector(Float:output[3]);
/**
* Gets the original return value of a hook for hooks that return entities.
*
* @param output The variable to store the value in. -1 on null.
*/
native GetOrigHamReturnEntity(&output);
/**
* Gets the original return value of a hook for hooks that return strings.
*
* @param output The buffer to store the string in.
* @param size The size of the buffer.
*/
native GetOrigHamReturnString(output[], size);
/**
* Sets the return value of a hook that returns an integer or boolean.
* This needs to be used in conjunction with HAM_OVERRIDE or HAM_SUPERCEDE.
*
* @param value The value to set the return to.
*/
native SetHamReturnInteger(value);
/**
* Sets the return value of a hook that returns a float.
* This needs to be used in conjunction with HAM_OVERRIDE or HAM_SUPERCEDE.
*
* @param value The value to set the return to.
*/
native SetHamReturnFloat(Float:value);
/**
* Sets the return value of a hook that returns a Vector.
* This needs to be used in conjunction with HAM_OVERRIDE or HAM_SUPERCEDE.
*
* @param value The value to set the return to.
*/
native SetHamReturnVector(const Float:value[3]);
/**
* Sets the return value of a hook that returns an entity. Set to -1 for null.
* This needs to be used in conjunction with HAM_OVERRIDE or HAM_SUPERCEDE.
*
* @param value The value to set the return to.
*/
native SetHamReturnEntity(value);
/**
* Sets the return value of a hook that returns a string.
* This needs to be used in conjunction with HAM_OVERRIDE or HAM_SUPERCEDE.
*
* @param value The value to set the return to.
*/
native SetHamReturnString(const value[]);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are integers.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamInteger(which, value);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are floats.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamFloat(which, Float:value);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are Vectors.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamVector(which, const Float:value[3]);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are entities.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamEntity(which, value);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are strings.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamString(which, const output[]);
/**
* Sets a parameter on the fly of the current hook. This has no effect in post hooks.
* Use this on parameters that are trace result handles.
*
* @param which Which parameter to change. Starts at 1, and works up from the left to right. 1 is always "this".
* @param value The value to change it to.
*/
native SetHamParamTraceResult(which, tr_handle);
/**
* Returns whether or not the function for the specified Ham is valid.
* Things that would make it invalid would be bounds (an older module version
* may not have all of the functions), and the function not being found in
* the mod's hamdata.ini file.
*
* @param function The function to look up.
* @return true if the function is valid, false otherwise.
*/
native bool:IsHamValid(Ham:function);
/**
* This is used to compliment fakemeta's {get,set}_pdata_{int,float,string}.
* This requires the mod to have the pev and base fields set in hamdata.ini.
* Note this dereferences memory! Improper use of this will crash the server.
* This will return an index of the corresponding cbase field in private data.
* Returns -1 on a null entry.
*
* @param id The entity to examine the private data.
* @param offset The windows offset of the data.
* @param linuxdiff The linux difference of the data.
* @param macdiff The mac os x difference of the data.
* @return The index of the corresponding pdata field. -1 for none set.
*/
native get_pdata_cbase(id, offset, linuxdiff=5, macdiff=5);
/**
* This is used to compliment fakemeta's {get,set}_pdata_{int,float,string}.
* This requires the mod to have the pev and base fields set in hamdata.ini.
* This will set the corresponding cbase field in private data with the index.
* Pass -1 to null the entry.
*
* @param id The entity to examine the private data.
* @param offset The windows offset of the data.
* @param value The index to store, -1 for invalid
* @param linuxdiff The linux difference of the data.
* @param macdiff The mac os x difference of the data.
*/
native set_pdata_cbase(id, offset, value, linuxdiff=5, macdiff=5);
/**
* This is similar to the get_pdata_cbase, however it does not dereference memory.
* This is many times slower than get_pdata_cbase, and this should only be used
* for testing and finding of offsets, not actual release quality plugins.
* This will return an index of the corresponding cbase field in private data.
* Returns -1 on a null entry. -2 on an invalid entry.
*
* @param id Entry to examine the private data.
* @param offset The windows offset of the data.
* @param linuxdiff The linux difference of the data.
* @param macdiff The mac os x difference of the data.
* @return The index of the corresponding pdata field, -1 for null, -2 for invalid.
*/
native get_pdata_cbase_safe(id, offset, linuxdiff=5, macdiff=5);
// This is the callback from the module, this handles any fatal errors.
// This will in turn call the "HamFilter(Ham:id, HamError:err, const reason[])" public, if it exists.
// Return PLUGIN_HANDLED from within the HamFilter to stop the plugin from failing.
// Any other return value will fail the plugin.
// You do not need to have a HamFilter, if there is none, all fatal errors will fail the plugin.
// Do not modify this!
public __fatal_ham_error(Ham:id, HamError:err, const reason[])
{
new func=get_func_id("HamFilter", -1);
new bool:fail=true;
if (func != -1 && callfunc_begin_i(func, -1)==1)
{
callfunc_push_int(_:id);
callfunc_push_int(_:err);
callfunc_push_str(reason, false);
if (callfunc_end()==PLUGIN_HANDLED)
{
fail=false;
}
}
if (fail)
{
set_fail_state(reason);
}
}
I had requested a respawn plugin few days ago, that plugin had this code
PHP Code:
ExecuteHamB(Ham_CS_RoundRespawn, id)
But in inc file, its like this
PHP Code:
native ExecuteHamB(Ham:function, this, any:...);
So I did not get this. What does "Ham:function" "this" "any" means?
Also same with default include files
Thanks.
ddhoward
06-09-2014 12:42
Re: Cvars, Pcvars, & inc files.
You are storing a "pointer" to the cvar in a variable. This way, when you reference the cvar again later, you are not making AMX Mod X search through the giant list of cvars a second time. Rather than saying to AMX "hey can you check the value of that cvar again? I forgot where it is, so you'll have to look through the list again for the right cvar" you are instead saying "Hey, can you check the value of that cvar again? It was the 321st cvar last time we checked." or something similar.
The include is showing you what is required in the native call. For example, that particular native requires you to specify a Ham function, an entity on which the function is being executed, followed by any number of parameters of any type.
Ham: and any: are tags. Much like Array:, Regex:, Float:, bool: and so on.
If you use another value except from the predefined list using the Ham: tag you will get a tag mismatch warning.
This is to hint people that they most likely is using the native incorrectly, trying to send a player index for example.
Custom tags have absolutely no effect at all after the compilation. The only tag(s) that actually treat the data in a different matter is Float: and perhaps bool:(?). bool may also just be a tagged 32-bit integer (1 cell) with a value of 1/0. I'm not entirely sure.
any: will allow you to send anything with any tag at that location without getting an error on compilation.
... will allow you to send any number of arguments without getting an error about wrong argument count.
"this" is just the name of that variable. What that actually is depends on which function is called. For any Ham_Player_* function, "this" is referring to the player index. And for all the Ham_Item_* functions "this" is referring to the item entity index. You can find more information in ham_const.inc.
When declaring natives for includes the names of the parameters makes no difference at all since natives get their parameters by numbers. They're often named so that people will understand what it is about. In the case of Ham, it can mean a lot of things. This is why "this" was selected instead.
EthicalHacker007
06-10-2014 04:09
Re: Cvars, Pcvars, & inc files.
Quote:
Originally Posted by ddhoward
(Post 2148956)
You are storing a "pointer" to the cvar in a variable. This way, when you reference the cvar again later, you are not making AMX Mod X search through the giant list of cvars a second time. Rather than saying to AMX "hey can you check the value of that cvar again? I forgot where it is, so you'll have to look through the list again for the right cvar" you are instead saying "Hey, can you check the value of that cvar again? It was the 321st cvar last time we checked." or something similar.
The include is showing you what is required in the native call. For example, that particular native requires you to specify a Ham function, an entity on which the function is being executed, followed by any number of parameters of any type.
Just checked ham_const.inc, it's same better than funcwiki. Thank you.
Quote:
Originally Posted by Black Rose
(Post 2149003)
Ham: and any: are tags. Much like Array:, Regex:, Float:, bool: and so on.
Hmmm.
Quote:
Originally Posted by Black Rose
(Post 2149003)
If you use another value except from the predefined list using the Ham: tag you will get a tag mismatch warning.
And where is the predefined list? (noob questions)
Quote:
Originally Posted by Black Rose
(Post 2149003)
This is to hint people that they most likely is using the native incorrectly, trying to send a player index for example.
Oh.
Quote:
Originally Posted by Black Rose
(Post 2149003)
Custom tags have absolutely no effect at all after the compilation. The only tag(s) that actually treat the data in a different matter is Float: and perhaps bool:(?). bool may also just be a tagged 32-bit integer (1 cell) with a value of 1/0. I'm not entirely sure.
Did not understood this, NVM. Will keep on learning.
Quote:
Originally Posted by Black Rose
(Post 2149003)
any: will allow you to send anything with any tag at that location without getting an error on compilation.
... will allow you to send any number of arguments without getting an error about wrong argument count.
"this" is just the name of that variable.
It would be great if you would explain this in layman's terms. Sorry.
Quote:
Originally Posted by Black Rose
(Post 2149003)
What that actually is depends on which function is called. For any Ham_Player_* function, "this" is referring to the player index. And for all the Ham_Item_* functions "this" is referring to the item entity index. You can find more information in ham_const.inc.
I read ham_const.inc, but didn't understood much. Where can I get these functions? Every function in include files is explained like this.
Code:
/**
42 * Description: This is typically called whenever an entity is created.
43 * It is the virtual equivilent of spawn from the engine.
44 * Some mods call this on player spawns too.
45 * Forward params: function(this)
46 * Return type: None.
47 * Execute params: ExecuteHam(Ham_Spawn, this);
48 */
I don't understand what's this forward params and execute params. Can you please explain this in short, basic language? Thanks.
Quote:
Originally Posted by Black Rose
(Post 2149003)
When declaring natives for includes the names of the parameters makes no difference at all since natives get their parameters by numbers. They're often named so that people will understand what it is about. In the case of Ham, it can mean a lot of things. This is why "this" was selected instead.
Thank You.
Nextra
06-10-2014 17:24
Re: Cvars, Pcvars, & inc files.
Quote:
Originally Posted by EthicalHacker007
(Post 2148952)
I had a doubt, since pcvars are faster than cvars and efficient too. Can you please explain me what's the difference between pcvars and cvars? I've understood this.
This code will show you the general difference between the two:
PHP Code:
get_cvar_num(name[]) { for (new i = 0; i < g_cvar_count; ++i) { if (equal(name, g_cvar_list[i])) return g_cvar_values[i]; }
Of course there is some extra safety internally, but this is the idea behind pcvars. They directly access the cvar while the old get_cvar natives have to search through a list of potentially thousands of cvars.
Quote:
Originally Posted by EthicalHacker007
(Post 2148952)
PHP Code:
native ExecuteHamB(Ham:function, this, any:...);
So I did not get this. What does "Ham:function" "this" "any" means?
Also same with default include files
Thanks.
Quote:
Originally Posted by EthicalHacker007
(Post 2149216)
And where is the predefined list? (noob questions)
Ham: refers to the Ham enum in ham_const.inc. If you remove all the documentation in ham_const.inc it looks like this:
It is there to ensure you actually pass in one of the Ham_* constants instead of a normal integer. This allows the compiler to give you a "tag mismatch" error if you don't do that.
any: is not a tag like all the others. any: is a magic tag that tells the compiler to ignore the tag of the variable or constant you pass into the function. I'll explain it using one of the cellaray.inc natives:
If ArrayPushCell was defined without the any: tag the compiler would expect an integer to be passed as the second argument. It would issue a "tag mismatch" warning although it is clearly okay to store floats in an array. But we can't use Float:input either because then a tag mismatch would happen when we use an integer. any: gets rid of this and simply allows any tag. Here are three examples that show the different scenarios.
Originally Posted by EthicalHacker007
(Post 2149216)
Did not understood this, NVM. Will keep on learning.
Tags in AMXX are introduced by enums. When the plugin is compiled they only matter for some light type checking that will result in tag mismatch errors. Other than that they are standard integers and will be treated as such.
The Float: tag is another kind of magic tag in AMXX. Unlike a language like C for example Pawn has no real types. Everything in AMXX is a 32bit integer, called a cell. Without the special Float: tag we wouldn't be able to work with floating point math in AMXX. All this magic happens in float.inc, where you will find things like this:
This is a light version of operator overloading which can be found in C++ for example. This code means that this transformation will take place:
PHP Code:
// This code ... new Float:fraction, Float:fraction2, number; fraction = fraction + fraction2; fraction = fraction2 + number;
PHP Code:
// ... will become this new Float:float, Float:float2, int; fraction = floatadd(fraction, fraction2); fraction = floatadd(fraction, float(number));
The natives like floatadd, floatsub and the like are actually responsible for treating the numbers as floating point. Without this transformation anything would behave like an integer. Only the AMXX core actually knows what floats are!
Quote:
Originally Posted by EthicalHacker007
(Post 2149216)
I don't understand what's this forward params and execute params. Can you please explain this in short, basic language? Thanks.
Consider Ham_Spawn:
PHP Code:
/** * Description: This is typically called whenever an entity is created. * It is the virtual equivilent of spawn from the engine. * Some mods call this on player spawns too. * Forward params: function(this) * Return type: None. * Execute params: ExecuteHam(Ham_Spawn, this); */ Ham_Spawn = 0,
Description tells you what this function is and when it is usually called. To find precise information for your mod you should search this forum or do some testing on your own.
Forward params tells you how your function will be called if you use RegisterHam. For example:
PHP Code:
RegisterHam(Ham_Spawn, "player", "ham_spawn");
// ...
public ham_spawn(this) { // This is your forward function. It will only receive one parameter. // In this case, "this" will actually refer to a "player" entity, as defined in RegisterHam's second parameter. }
Return type tells you what the function returns when it runs. You need this information when you want to use the SetHamReturn* natives inside a Hamsandwich forward.
Execute params tells you how you should execute the function using ExecuteHam. This equivalent to the forward params of course.
this is simply the entity that the function has or should run on. So when you use RegisterHam with the player entity class the this parameter will refer to a player entity, which is usually a client. The naming comes from the this pointer in C++ as Hamsandwich deals with virtual functions of the various C++ classes that represent the entities inside of the engine. This is a technical detail you don't really need to understand (yet).
EthicalHacker007
06-11-2014 01:11
Re: Cvars, Pcvars, & inc files.
Thanks very much Nextra. For typing this much and explaining it. Thank you. I understood most of it, I'll study more about includes. Thanks.