Raised This Month: $ Target: $400
 0% 

new VS static


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 04-04-2011 , 15:09   new VS static
Reply With Quote #1

I would like to know whether it is worth to use static instead of new? In what situations?
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-04-2011 , 16:03   Re: new VS static
Reply With Quote #2

http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global
__________________
fysiks is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 04-04-2011 , 16:36   Re: new VS static
Reply With Quote #3

If it isn't being called more than 1 time per second the entire time the server is running, just use new.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 04-04-2011 , 16:43   Re: new VS static
Reply With Quote #4

hmm i was interested to find this out too
thank you
__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
katna
Senior Member
Join Date: May 2010
Old 04-05-2011 , 06:00   Re: new VS static
Reply With Quote #5

static - declared only once.
new - declared everytime the word new is called. Use more memory.

static is used in functions which get called a lot. Ex: client_prethink
katna is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 04-05-2011 , 09:46   Re: new VS static
Reply With Quote #6

So what about when hooking eg. a weapon's fire event using Ham_Weapon_PrimaryAttack, and let's say you want to find the shooters id using pev_owner .... would you be better off to make it static id = pev() or new ?
hornet is offline
Old 04-05-2011, 12:39
hleV
This message has been deleted by hleV.
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-05-2011 , 17:13   Re: new VS static
Reply With Quote #7

Static variables seem to only be useful when you are using arrays in functions that are called very often, or when you want a global-type variable with only the local scope.

First example:
Code:
public client_PreThink(id) {     new team = get_user_team(id);     if( team != 1 && team != 2 ) return;         new name[32];     new message[256], len;         new players[32], pnum;     get_players(players, pnum, "ae", (team == 1) ? "TERRORIST" : "CT");         for( new i = 0, player; i < pnum; i++ )     {         player = players[i];         if( player == id ) continue;                 get_user_name(player, name, charsmax(name));         len += formatex(message[len], charsmax(message) - len, "^n%s [%d HP]", name, get_user_health(player));     }         if( len )     {         if( team == 1 ) set_hudmessage(255, 0, 0, .x=0.1, .y=0.3, .holdtime=0.1, .channel=-1);         else            set_hudmessage(0, 127, 255, .x=0.1, .y=0.3, .holdtime=0.1, .channel=-1);         show_hudmessage(id, "Teammates:%s", message);     } }

It has to create and destroy those variables each call, and arrays make it even more expensive.

Instead, this should be done:
Code:
public client_PreThink(id) {     new team = get_user_team(id);     if( team != 1 && team != 2 ) return;         static name[32];     static message[256], len;     len = 0; // reset to 0 each prethink         static players[32], pnum;     get_players(players, pnum, "ae", (team == 1) ? "TERRORIST" : "CT");         for( new i = 0, player; i < pnum; i++ )     {         player = players[i];         if( player == id ) continue;                 get_user_name(player, name, charsmax(name));         len += formatex(message[len], charsmax(message) - len, "^n%s [%d HP]", name, get_user_health(player));     }         if( len )     {         if( team == 1 ) set_hudmessage(255, 0, 0, .x=0.1, .y=0.3, .holdtime=0.1, .channel=-1);         else            set_hudmessage(0, 127, 255, .x=0.1, .y=0.3, .holdtime=0.1, .channel=-1);         show_hudmessage(id, "Teammates:%s", message);     } }

Second example:
Code:
new g_msgid_SayText; public plugin_init() {     g_msgid_SayText = get_user_msgid("SayText"); } ColorMessage(id, const fmt[], any:...) {     new message[192];     vformat(message, charsmax(message), fmt, 3);         if( !id )     {         new players[32], pnum;         get_players(players, pnum);                 for( new i = 0; i < pnum; i++ ) SayText(players[i], message);     }     else SayText(id, message); } SayText(id, const message[]) {     message_begin(MSG_ONE_UNRELIABLE, g_msgid_SayText, _, id);     write_byte(id);     write_string(message);     message_end(); }

Could be changed to less code:

Code:
ColorMessage(id, const fmt[], any:...) {     new message[192];     vformat(message, charsmax(message), fmt, 3);         if( !id )     {         new players[32], pnum;         get_players(players, pnum);                 for( new i = 0; i < pnum; i++ ) SayText(players[i], message);     }     else SayText(id, message); } SayText(id, const message[]) {     static msgid_SayText;     if( msgid_SayText || (msgid_SayText = get_user_msgid("SayText")) )     {         message_begin(MSG_ONE_UNRELIABLE, msgid_SayText, _, id);         write_byte(id);         write_string(message);         message_end();     } }

It makes it easier to copy/paste simple functions into your own code that are useful without having to make sure you have the variables or extras added to plugin_init().
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 14:34.


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