SA-MP Forums Archive
Scope of variables in PAWN? - 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: Scope of variables in PAWN? (/showthread.php?tid=306961)



Scope of variables in PAWN? - T0pAz - 28.12.2011

Does PAWN has variables scope? For example Local Variables and Global Variables.


Re: Scope of variables in PAWN? - CaHbKo - 28.12.2011

Yes.

pawn Код:
new global_var;

function()
{
    new local_var;
    if(true)
    {
        new even_more_local_var;
    }
}



Re: Scope of variables in PAWN? - randomkid88 - 28.12.2011

Yes. Variables declared outside of a callback are considered Global (all functions/callbacks can access them). Anything declared inside a function is local, and only usable by that function.


Re: Scope of variables in PAWN? - T0pAz - 28.12.2011

What is it's memory allocation? For example if I do

pawn Код:
//At Top
new A;

//At OnGameModeInit CB
A = 2;

//At OnPlayerSpawn CB
print(A); // Is the variable's A value still 2?
Edit: When will the memory get destroyed?


Re: Scope of variables in PAWN? - Calgon - 28.12.2011

The value will remain the same regardless of which scope you change it in, as long as it's a global variable.

pawn Код:
new var;

public OnGameModeInit() {
   var = 2;
}

//var is always 2 unless changed now



Re: Scope of variables in PAWN? - Tannz0rz - 28.12.2011

Quote:
Originally Posted by T0pAz
Посмотреть сообщение
What is it's memory allocation? For example if I do

pawn Код:
//At Top
new A;

//At OnGameModeInit CB
A = 2;

//At OnPlayerSpawn CB
print(A); // Is the variable's A value still 2?
Edit: When will the memory get destroyed?
I don't know for sure, but I'd imagine when the program exits.


Re: Scope of variables in PAWN? - T0pAz - 28.12.2011

Quote:
Originally Posted by Calgon
Посмотреть сообщение
The value will remain the same regardless of which scope you change it in, as long as it's a global variable.

pawn Код:
new var;

public OnGameModeInit() {
   var = 2;
}

//var is always 2 unless changed now
Last question. Is there a way to know the memory address of a specific variable? Like using pointers or anything.


Re: Scope of variables in PAWN? - Calgon - 28.12.2011

Probably via amx_GetAddr and amx_GetString, in C++ with the Pawn/SA-MP libraries.


Re: Scope of variables in PAWN? - T0pAz - 28.12.2011

Quote:
Originally Posted by Calgon
Посмотреть сообщение
Probably via amx_GetAddr and amx_GetString, in C++ with the Pawn/SA-MP libraries.
Anyway to use it on PAWN?

Edit: I need it to prevent hacking.


Re: Scope of variables in PAWN? - Ash. - 28.12.2011

Quote:
Originally Posted by T0pAz
Посмотреть сообщение
Anyway to use it on PAWN?

Edit: I need it to prevent hacking.
Nope.

+ How on earth are you planning on doing that just by getting the memory addresses?