AlliedModders

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

.Dare Devil. 04-25-2012 05:13

difference between new and static?
 
Hello!

i still think that they are same things so i use always new, because it is shorter :)
I dont see any difference between new and static...
what difference between new and static? :):)

Thanks!

claudiuhks 04-25-2012 05:21

Re: difference between new and static?
 
A static variable couldn't be initialized immediately, then it will take less performance.
Also, it remains unchanged in function and won't be initialized again. Only a new attribution will change the static's variable value.

PHP Code:

new 1337// YEYEYE
static 1337// NONONO
static i1337// YEYEYE 

PHP Code:

public MyFunction( )
{
  static 
msgSayText;
  if( !
msgSayText )
    
msgSayText get_user_msgid"SayText" );

  
server_print"SayText's index is %d"msgSayText );

  
// Native get_user_msgid will be executed once in this function named MyFunction because msgSayText's value won't be lost.



.Dare Devil. 04-25-2012 05:52

Re: difference between new and static?
 
Quote:

Originally Posted by claudiuhks (Post 1696069)
A static variable couldn't be initialized immediately, then it will take less performance.
Also, it remains unchanged in function and won't be initialized again. Only a new attribution will change the static's variable value.

PHP Code:

new 1337// YEYEYE
static 1337// NONONO
static i1337// YEYEYE 

PHP Code:

public MyFunction( )
{
  static 
msgSayText;
  if( !
msgSayText )
    
msgSayText get_user_msgid"SayText" );

  
server_print"SayText's index is %d"msgSayText );

  
// Native get_user_msgid will be executed once in this function named MyFunction because msgSayText's value won't be lost.



that explain somethings...
Sometimes my static variables get wrong value when i use them like "entity thinking function"
i just replaced all static to new and then it worked fine :crab:

Anyway thanks for that information.

GordonFreeman (RU) 04-25-2012 05:57

Re: difference between new and static?
 
new creating every time on function call
static creating on plugin start and using (not creating) on function call
static is more perfomancy for often calls
like shooting

.Dare Devil. 04-25-2012 06:10

Re: difference between new and static?
 
One question

PHP Code:


entity_think
(ent)
{
     static 
y
     y 
+= something



if entity_think is called "entity_think(130)" example
then y + 10 example and if entity_think is called "entity_think(129)"
then that old y + 10 or there come new y because func ent param is different?

i think it is a bad explanation :) ( bad english! :crab: )
if the entity_think param is different then what happen to the y?

rak 04-25-2012 06:55

Re: difference between new and static?
 
Quote:

Originally Posted by .Dare Devil. (Post 1696078)
One question

PHP Code:


entity_think
(ent)
{
     static 
y
     y 
+= something



if entity_think is called "entity_think(130)" example
then y + 10 example and if entity_think is called "entity_think(129)"
then that old y + 10 or there come new y because func ent param is different?

i think it is a bad explanation :) ( bad english! :crab: )
if the entity_think param is different then what happen to the y?

static never clean his value;
if you want.. make a test in Ham_Spawn using a static

.Dare Devil. 04-25-2012 07:22

Re: difference between new and static?
 
PHP Code:

#include <amxmodx>
#include <amxmisc>


public plugin_init()
{
    
register_plugin"test plugin""1.0""Dare-Devil" )
    
register_clcmd("say t""call_Testing")
}

public 
call_Testing(id)
{
    
test(12)
    
test(21)
    
test(102)
    
test(201)
}

test(testparam)
{
    static 
hj
    hj
++
    
client_print(0print_chat"hj: %d"hj)


Quote:

// result:
hj: 1
hj: 2
hj: 3
hj: 4
what I hoped
Quote:

// result:
hj: 1
hj: 1
hj: 1
hj: 1
so it seems that if function param is different the system still use same variable.
Anyway in the entity thinking i must do

PHP Code:

entitythink(ent)
{
   static 
entvariable[512]
   
entvariable[Ent] += something



this way my data will not get corrupted.
I get all information what i want, thanks!

ConnorMcLeod 04-25-2012 07:36

Re: difference between new and static?
 
Do :
static hj = 0 to be sure of initialization.

For an array, prefer to use a global array, especially if you gonna increment cells :

new g_MyGlobalEntArray[512];


You can consider a static variable like a global variable but that you can access only in 1 function.

Also, as said GordonFreeman, prefer static when your function is called very often, and especially when variables are strings.

Carlosx 04-25-2012 08:07

Re: difference between new and static?
 
for short 'static' variable operating speeds of the plugin .


All times are GMT -4. The time now is 07:47.

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