[Include] Vector implementation in PAWN
#21

I don't know any practical use for that unless you want to make player data extensible in runtime. If you really want to structurize single player data as a single vector, you can do
pawn Код:
// "MEM_struct" is an alias of "enum _:"
MEM_struct EPlayerData
{
    Float:EPlayerData_Health,
    Float:EPlayerData_Armour
}

// Somewhere inside a function
// This vector is not a list of player data
// But represents data of a single player instead
new Vector:player_data_vector, Float:health, Float:armour;
GetPlayerHealth(playerid, health);
GetPlayerArmour(playerid, armour);
VECTOR_resize(player_data_vector, EPlayerData);
VECTOR_set_val(player_data_vector, _:EPlayerData_Health, _:health);
VECTOR_set_val(player_data_vector, _:EPlayerData_Armour, _:armour);
// Store "player_data_vector" somewhere

// Read from "player_data_vector"
// Assuming "player_data_vector" references to the previous vector
new Float:health = Float:VECTOR_get_val(player_data_vector, _:EPlayerData_Health);
new Float:armour = Float:VECTOR_get_val(player_data_vector, _:EPlayerData_Armour);

// Clear vector if no more needed
VECTOR_clear(player_data_vector);
I would actually not recommend doing this, because you can already use a statically sized array, which is syntactically easier to structurize.
However I posted this for the sake of if for example you want to make player data actually extensible in runtime.


I believe this is actually not what you meant, so I'll post a more practical use for storing player data, where data of all players are stored inside a vector.
pawn Код:
// "MEM_struct" is an alias of "enum _:"
MEM_struct EPlayerData
{
    Float:EPlayerData_Health,
    Float:EPlayerData_Armour
}

// This is a "list" of player data
new Vector:playerDataVector;

public OnGameModeInit()
{
    // ...
   
    // Resize player data vector to store data for all players
    VECTOR_resize(playerDataVector, MAX_PLAYERS);
   
    // ...
}

public OnGameModeExit()
{
    // ...
   
    // Clear vector if no more needed
    VECTOR_clear(playerDataVector);
   
    // ...
}

// ...
// Somewhere inside a function, where player data is stored
new player_data[EPlayerData];
GetPlayerHealth(playerid, player_data[EPlayerData_Health]);
GetPlayerArmour(playerid, player_data[EPlayerData_Armour]);
VECTOR_set_arr(playerDataVector, playerid, player_data);

// ...
// Somewhere inside a function, where player data is read
new player_data[EPlayerData];
VECTOR_get_arr(playerDataVector, playerid, player_data);

// "player_data" now contains the player data recorded inside the player data vector
printf("Recorded health: %f; recorded armour: %f", player_data[EPlayerData_Health], player_data[EPlayerData_Armour]);
Reply


Messages In This Thread
Vector implementation in PAWN - by BigETI - 16.07.2018, 20:06
Re: Vector implementation in PAWN - by TroS - 16.07.2018, 20:11
Re: Vector implementation in PAWN - by BigETI - 19.07.2018, 07:12
Re: Vector implementation in PAWN - by TommyB - 19.07.2018, 07:23
Re: Vector implementation in PAWN - by BigETI - 19.07.2018, 07:29
Re: Vector implementation in PAWN - by iLearner - 19.07.2018, 07:30
Re: Vector implementation in PAWN - by CodeStyle175 - 19.07.2018, 08:40
Re: Vector implementation in PAWN - by GhostHacker9 - 19.07.2018, 09:03
Re: Vector implementation in PAWN - by Calisthenics - 12.10.2018, 09:36
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 12:10
Re: Vector implementation in PAWN - by BigETI - 27.10.2018, 12:13
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 14:00
Re: Vector implementation in PAWN - by BigETI - 27.10.2018, 15:37
Re: Vector implementation in PAWN - by 2Col - 27.10.2018, 16:30
Re: Vector implementation in PAWN - by d3Pedro - 27.10.2018, 23:09
Re: Vector implementation in PAWN - by 2Col - 11.11.2018, 08:15
Re: Vector implementation in PAWN - by BigETI - 11.11.2018, 21:30
Re: Vector implementation in PAWN - by 2Col - 12.11.2018, 09:18
Re: Vector implementation in PAWN - by BigETI - 12.11.2018, 10:04
Re: Vector implementation in PAWN - by 2Col - 12.11.2018, 10:52
Re: Vector implementation in PAWN - by BigETI - 12.11.2018, 13:22

Forum Jump:


Users browsing this thread: 1 Guest(s)