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

[TUTORIAL] "Structs" in pawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Greenberet
AMX Mod X Beta Tester
Join Date: Apr 2004
Location: Vienna
Old 04-06-2006 , 04:23   [TUTORIAL] "Structs" in pawn
Reply With Quote #1

Code:
/* *   This tutorial will show you how to use "structs" aka "incredible hacked enums" (named by Bailopan ) *   in pawn. *    *   Tutorial by core|Greenberet *           ICQ: 169799352 *           MSN: <a href="mailto:[email protected]">[email protected]</a> *           EMAIL: <a href="mailto:[email protected]">[email protected]</a> * * * *   Chapter 1: Simple structs         Line: 20 *   Chapter 2: Floats and other tags      Line: 52 *   Chapter 3: Strings          Line: 85 */ /********************************************************************************************/ /******************************** CHAPTER 1: Simple structs *********************************/ /********************************************************************************************/ // First we need an enum. // This enum will be our struct table // First we will use an simple one with only cells enum EPlayer {     chFrags, // the frags of the player     chDeaths, // the deaths of the player }; // Now we need a variable that holds the player info new PlayerStats[ 33 ][ EPlayer ]; // You can write into the array like this: PlayerStats[ id ][ chFrags ] = get_user_frags( id ); PlayerStats[ id ][ chDeaths ] = get_user_deaths( id ); //you can read the values in the same way. new Frags = PlayerStats[ id ][ chFrags ]; new Deaths = PlayerStats[ id ][ chDeaths ]; // now let us print the stats on the screen client_print( id, print_chat, "Frags %i", PlayerStats[ id ][ chFrags ] ); client_print( id, print_chat, "Deaths %i", PlayerStats[ id ][ chDeaths ] ); /********************************************************************************************/ /***************************** CHAPTER 2: Floats and other tags *****************************/ /********************************************************************************************/ // First of all we have to upgade the enum // We want to save the speed of the client enum EPlayer {     chFrags,     chDeaths,     chSpeed, // This is our new Speed field }; // We don't have to upgrade the PlayerStats array since it is growing & shrinking automatically // if we change something in the enum // Now let us write the speed into the array. We have to cast the Float tag to _ // If we don't do this, we would get an Tag missmatch warning PlayerStats[ id ][ chSpeed ] = _:entity_get_float( id, EV_FL_speed ); // If you want to read the speed you have to cast it back to Float entity_set_float( id, EV_FL_speed, Float:PlayerStats[ id ][ chSpeed ] ); // With all the other tags it is working in the same way. // print the speed on the screen client_print( id, print_chat, "Speed %f", Float:PlayerStats[ id ][ chSpeed ] ); /********************************************************************************************/ /********************************* CHAPTER 3: Strings ***************************************/ /********************************************************************************************/ // We have to upgrade the enum again XD // Now we are storing the Name of the player in the array enum EPlayer {     chFrags,     chDeaths,     chSpeed,     chName[33], // This is a array with the len of 33, this should be enough for an name }; // Well you don't have to cast if you want to write&read the array // but we have to use all the string functions // Now let us write the name of the player into the struct copy( PlayerStats[ id ][ chName ], 32, "core|Greenberet" ); //or get_user_info( id, "Name", PlayerStats[ id ][ chName ], 32 ); // If we want to read it we have to use it in the same way: new Name[ 33 ]; copy( Name, 32, PlayerStats[ id ][ chName ] ); // Print it on the screen client_print( id, print_chat, "Your name is %s", PlayerStats[ id ][ chName ] );
Greenberet is offline
Send a message via ICQ to Greenberet Send a message via MSN to Greenberet
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 04-06-2006 , 09:42  
Reply With Quote #2

Nice, too bad there's no OOP support in Pawn. I wonder how hard it would be to implement classes...
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 04-06-2006 , 10:40  
Reply With Quote #3

Interesting.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Zenith77
Veteran Member
Join Date: Aug 2005
Old 04-06-2006 , 16:08  
Reply With Quote #4

Quote:
Originally Posted by Hawk552
Nice, too bad there's no OOP support in Pawn. I wonder how hard it would be to implement classes...
That would basically be turning pawn into C++, with just a few minor differences
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
capndurk
Senior Member
Join Date: Feb 2006
Old 04-06-2006 , 16:18  
Reply With Quote #5

That's pretty cool, thanks for writing up that tutorial, Greenberet.
capndurk is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 04-06-2006 , 16:33  
Reply With Quote #6

Quote:
Originally Posted by Zenith77
Quote:
Originally Posted by Hawk552
Nice, too bad there's no OOP support in Pawn. I wonder how hard it would be to implement classes...
That would basically be turning pawn into C++, with just a few minor differences
What's wrong with that?
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 04-06-2006 , 17:00  
Reply With Quote #7

Quote:
Originally Posted by Zenith77
Quote:
Originally Posted by Hawk552
Nice, too bad there's no OOP support in Pawn. I wonder how hard it would be to implement classes...
That would basically be turning pawn into C++, with just a few minor differences
You'd have to do a lot of turning to turn it into something similar to C++

C++ has real types, C++ has pointers and references, C++ has classes, C++ has inheritance, C++ has virtual functions, C++ has operator overloading, C++ has templates, C++ has exceptions, C++ has a standard library, C++ has a more powerful preprocessor, and I probably forgot some more

On a side note, I think you can tag "members" of "enum structs" in pawn with tags (like Float like I did in my cvar rules plugin; at least it was possible when I had written it.
__________________
hello, i am pm
PM is offline
MaximusBrood
Veteran Member
Join Date: Sep 2005
Location: The Netherlands
Old 04-06-2006 , 17:56  
Reply With Quote #8

Quote:
Originally Posted by PM
C++ has real types, C++ has pointers and references, C++ has classes, C++ has inheritance, C++ has virtual functions, C++ has operator overloading, C++ has templates, C++ has exceptions, C++ has a standard library, C++ has a more powerful preprocessor, and I probably forgot some more
You can say that the power of Pawn is the absence of all these functions
__________________
Released six formerly private plugins. Not active here since ages.
MaximusBrood is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-06-2006 , 19:04  
Reply With Quote #9

There is an OOP module coming out eventually, that will simulate OOP in PAWN.
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
tlc660
Junior Member
Join Date: Jan 2007
Old 01-27-2007 , 07:53   Re: [TUTORIAL] "Structs" in pawn
Reply With Quote #10

how to get all members?
i have try this, but not work

Code:
for(new i = 0; i < EPlayer; i++){
    PlayerStats[id][i]
}
tlc660 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 04:56.


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