what is "static"? (question)
#1

i see the word or expression "static" in pawn and many programming languages (like c++ or something)
and i wonder what is it?
i see it used as a variable
like "static variableName"

can someone explain what is this?
what does it do?
what's its difference from declaring a variable with "new"? what's the difference between "static" and "new"
and when should it be used
and can u give me examples?

i searched but i didn't understand



Thanks in advance !!!
Reply
#2

Depending on where the variable is declared, the meaning of static changes.

In Pawn, if a variable is declared static in the global space then it functions exactly like a normal ("new") global variable, with the key exception that it is only visible in the file that it was declared in. This is useful for includes as the main gamemode file won't be able to access it, and other (include) files can use the same variable name without conflict.

If a variable is declared static within the local scope then it will be kept in memory and it will retain its value between function calls. Normal variables are immediately disposed of when the function ends. In that sense, a static local is useful when you require a global that is only used within that one function.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Depending on where the variable is declared, the meaning of static changes.

In Pawn, if a variable is declared static in the global space then it functions exactly like a normal ("new") global variable, with the key exception that it is only visible in the file that it was declared in. This is useful for includes as the main gamemode file won't be able to access it, and other (include) files can use the same variable name without conflict.

If a variable is declared static within the local scope then it will be kept in memory and it will retain its value between function calls. Normal variables are immediately disposed of when the function ends. In that sense, a static local is useful when you require a global that is only used within that one function.
Ahh, ok for example about static inside functions i do this:

Код:
stock function()
{
     static a =50;
     a++;
     print("%d", a);
}

function();
function();
it will add 1 to 50 and it becomes 51 and the number 51 will be kept as the new int for the value, right?

see i used "function();" two times, that means the first time it will return 50 + 1 = 51 and then, it will return the second time 52 because it kept "51" and it added 1 after
am i right?

and one little question, can we use static for strings?
thanks in advance
Reply
#4

Quote:
Originally Posted by okaym8
Посмотреть сообщение
Ahh, ok for example about static inside functions i do this:

Код:
stock function()
{
     static a =50;
     a++;
     print("%d", a);
}

function();
function();
it will add 1 to 50 and it becomes 51 and the number 51 will be kept as the new int for the value, right?

see i used "function();" two times, that means the first time it will return 50 + 1 = 51 and then, it will return the second time 52 because it kept "51" and it added 1 after
am i right?

and one little question, can we use static for strings?
thanks in advance
Yes you are right about the first question
And second yes you can use static for each type of variable you want to use BUT you have to be sure that the VARIABLE you want to change is the same type as the static variable(If i am not wrong!)
Reply
#5

Thank u for all help guys, rep for both
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)