Raised This Month: $12 Target: $400
 3% 

Diffrence between .....


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
pokemonmaster
princess milk
Join Date: Nov 2010
Location: Somewhere in this world
Old 12-29-2011 , 16:59   Diffrence between .....
Reply With Quote #1

Hi,
I just want to know whats the difference between the
PHP Code:
#define 
And the
Code:
new
Why don't i use for example
PHP Code:
new AUTHOR[]="Pokemonmaster" 
Instead of
PHP Code:
#define AUTHOR "Pokemonmaster" 
Does the "define" command uses different includes than the "new" ?

Last edited by pokemonmaster; 12-30-2011 at 04:53.
pokemonmaster is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-29-2011 , 17:19   Re: Diffrence between .....
Reply With Quote #2

#define is a preprocessor directive which replaces wherever you have that "name" into the value when it compiles.

For example:
Code:
#define TEST 1 server_print("%d", TEST);
It would translate to:
Code:
server_print("%d", 1);
And then compile.

What's the difference? Well, when using a define multiple times, it is possible to have issues.
For example:
Code:
#define TAG "[AMXX]" // ... server_print("%s WOOO", TAG); log_amx("%s Something logged.", TAG);
That translates to:
Code:
server_print("%s WOOO", "[AMXX]"); log_amx("%s Something logged.", "[AMXX]");
The problem here is that you wasted 2 memory spaces with the same string because each "[AMXX]" is treated as its own string.
You could have saved memory space if you had used a variable.

To learn more about the preprocessor or AMXX scripting in general, check out the Code Snippets / Tutorials section.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 12-29-2011 , 17:26   Re: Diffrence between .....
Reply With Quote #3

In the event that you use something like this what is the benfit from doing it these 2 ways kinda on the same note

Code:
const MAX_PLAYERS 32
new Array[MAX_PLAYERS+1];
vs
Code:
#define MAX_PLAYERS 32
new Array[MAX_PLAYERS+1];
vs
Code:
new Array[33];
Doc-Holiday is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-29-2011 , 17:32   Re: Diffrence between .....
Reply With Quote #4

I would think that if you use the variable more than once then the first is the more efficient one. The second and third are identical except that the processor has to perform the addition.
__________________

Last edited by fysiks; 12-29-2011 at 17:33.
fysiks is offline
Doc-Holiday
AlliedModders Donor
Join Date: Jul 2007
Old 12-29-2011 , 17:33   Re: Diffrence between .....
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
I would think that if you use the variable more than once then the first is the more efficient one. The second and third are identical.
Thanks.
Doc-Holiday is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 12-29-2011 , 19:30   Re: Diffrence between .....
Reply With Quote #6

all three cases will be calculated during compiling time. the benifit is:
1 and 2 is adjustable in the future developing. imagine you have a large project which uses that number a lot(usually not in the same file). some day you want to change it to larger or smaller. you don't have to trace it everywhere, just change the define or declaration.
The different between 1 and 2 is that, 1 can do error check when compiling, because it's a varible, it takes memory space. 2 just do the mechanic replacement in pre-compiling time.
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
pokemonmaster
princess milk
Join Date: Nov 2010
Location: Somewhere in this world
Old 12-30-2011 , 04:49   Re: Diffrence between .....
Reply With Quote #7

OK thanks all for your help,
but one last question, which one is better "define" or "new"?
pokemonmaster is offline
MyPc
Senior Member
Join Date: Sep 2011
Old 12-30-2011 , 04:53   Re: Diffrence between .....
Reply With Quote #8

Quote:
Originally Posted by pokemonmaster View Post
OK thanks all for your help,
but one last question, which one is better "define" or "new"?
Read the comments again and find out
MyPc is offline
pokemonmaster
princess milk
Join Date: Nov 2010
Location: Somewhere in this world
Old 12-30-2011 , 14:44   Re: Diffrence between .....
Reply With Quote #9

Quote:
Originally Posted by MyPc View Post
Read the comments again and find out

I read all of them but Exolent's comment was the best one i could understand, as i started learning to make scripts, i don't know what most of the functions in other comments do.

But thanks anyway

And one last thing, is there any tutorial do you recommend a beginner that doesn't know anything about scripting, you recommend to start with?

Last edited by pokemonmaster; 12-30-2011 at 15:19.
pokemonmaster is offline
matsi
Thinkosaur
Join Date: Sep 2006
Old 12-30-2011 , 22:03   Re: Diffrence between .....
Reply With Quote #10

Quote:
Originally Posted by pokemonmaster View Post
I read all of them but Exolent's comment was the best one i could understand, as i started learning to make scripts, i don't know what most of the functions in other comments do.

But thanks anyway

And one last thing, is there any tutorial do you recommend a beginner that doesn't know anything about scripting, you recommend to start with?
I think there is tutorial to find that beginner tutorial.
matsi 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 03:45.


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