[TUT] Class Specifiers (new static stock public const)

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

[TUT] Class Specifiers (new static stock public const)

Post by Archive-Poster »

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

New vs. Static

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: Select all

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: Select all

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

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: Select all

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

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: Select all

// 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

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: Select all

// 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: Select all

// 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.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

Re: [TUT] Class Specifiers (new static stock public const)

Post by Archive-Poster »

again, phenomenal work.

NOTE: you cannot define statics or constants to pointers.
nor can you define statics in such a manner....it throws a compilation error

Code: Select all

static money = cs_get_user_money(id)
This leads me to believe that most of these are only compatible with raw data, not retrieved data.

EDIT: if you set the static var on a separate call, it is accepted.

Code: Select all

static money;
money = cs_get_user_money(id)
could anyone shed some light on this anomaly?
Because you can only set to a constant, not variables.
The second line of assignment is accepted, because you can easily change static variables like new variables.
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

Re: [TUT] Class Specifiers (new static stock public const)

Post by Archive-Poster »

New vs. Static
Partially true.

AMX file contain .DATA section to store values for constants and global variables. This section filled at compile time (Thats why we cant initialize static and constant variables using runtime functions). Some example:

Code: Select all

const var1 = 0;
new var2;
static var3;

func()
{
    const var4 = 0;
    static var5;
}
//here var4 still in memory, and not deleted
At run time we can access .DATA section, we can change it (even constants using some methods), but can`t add cells (new variables) to it.

Code: Select all

func()
{
    new var1 = 5;
    new var2[2048];
} 
Here we create variables (at run time) in STACK of AMXX Virtual Machine, each time var is created it fills with 0 or constant value. Thats why var2[2048] is bad idea in frequently accessed functions. After loosing variable scope compiler clears STACK (here it totally removes from "memory"). To return array from functions compiler using another type of memory - HEAP (it`s not directly for variables, but why not to know about it ).

So, with "static" keyword we can create variables only in .DATA section of AMX file. With "new" keyword in .DATA section and STACK. For me better to use "new" keyword to define only local variables, to avoid double meaning of that keyword:

Code: Select all

#include "MyInc"

stock var1;

func()
{
    new var2;
} 
User avatar
Archive-Poster
Posts: 1367
Gender:
United States of America

Re: [TUT] Class Specifiers (new static stock public const)

Post by Archive-Poster »

This is a problem

Code: Select all

static  bool:TA =false
This is how you fix it....

Code: Select all

static bool:TA
TA = false
You need to do this because, as Exolent stated, when a static variable is called into scope again, it is just toggled back on, rather than created again. And thus, it doesn't set a new value; because it assumes the last value from its memory slot. That is why static is so much faster than using new and such.
Of course there are other ways to do the above snippet, but that was a very simplified version of what i just went through and figured I'd teach everyone with my own mistakes.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests