AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   static question (https://forums.alliedmods.net/showthread.php?t=210742)

n0br41ner 03-13-2013 20:37

static question
 
Hello,

I have a small and possibly dumb question. Before i came hear i read the part where it explains about static variables in the pawn language guide, but still i couldn't find an answer to my question.

Let's say i have this line:
PHP Code:

MyFunction( ) {
    static 
iVariable 5;

    
// stuff here


In C++, AFAIK, the first time that function is called, iVariable is set to 5, but when any other time that function is called, it is not re-initialized to 5, it's current value stay the same. Let me make things clear with an example.

Let's say inside that function i changed the value of iVariable like the following:
PHP Code:

MyFunction( ) {
    static 
iVariable 5;

    
// stuff here

    
iVariable += 1;

    
// stuff here


The first time the function is called, iVariable is created and set to 5. Then iVariable becomes 6. But then at the second call of the function, iVariable is not reset to 5, but it's value remains 6, therefore after the function ended, it's value becomes 7. That is in C++.

My question is, does that scenario applies to pawn as well?

If i wasn't completely clear, please tell me and i will explain in another way if possible.

EDIT: nvm tested it and the scenario still applies. I have no idea why i didn't test it before.

Regards,
n0br41ner

Ef_Deas 03-14-2013 08:23

Re: static question
 
Yes.
PHP Code:

MyFunction() {
    static 
iVariable 5;
    
iVariable += 1;
    
console_print(0"Variable: %d\n"iVariable )


PHP Code:

while(true)
MyFunction(); 

Should display in console
Quote:

"Variable: 6
"Variable: 7
"Variable: 8
"Variable: 9
"Variable: 10
...
"Variable: 65535
...


All times are GMT -4. The time now is 21:36.

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