Variable before function is
global variable. It's mean that it can be used everywhere.
Variable in function is
local variable. It's mean that it can be used only in this function.
Example for global:
Код:
new gString[128];
MyFunction(playerid)
{
format(gString, sizeof gString, "ID: %d", playerid); // It can be used here
SendClientMessage(playerid, -1, gString);
return 1;
}
// But also here
MyFunction2(playerid)
{
gString[0] = EOS;
return 1;
}
For local variable:
Код:
MyFunction(playerid)
{
new gString[128];
format(gString, sizeof gString, "ID: %d", playerid);
SendClientMessage(playerid, -1, gString); // It can be used here
return 1;
}
// But not here - error: undefined symbol gString
MyFunction2(playerid)
{
gString[0] = EOS; // Undefined symbol gString
return 1;
}
If you using same variable (ex. string) everywhere, the best option is global variable.
Код:
MyFunction(playerid)
{
// I must get it here
return 1;
}
MyFunction2(playerid)
{
// And here!
return 1;
}
Up option is better than:
Код:
MyFunction(playerid)
{
new gString[128];
// Using my variable
return 1;
}
MyFunction2(playerid)
{
new gString[128]; // New variable! OMG!
// Using my variable again
return 1;
}