Raised This Month: $ Target: $400
 0% 

difference between new and static?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-25-2012 , 05:13   difference between new and static?
Reply With Quote #1

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!
.Dare Devil. is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 04-25-2012 , 05:21   Re: difference between new and static?
Reply With Quote #2

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.

__________________

Last edited by claudiuhks; 04-25-2012 at 05:29.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-25-2012 , 05:52   Re: difference between new and static?
Reply With Quote #3

Quote:
Originally Posted by claudiuhks View Post
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

Anyway thanks for that information.
.Dare Devil. is offline
GordonFreeman (RU)
Veteran Member
Join Date: Jan 2010
Location: Uzbekistan
Old 04-25-2012 , 05:57   Re: difference between new and static?
Reply With Quote #4

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
__________________
The functional way is the right way
GordonFreeman (RU) is offline
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-25-2012 , 06:10   Re: difference between new and static?
Reply With Quote #5

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! )
if the entity_think param is different then what happen to the y?
.Dare Devil. is offline
rak
Veteran Member
Join Date: Oct 2011
Location: banned country
Old 04-25-2012 , 06:55   Re: difference between new and static?
Reply With Quote #6

Quote:
Originally Posted by .Dare Devil. View Post
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! )
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
__________________

www.amxmodx-es.com

Steam: Luchokoldo
rak is offline
Send a message via MSN to rak Send a message via Skype™ to rak
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 04-25-2012 , 07:22   Re: difference between new and static?
Reply With Quote #7

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!

Last edited by .Dare Devil.; 04-25-2012 at 07:23.
.Dare Devil. is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-25-2012 , 07:36   Re: difference between new and static?
Reply With Quote #8

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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Carlosx
New Member
Join Date: Feb 2012
Location: n/a
Old 04-25-2012 , 08:07   Re: difference between new and static?
Reply With Quote #9

for short 'static' variable operating speeds of the plugin .
Carlosx 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 07:47.


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