AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   new VS static (https://forums.alliedmods.net/showthread.php?t=154229)

PRoSToTeM@ 04-04-2011 15:09

new VS static
 
I would like to know whether it is worth to use static instead of new? In what situations?

fysiks 04-04-2011 16:03

Re: new VS static
 
http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global

Emp` 04-04-2011 16:36

Re: new VS static
 
If it isn't being called more than 1 time per second the entire time the server is running, just use new.

georgik57 04-04-2011 16:43

Re: new VS static
 
hmm i was interested to find this out too
thank you

katna 04-05-2011 06:00

Re: new VS static
 
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

hornet 04-05-2011 09:46

Re: new VS static
 
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 ?

Exolent[jNr] 04-05-2011 17:13

Re: new VS static
 
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().


All times are GMT -4. The time now is 14:34.

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