Raised This Month: $ Target: $400
 0% 

register_plugin, defines and constants.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-10-2014 , 07:02   register_plugin, defines and constants.
Reply With Quote #1

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.
__________________
EthicalHacker007 is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 06-10-2014 , 07:06   Re: register_plugin, defines and constants.
Reply With Quote #2

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
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-10-2014 , 07:39   Re: register_plugin, defines and constants.
Reply With Quote #3

Sir. What do you mean by more than once? Which one do you prefer?
__________________
EthicalHacker007 is offline
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 06-10-2014 , 07:47   Re: register_plugin, defines and constants.
Reply With Quote #4

i prefer the 1 .

Last edited by Freezo Begin; 06-10-2014 at 07:48.
Freezo Begin is offline
DWIGHTpN
Senior Member
Join Date: Jan 2013
Location: Romania.
Old 06-10-2014 , 08:08   Re: register_plugin, defines and constants.
Reply With Quote #5

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.

Last edited by DWIGHTpN; 06-10-2014 at 08:09.
DWIGHTpN is offline
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-10-2014 , 08:44   Re: register_plugin, defines and constants.
Reply With Quote #6

Thanks DWIGHTpN. But I'm not going to read that long guide! hehe.
__________________
EthicalHacker007 is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 06-10-2014 , 10:16   Re: register_plugin, defines and constants.
Reply With Quote #7

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.
Backstabnoob is offline
EthicalHacker007
Veteran Member
Join Date: May 2014
Old 06-10-2014 , 10:47   Re: register_plugin, defines and constants.
Reply With Quote #8

Quote:
Originally Posted by Backstabnoob View Post
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?
__________________
EthicalHacker007 is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-10-2014 , 11:42   Re: register_plugin, defines and constants.
Reply With Quote #9

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.
__________________
Black Rose is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-10-2014 , 16:39   Re: register_plugin, defines and constants.
Reply With Quote #10

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.
__________________
In Flames we trust!
Nextra 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:44.


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