Difference with "static" and "new"
#1

Well, I'd like to know which one I should actually be using.

For example, i'm creating a register function, and want to declare a Query variable, would I use "static" or "new"?
Reply
#2

https://sampwiki.blast.hk/wiki/Scripting_Basics#Scope

https://sampwiki.blast.hk/wiki/Keywords:Initialisers#static
https://sampwiki.blast.hk/wiki/Keywords:Initialisers#new

https://sampforum.blast.hk/showthread.php?tid=108391
Reply
#3

afaik static is something that must stay in the part that you've coded it to.

If you created an include and added some statics variables, the gamemode cannot use those variables as they're only usable in the include.
Reply
#4

Quote:
Originally Posted by KyleSmith
Посмотреть сообщение
If you created an include and added some statics variables, the gamemode cannot use those variables as they're only usable in the include.
oh, no wonder why i can't use my variables from include. was set to static ;q
Reply
#5

Ah right, so it can be static when it's inside a stock function? I am only using the variable inside that stock function.

Also does it reduce the amx file size?
Reply
#6

Yes, it can be used inside a function and it's called static local. It also "holds" its old value.

I'm not aware of whether it reduces the size of the .amx or not.
Reply
#7

About the size -- it isn't important at all. The static keyword serves other purposes. newbienoob already mentioned one usage: keeping the variable inside its file. Keeping a variable known in only a function's scope is beneficial when working on large or confusing code, it leaves you less room for error.
Its function is that its value is kept. It is not stored on the stack and very speaking by a simplified model of memory management, it is as fast as any other global variable (which are in the data segment of some sort, on the heap).

But please do find the keyword useful.
Reply
#8

pawn Код:
main()
{
    for (;;)
    {
        new value;
        printf("old value: %d", value); // always 0
        value = random(10000);
        printf("new value: %d", value);
       
        sleep(1000);
    }
}
pawn Код:
main()
{
    for (;;)
    {
        static value;
        printf("old value: %d", value); // not lost unlike with "new"
        value = random(10000);
        printf("new value: %d", value);
       
        sleep(1000);
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)