SA-MP Forums Archive
What's the difference between static and new? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: What's the difference between static and new? (/showthread.php?tid=582399)



What's the difference between static and new? - Sjn - 20.07.2015

As the title says, anyone can explain me?


Re: What's the difference between static and new? - xVIP3Rx - 20.07.2015

read this, explains it all


Re: What's the difference between static and new? - Threshold - 20.07.2015

I think ****** once said:
"If you don't know the difference between 'new' and 'static', then don't use it"


AW: Re: What's the difference between static and new? - Mencent - 20.07.2015

Quote:
Originally Posted by Threshold
Посмотреть сообщение
I think ****** once said:
"If you don't know the difference between 'new' and 'static', then don't use it"
Bullshit...

If he don't know the difference between "new" and "static" he ask. So, he did it.

The difference between "static" and "new" is, that the declaration of "static" can used also in filterscripts, includes and gamemodes at the same time.. The declaration of "new" can't used in filterscripts, includes and gamemodes at the same time.


- Mencent


AW: What's the difference between static and new? - Kaliber - 20.07.2015

There are more diffrences...

static saves the pointer of an variable in the stack/heap.

For example:

PHP код:
stock func()
{
    static 
a;
    
printf("%d",++a);
}
//main()
func(); //It will print 1
func(); //It will print 2 
And if you use for example:

PHP код:
//in the include
static x;
//You can use in the main mode
new x//without problems 



Re: What's the difference between static and new? - Sjn - 20.07.2015

Oh ok, but 1 more question. I have an include called def.inc which has all of my arrays, enums, defines and other vars stored. If i create a connection handle for mysql in that include with static, it gives undefined symbol error, but if i create it with new, it works fine. Why is that?


AW: What's the difference between static and new? - Kaliber - 20.07.2015

Because of the scope of static.

Read my post...read the post in the wiki...


Re: AW: Re: What's the difference between static and new? - Threshold - 20.07.2015

Quote:
Originally Posted by Mencent
Посмотреть сообщение
Bullshit...

If he don't know the difference between "new" and "static" he ask. So, he did it.

The difference between "static" and "new" is, that the declaration of "static" can used also in filterscripts, includes and gamemodes at the same time.. The declaration of "new" can't used in filterscripts, includes and gamemodes at the same time.


- Mencent
Okay, but that's not what I said. I said if you don't know what the difference is, don't use it. Also, 'new' can be used in filterscripts, includes and gamemodes at the same time as well? How is that a 'difference'?

Static variables remember their old values, and can only be used in the script in which they're declared.

Quote:
Originally Posted by Sjn
Посмотреть сообщение
Oh ok, but 1 more question. I have an include called def.inc which has all of my arrays, enums, defines and other vars stored. If i create a connection handle for mysql in that include with static, it gives undefined symbol error, but if i create it with new, it works fine. Why is that?
Because 'static' variables only exist within the script they are declared in. So if you put it in your include file, the variables will not work outside of that include file.