Raised This Month: $12 Target: $400
 3% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-07-2012 , 17:41   [TUT] Class Specifiers (new static stock public const)
Reply With Quote #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


Stocks
Spoiler


Constants
Spoiler


Private vs. Public
Spoiler


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 View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-11-2012 at 10:25.
Exolent[jNr] is offline
t3hNox
Senior Member
Join Date: Oct 2009
Old 06-07-2012 , 17:49   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #2

FINALLY.
It is actually a basic stuff, but it is not explained anywhere other than Pawn manual.

Thank you for this tutorial.
t3hNox is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-07-2012 , 18:24   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #3

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:
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:
static money;
money = cs_get_user_money(id)
could anyone shed some light on this anomaly?
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-07-2012 at 21:22.
Liverwiz is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-08-2012 , 01:02   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #4

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Aooka
Veteran Member
Join Date: Aug 2011
Location: Villeurbanne
Old 06-08-2012 , 02:21   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #5

Awesome !

Thanks to you, I have learned many things
__________________
Pawn ? Useless
Aooka is offline
Lt.RAT
Member
Join Date: Sep 2008
Location: Russia Yekaterinburg
Old 06-09-2012 , 13:49   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #6

Quote:
Originally Posted by Exolent[jNr] View Post
New vs. Static
You should only be using these for variables.

New
  • Creates a new variable in memory every time it is declared
  • At the end of this variable's scope, it is deleted from memory and no longer accessible

Static
  • At the end of this variable's scope, it is deleted from memory and no longer accessible
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:
PHP Code:
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.

PHP Code:
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:
PHP Code:
#include "MyInc"

stock var1;

func()
{
    new 
var2;

Lt.RAT is offline
Send a message via ICQ to Lt.RAT
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 06-18-2012 , 18:44   Re: [TUT] Class Specifiers (new static stock public const)
Reply With Quote #7

It is also important to note that if you set the value of a static variable in the definition it will ignore that set and just take what it was at last call. As shown in the above example.

Here's some code to show you. (one that took me a while to figure out why it was broken)
The highlighted line is the problem

Code:
public tkExample(victimID, inflictor, attackerID, Float:flDamage, iDmgBits) {         if( ! (inflictor == attackerID         && (1 <= attackerID <= gMaxPlayers)         && attackerID != victimID         && g_bknuckles[attackerID]         && is_user_alive(victimID) )         return HAM_IGNORED
    static  bool:TA = false
    if(cs_get_user_team(victimID) == cs_get_user_team(attackerID) )         TA = true     (TA) ? client_print(victimID, print_chat, "Just a teammate....") : client_print(victimID, print_chat, "RUN!")     return HAM_HANDLED }
That code, after a team attack, will ALWAYS return Just a teammate... and, depending how you implement your TeamAttack code can cause many problems.

This is how you fix it....

Code:
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 [i]very[/t] simplified version of what i just went through and figured I'd teach everyone with my own mistakes.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 06-18-2012 at 18:45.
Liverwiz is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:34.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode