AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   register_plugin, defines and constants. (https://forums.alliedmods.net/showthread.php?t=241862)

EthicalHacker007 06-10-2014 07:02

register_plugin, defines and constants.
 
I wanted to know that why do most of the coders use defines for registering plugin.

Plugin can be registered using
PHP Code:

register_plugin"My plugin name" "My plugin version" "My Name" 

So why do most of them use

PHP Code:

#define PLUGIN "My plugin name"
#define VERSION "My plugin version" 
#define AUTHOR "My Name" 

register_plugin PLUGIN VERSION AUTHOR 

And rarely, some use constants like

PHP Code:

new const[VERSION] = "My plugin version" //This might be wrong. 

My question is which is best?

1, 2 or 3? In wiki, they have said that constants are better than defines.

YamiKaitou 06-10-2014 07:06

Re: register_plugin, defines and constants.
 
1 & 2 are identical to the compiler.
2 is used for readability.
3 is used when you use the value more than once, typically used with Version

EthicalHacker007 06-10-2014 07:39

Re: register_plugin, defines and constants.
 
Sir. What do you mean by more than once? Which one do you prefer?

Freezo Begin 06-10-2014 07:47

Re: register_plugin, defines and constants.
 
i prefer the 1 .

DWIGHTpN 06-10-2014 08:08

Re: register_plugin, defines and constants.
 
PHP Code:

#define PLUGIN "My plugin name" 
#define VERSION "My plugin version"  
#define AUTHOR "My Name"  

register_plugin PLUGIN VERSION AUTHOR 

They disappear after compilation and values are replaced automated.
Search page with pre-processor here : http://www.compuphase.com/pawn/Pawn_Language_Guide.pdf

PHP Code:

3 is used when you use the value more than oncetypically used with Version 

More than once like using in chat message (ex: client_print(id, print_chat, "This server using plugin x version %s",VERSION), or something, i think.

EthicalHacker007 06-10-2014 08:44

Re: register_plugin, defines and constants.
 
Thanks DWIGHTpN. But I'm not going to read that long guide! hehe.

Backstabnoob 06-10-2014 10:16

Re: register_plugin, defines and constants.
 
Defines are replaced on compile time, so the string gets copied wherever you use it. Constants are initialized on plugin startup and the string is stored in memory with a pointer, which will be a better performer if you use it more than once as it won't have to be reinitialized.

EthicalHacker007 06-10-2014 10:47

Re: register_plugin, defines and constants.
 
Quote:

Originally Posted by Backstabnoob (Post 2149345)
Defines are replaced on compile time, so the string gets copied wherever you use it. Constants are initialized on plugin startup and the string is stored in memory with a pointer, which will be a better performer if you use it more than once as it won't have to be reinitialized.

That means if I use constants for "PLUGIN", "VERSION", "AUTHOR", even if I use it 1 time in entire plugin. It's better than define?

Black Rose 06-10-2014 11:42

Re: register_plugin, defines and constants.
 
I don't think making a variable "const" will have an effect on memory reservation. If you're talking about global variables, then no. If you only use it once it's better to use the defines or just the plain strings since they are released from the memory when the public function ends.

Although I'm no expert, so I might be wrong here.

Nextra 06-10-2014 16:39

Re: register_plugin, defines and constants.
 
Defines are replaced by the compiler's preprocessor before compilation. So example 1 and 2 are *exactly* the same in the end. Using defines you can get that info to the top of the file for convenience, and update multiple occurances of the same string with ease.

Using a global const variable is helpful because the AMXX compiler is not clever enough to collapse usages of the same string literal into one. So if you write "1.0.0" as your version string ten times in your plugin (or use an equivalent define) it will actually end up ten times in the file.


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

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