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 ] );