Author
|
Message
|
Veteran Member
Join Date: Feb 2007
Location: Tennessee
|
![Old](images/statusicon/post_old.svg)
06-07-2012
, 17:41
[TUT] Class Specifiers (new static stock public const)
|
#1
|
Class Specifiers
and when to use them
Introduction
This tutorial assumes you know the basics about Pawn programming, which include (but not limited to):- scopes (no, not the zoom on a weapon)
- general syntax
- what variables and functions are
Need help understanding some of those?
Please read some tutorials under the "Pawn Programming" section in this post:
https://forums.alliedmods.net/showth...awnProgramming
New vs. Static
Spoiler
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 scope
if(i == 0) {
// Created in memory, initialized to 5
new 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 time
static var;
// Add 5 to variable
var += 5;
// Return variable
return 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() {
return 1;
}
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 value
const PI = 3.14;
// Throws an error because constants cannot change
PI = 0.0;
// Throws an error because it isn't assigned to a value
const INVALID;
// Creates a constant array
// Array constants must be prefixed with: new, static, or stock
new const ARRAY[] = {1, 2, 3};
some_function() {
// Create the constant one time in memory
static const MAX_USES = 3;
// Create a variable on time in memory, defaults to 0
static uses;
// Check if increased too many times
if(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 variable
public my_public_global_variable = 1;
// Create a private variable
new my_private_global_variable = 2;
// Create a public function
public 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 allowed
public valid_header(variable1, const variable2, bool:boolean3, myArray[], &someByReference, ...) {
// Even dynamic parameter list is allowed
}
// Invalid function header
public 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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw
I love you exolent!
|
Last edited by Exolent[jNr]; 06-11-2012 at 10:25.
|
|
|
|