AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved When should i use "static" over "new" variable? (https://forums.alliedmods.net/showthread.php?t=305760)

Syturi0 03-03-2018 08:59

When should i use "static" over "new" variable?
 
In what situations should i use "static" over "new"? I dont get it.
Only when i dont need to empty/zero it first before using?

HamletEagle 03-03-2018 09:06

Re: When should i use "static" over "new" variable?
 
Just search, there are literally 100 tutorials and questions about this.

eyal282 03-04-2018 07:25

Re: When should i use "static" over "new" variable?
 
new gets deleted in the end of the function, static is saved in the end of the function.

Syturi0 03-04-2018 07:35

Re: When should i use "static" over "new" variable?
 
Quote:

Originally Posted by eyal282 (Post 2581290)
new gets deleted in the end of the function, static is saved in the end of the function.

I knew that already.
But i still dont get it why would i use "new" over "static" and vice versa.
If "static" is faster and does the same job as "new", why dont i use "static" all the times?
Examples would be nice.

Syturi0 03-04-2018 07:35

Re: When should i use "static" over "new" variable?
 
Quote:

Originally Posted by HamletEagle (Post 2581136)
Just search, there are literally 100 tutorials and questions about this.

And not a single one explains it properly.

klippy 03-04-2018 07:46

Re: When should i use "static" over "new" variable?
 
Use static for large arrays (at least like 500 or 1000 elements) unless the function declaring them is called rarely, or if you want a variable to preserve its value between function calls.
If at any time you spend at least a few seconds thinking whether you should go with static or new, just go with new, it won't matter. People are making a fuss about this as if the difference is noticeable, and also I've seen people slamming static over almost everything in their code which is just dumb.

Syturi0 03-04-2018 07:49

Re: When should i use "static" over "new" variable?
 
Quote:

Originally Posted by KliPPy (Post 2581295)
Use static for large arrays (at least like 500 or 1000 elements) unless the function declaring them is called rarely, or if you want a variable to preserve its value between function calls.
If at any time you spend at least a few seconds thinking whether you should go with static or new, just go with new, it won't matter. People are making a fuss about this as if the difference is noticeable, and also I've seen people slamming static over almost everything in their code which is just dumb.

Finally a GOOD answer.
Thank you.

E1_531G 03-04-2018 08:23

Re: When should i use "static" over "new" variable?
 
For example, we have client_prethink() which is executed every client frame.
If inside the function you use new var, you will have:
1. create var
2. use var
3. delete var (on func end)
If you have static var instead:
1. create var (only first call)
2. use var
(never delete on func end)

klippy 03-04-2018 09:15

Re: When should i use "static" over "new" variable?
 
Quote:

Originally Posted by E1_531G (Post 2581306)
For example, we have client_prethink() which is executed every client frame.
If inside the function you use new var, you will have:
1. create var
2. use var
3. delete var (on func end)
If you have static var instead:
1. create var (only first call)
2. use var
(never delete on func end)

It doesn't matter.

HamletEagle 03-04-2018 13:09

Re: When should i use "static" over "new" variable?
 
Quote:

Originally Posted by KliPPy (Post 2581308)
It doesn't matter.

Step 1: new array[1000] in prethink
Step 2: enjoy.


All times are GMT -4. The time now is 06:29.

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