[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


Forum Jump:


Users browsing this thread: 1 Guest(s)