Page 1 of 1

[TUTORIAL] "Structs" in pawn

Posted: Sat Aug 15, 2026 8:48 pm
by Archive-Poster

Code: Select all

/*
*   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:greenberet1989@hotmail.com">greenberet1989@hotmail.com</a>
*            EMAIL: <a href="mailto:greenberet@corona-bytes.net">greenberet@corona-bytes.net</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 ] );

Re: [TUTORIAL] "Structs" in pawn

Posted: Sat Aug 15, 2026 8:51 pm
by Archive-Poster
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 :D

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. :D

Re: [TUTORIAL] "Structs" in pawn

Posted: Sat Aug 15, 2026 8:55 pm
by Archive-Poster
how to get all members?
i have try this, but not work

Code: Select all

for(new i = 0; i < EPlayer; i++){
    PlayerStats[id][i]
}

Re: [TUTORIAL] "Structs" in pawn

Posted: Sat Aug 15, 2026 8:56 pm
by Archive-Poster
First:dont use enums ,they can make many problem.

You must use it so in a loop.

Code: Select all

for(new i = 0; EPlayer:i < EPlayer; EPlayer:i++){
        PlayerStats[id][EPlayer:i] = 1//the 1 was only for example.
    }

Re: [TUTORIAL] "Structs" in pawn

Posted: Sat Aug 15, 2026 8:56 pm
by Archive-Poster
enum have no check how long the array is,if you acces over the arraylenght can give problems.
Pawn have no fully support like in C++,this make some problems using enums with arrays or floats.
Some Functions dont work with enums.The most Functions who work are with fakemeta.
Enums dont check if something right or wrong.
They can give Problems,but not must.

You can use enums,but only if you really check that everything is ok.