You should only be using these for variables.
These are unnecessary for using functions.
New
Creates a new variable in memory every time it is declared
Every declaration, the initialization is set
At the end of this variable's scope, it is deleted from memory and no longer accessible
New Example:
Code:
new i; // Created in memory, initialized to 0// Just to create a deeper scopeif(i ==0){// Created in memory, initialized to 5new j =5;
}// Outside of that scope, so j is deleted from memory
Static
Creates a variable in memory the first time it is declared
The first declaration sets the initialization
Every subsequent declaration, the variable will hold whatever its last value was
At the end of this variable's scope, it is not deleted from memory but still no longer accessible
Think of static like a global variable, except it is only accessible in its declared scope
Static Example:
Code:
get_var(){// Create a variable in memory once, initialized to 0 first timestatic var;
// Add 5 to variable
var +=5;
// Return variablereturn var;
}
some_function(){// Create a new variable in memory and assign to get_var()new var = get_var(); // var = 5 (0 + 5) = 5// After that call to get_var(), the static variable inside get_var()// will still equal 5 for the next time it is used
var = get_var();
// Now var = 10, because (5 + 5) = 10// This is because the static variable did not reset to 0.// The static variable kept its old value that we last set}
Stocks
Spoiler
When compiling your plugins, the compiler checks for any unused variables and functions.
If it finds any, then it throws a warning to you.
Stocks are used for variables and functions that aren't required to be used in the plugin.
If a variable or function is declared as stock, then the compiler doesn't care if it is used or not.
Stocks should only be used in include files because you won't necessarily use everything from an include.
In your own plugin code, you should not declare anything as stock because you should only have code that is being used.
Examples:
Code:
stock my_global;
stock my_function(){return1;
}
If you include those in your plugin but don't use them, it will be as if they don't even exist.
Constants
Spoiler
Constants are basically variables that never change.
They have a specified name like variables, are declared and initialized, and take up space in memory.
However, constants can never be assigned a new value.
The value they are declared as will always be their value.
Constants must be assiged a value when declared.
Examples:
Code:
// Creates a constant variable with a valueconst PI =3.14;
// Throws an error because constants cannot change
PI =0.0;
// Throws an error because it isn't assigned to a valueconst INVALID;
// Creates a constant array// Array constants must be prefixed with: new, static, or stocknewconst ARRAY[]={1, 2, 3};
some_function(){// Create the constant one time in memorystaticconst MAX_USES =3;
// Create a variable on time in memory, defaults to 0static uses;
// Check if increased too many timesif(uses >= MAX_USES)return;
// Increase for next usage
uses++;
}
Private vs. Public
Spoiler
By default, all variables and functions are private.
Variables and functions can only be declared public in the global scope.
Making a variable public will let it be accessible to other plugins using XVars.
This can be useful for sharing variables among plugins.
Generally, public variables are not used, and dynamic natives are created to get/set them.
Making a function public will let it be accessible to modules and other plugins.
This can be useful for:
- hooking events, forwards, menu key presses, commands
- handling tasks
- handling custom forwards
- calling functions from another plugin using callfuncs (tutorial coming soon)
Examples:
Code:
// Create a public variablepublic my_public_global_variable =1;
// Create a private variablenew my_private_global_variable =2;
// Create a public functionpublic my_public_function(){}// Create a private function
my_private_function(){}
In public functions, the parameter list cannot have default values.
Examples:
Code:
// Valid header of what is allowedpublic valid_header(variable1, const variable2, bool:boolean3, myArray[], &someByReference, ...){// Even dynamic parameter list is allowed}// Invalid function headerpublic invalid_header(var =0){// Cannot have default values}
Combining them, the most you would need to do is:
- new const
- static const
- public const
- stock const
- stock static const
- stock public const
There's a lot more combinations you could do, but that's all that would be even close to necessary.