AlliedModders

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

Emp0wer 08-23-2010 15:00

global new or static?
 
What is better?

PHP Code:

new g_classname[32]

public 
fw_Think(entity)
{
    
pev(entity,pev_classname,g_classname,31)


PHP Code:

public fw_Think(entity)
{
    new 
classname[32]
    
pev(entity,pev_classname,classname,31)


PHP Code:

public fw_Think(entity)
{
    static 
classname[32]
    
pev(entity,pev_classname,classname,31)



nikhilgupta345 08-23-2010 15:03

Re: global new or static?
 
well you gotta think abot what you are using it for. Global is used so that you can use it in more than one function. New is limited to only that function, as well as static.

http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global

Arkshine 08-23-2010 15:07

Re: global new or static?
 
For arrays used in a forward called often, you better use static. 1 is the same than 3 except the scope of the var is expanded all the functions.

Hunter-Digital 08-23-2010 16:42

Re: global new or static?
 
About that document ( http://wiki.amxmodx.org/Optimizing_P...atic_vs_Global )... I can't really understand this part:
Quote:

Note: Be careful of re-entrancy!
When a variable is static, it has the same re-entrancy problems of a global variable. That means, if your function might be called recursively, or twice in the same stack frame, you should not use static variables. This is most often the case for API provided to other plugins or helper functions. Triggered events are usually never called twice on the same execution chain.

fysiks 08-23-2010 16:56

Re: global new or static?
 
If both functions A and B use function C and C is using static variable then you might not get what expected. If A executes first then B it might not be the same as B executing first then A. The outcome of A and B might be different.

Something along those lines. It's similar to race conditions I believe.

Emp0wer 08-23-2010 17:07

Re: global new or static?
 
Ok I got it :) Thx for answers


All times are GMT -4. The time now is 22:03.

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