Raised This Month: $ Target: $400
 0% 

Cvars, Pcvars, & inc files.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-09-2014 , 12:36   Cvars, Pcvars, & inc files.
Reply With Quote #1

Hello all.

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 View Post
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


I had requested a respawn plugin few days ago, that plugin had this code

PHP Code:
ExecuteHamB(Ham_CS_RoundRespawnid
But in inc file, its like this

PHP Code:
native ExecuteHamB(Ham:function, thisany:...); 
So I did not get this. What does "Ham:function" "this" "any" means?
Also same with default include files

Thanks.
__________________
EthicalHacker007 is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 06-09-2014 , 12:42   Re: Cvars, Pcvars, & inc files.
Reply With Quote #2

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.

https://forums.alliedmods.net/showthread.php?t=215635

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.

http://www.amxmodx.org/funcwiki.php?go=module&id=24
__________________

Last edited by ddhoward; 06-09-2014 at 12:44.
ddhoward is offline
Old 06-09-2014, 12:44
fysiks
This message has been deleted by fysiks.
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-09-2014 , 14:35   Re: Cvars, Pcvars, & inc files.
Reply With Quote #3

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.
__________________

Last edited by Black Rose; 06-09-2014 at 14:39.
Black Rose is offline
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-10-2014 , 04:09   Re: Cvars, Pcvars, & inc files.
Reply With Quote #4

Quote:
Originally Posted by ddhoward View Post
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.

https://forums.alliedmods.net/showthread.php?t=215635
Thank You. Nice explanation skills.

Quote:
Originally Posted by ddhoward View Post
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.

http://www.amxmodx.org/funcwiki.php?go=module&id=24
Just checked ham_const.inc, it's same better than funcwiki. Thank you.


Quote:
Originally Posted by Black Rose View Post
Ham: and any: are tags. Much like Array:, Regex:, Float:, bool: and so on.
Hmmm.

Quote:
Originally Posted by Black Rose View Post
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 View Post
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 View Post
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 View Post
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 View Post
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 View Post
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.
__________________
EthicalHacker007 is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-10-2014 , 17:24   Re: Cvars, Pcvars, & inc files.
Reply With Quote #5

Quote:
Originally Posted by EthicalHacker007 View Post
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 
0g_cvar_count; ++i) {
      if (
equal(nameg_cvar_list[i]))
         return 
g_cvar_values[i]; 
   }

   return 
0;
}

get_pcvar_num(pointer) {
   return 
g_cvar_values[pointer];

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 View Post
PHP Code:
native ExecuteHamB(Ham:function, thisany:...); 
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 View Post
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:

PHP Code:
enum Ham {
    
Ham_Spawn 0,
    
Ham_Precache,
    
Ham_Keyvalue,
// ...

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.

PHP Code:
native ArrayPushCell(Array:whichinput);

function() {
   
ArrayPushCell(g_array1); // okay
   
ArrayPushCell(g_array1.0); // tag mismatch

PHP Code:
native ArrayPushCell(Array:whichFloat:input);

function() {
   
ArrayPushCell(g_array1); // tag mismatch
   
ArrayPushCell(g_array1.0); // okay

PHP Code:
native ArrayPushCell(Array:whichany:input);

function() {
   
ArrayPushCell(g_array1); // okay
   
ArrayPushCell(g_array1.0); // okay

Quote:
Originally Posted by EthicalHacker007 View Post
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:

PHP Code:
native Float:operator+(Float:oper1Float:oper2) = floatadd;
stock Float:operator+(Float:oper1oper2)
    return 
floatadd(oper1float(oper2)); 
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:fractionFloat:fraction2number;
fraction fraction fraction2;
fraction fraction2 number
PHP Code:
// ... will become this
new Float:floatFloat:float2int;
fraction floatadd(fractionfraction2);
fraction floatadd(fractionfloat(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 View Post
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).
__________________
In Flames we trust!

Last edited by Nextra; 06-10-2014 at 17:29.
Nextra is offline
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-11-2014 , 01:11   Re: Cvars, Pcvars, & inc files.
Reply With Quote #6

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.
__________________
EthicalHacker007 is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 09:45.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode