GetPlayerWeaponData doesn't return the correct info
#1

Well, I've been trying to create a list of weapons and it seems that GetPlayerWeaponData limits it.

A better example would be to give player some weapons and run GetPlayerWeaponData. It returns the correct info till now. After, give the player another weapon and run GetPlayerWeaponData again. Guess what? The new weapon you just gave the player isn't returned by the function.

pawn Код:
CMD:test( playerid )
{
        GivePlayerWeapon( playerid, 24, 300 );
        GivePlayerWeapon( playerid, 25, 300 );

    new
                weapon_id_test[ 13 ],
                weapon_ammo_test[ 13 ]
            ;

            for ( new i = 0; i <= 12; i ++ )
            {
                GetPlayerWeaponData( playerid, i, weapon_id_test[ i ], weapon_ammo_test[ i ] );
                printf("Weapon: %d and ammo: %d", weapon_id_test[ i ], weapon_ammo_test[ i ] );
            }

            GivePlayerWeapon( playerid, 31, 300 );

            new
                w_data[13][2];

            for(new a = 0; a < 13; a++)
            {
                GetPlayerWeaponData(playerid, a, w_data[a][0], w_data[a][1]);
                printf("Weaponry for slot %i: weapon (%i), amount (%i)", a, w_data[a][0], w_data[a][1]);
            }
            return 1;
}
If there is something I did wrong, just let me know in the comments bellow. Thanks.
Reply
#2

I saw, there is nothing wrong. I tested and it actually returns data before that command was used (even though guns are given on command).

I guess GetPlayerWeaponData is not server sided weapon data, so both function (perhaps, getplayerweapon too) need to wait for player sync. I think without this, an anti-cheat script would have failure possibility. All you can do is probably to wait (by using timer).
Reply
#3

Two workarounds available:

1. No sync (to be used only when needed):

pawn Код:
// ** INCLUDES

#include <a_samp>
#include <zcmd>

// ** VARIABLES

// *** PER-PLAYER VARIABLES

// **** GLOBAL

new pWeaponData[MAX_PLAYERS][2][13];

// ** MAIN

main()
{
    print("Loaded \"get_player_weapon_data_no_sync.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** COMMANDS

CMD:weapondata1(playerid, params[])
{
    new string[144], weapon_id[13], weapon_ammo[13];
    GivePlayerWeapon_NoSync(playerid, 26, 500);

    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData_NoSync(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

CMD:weapondata2(playerid, params[])
{
    new string[144], weapon_id[13], weapon_ammo[13];
    GivePlayerWeapon_NoSync(playerid, 27, 500);

    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData_NoSync(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

// ** FUNCTIONS

stock GivePlayerWeapon_NoSync(playerid, weapon_id, weapon_ammo)
{
    new weapon_data[2], slot = GetWeaponSlot(weapon_id);
    GetPlayerWeaponData(playerid, slot, weapon_data[0], weapon_data[1]);

    if(weapon_data[1] >= 1)
    {
        pWeaponData[playerid][1][slot] = weapon_data[1] + weapon_ammo;
    }
    else
    {
        pWeaponData[playerid][0][slot] = weapon_id;
        pWeaponData[playerid][1][slot] = weapon_ammo;
    }
    return GivePlayerWeapon(playerid, weapon_id, weapon_ammo);
}

stock GetPlayerWeaponData_NoSync(playerid, slot, &weapon_id, &weapon_ammo)
{
    weapon_id = pWeaponData[playerid][0][slot];
    weapon_ammo = pWeaponData[playerid][1][slot];
    return 1;
}

stock GetWeaponSlot(weaponid)
{
    new slot;
    switch(weaponid)
    {
        case 0, 1: slot = 0;
        case 2, 3, 4, 5, 6, 7, 8, 9: slot = 1;
        case 22, 23, 24: slot = 2;
        case 25, 26, 27: slot = 3;
        case 28, 29, 32: slot = 4;
        case 30, 31: slot = 5;
        case 33, 34: slot = 6;
        case 35, 36, 37, 38: slot = 7;
        case 16, 17, 18, 39: slot = 8;
        case 41, 42, 43: slot = 9;
        case 10, 11, 12, 13, 14, 15: slot = 10;
        case 44, 45, 46: slot = 11;
        case 40: slot = 12;
    }
    return slot;
}
2. Delay:

pawn Код:
// ** INCLUDES

#include <a_samp>
#include <zcmd>

// ** VARIABLES

// *** PER-PLAYER VARIABLES

// **** TIMERS

new tmPrintPlayerWeaponData[MAX_PLAYERS];

// ** MAIN

main()
{
    print("Loaded \"get_player_weapon_data_delay.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    KillTimer(tmPrintPlayerWeaponData[playerid]);
    return 1;
}

// ** COMMANDS:

CMD:weapondata(playerid, params[])
{
    GivePlayerWeapon(playerid, 24, 300);
    GivePlayerWeapon(playerid, 25, 300);
    GivePlayerWeapon(playerid, 31, 300);

    tmPrintPlayerWeaponData[playerid] = SetTimerEx("PrintPlayerWeaponData", 500, false, "i", playerid);
    return 1;
}

// ** FUNCTIONS

forward PrintPlayerWeaponData(playerid);
public PrintPlayerWeaponData(playerid)
{
    new string[144], weapon_id[13], weapon_ammo[13];
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
Reply
#4

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Two workarounds available:

1. No sync (to be used only when needed):

pawn Код:
// ** INCLUDES

#include <a_samp>
#include <zcmd>

// ** VARIABLES

// *** PER-PLAYER VARIABLES

// **** GLOBAL

new pWeaponData[MAX_PLAYERS][2][13];

// ** MAIN

main()
{
    print("Loaded \"get_player_weapon_data_no_sync.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// ** COMMANDS

CMD:weapondata1(playerid, params[])
{
    new string[144], weapon_id[13], weapon_ammo[13];
    GivePlayerWeapon_NoSync(playerid, 26, 500);

    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData_NoSync(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

CMD:weapondata2(playerid, params[])
{
    new string[144], weapon_id[13], weapon_ammo[13];
    GivePlayerWeapon_NoSync(playerid, 27, 500);

    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData_NoSync(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}

// ** FUNCTIONS

stock GivePlayerWeapon_NoSync(playerid, weapon_id, weapon_ammo)
{
    new weapon_data[2], slot = GetWeaponSlot(weapon_id);
    GetPlayerWeaponData(playerid, slot, weapon_data[0], weapon_data[1]);

    if(weapon_data[1] >= 1)
    {
        pWeaponData[playerid][1][slot] = weapon_data[1] + weapon_ammo;
    }
    else
    {
        pWeaponData[playerid][0][slot] = weapon_id;
        pWeaponData[playerid][1][slot] = weapon_ammo;
    }
    return GivePlayerWeapon(playerid, weapon_id, weapon_ammo);
}

stock GetPlayerWeaponData_NoSync(playerid, slot, &weapon_id, &weapon_ammo)
{
    weapon_id = pWeaponData[playerid][0][slot];
    weapon_ammo = pWeaponData[playerid][1][slot];
    return 1;
}

stock GetWeaponSlot(weaponid)
{
    new slot;
    switch(weaponid)
    {
        case 0, 1: slot = 0;
        case 2, 3, 4, 5, 6, 7, 8, 9: slot = 1;
        case 22, 23, 24: slot = 2;
        case 25, 26, 27: slot = 3;
        case 28, 29, 32: slot = 4;
        case 30, 31: slot = 5;
        case 33, 34: slot = 6;
        case 35, 36, 37, 38: slot = 7;
        case 16, 17, 18, 39: slot = 8;
        case 41, 42, 43: slot = 9;
        case 10, 11, 12, 13, 14, 15: slot = 10;
        case 44, 45, 46: slot = 11;
        case 40: slot = 12;
    }
    return slot;
}
2. Delay:

pawn Код:
// ** INCLUDES

#include <a_samp>
#include <zcmd>

// ** VARIABLES

// *** PER-PLAYER VARIABLES

// **** TIMERS

new tmPrintPlayerWeaponData[MAX_PLAYERS];

// ** MAIN

main()
{
    print("Loaded \"get_player_weapon_data_delay.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    KillTimer(tmPrintPlayerWeaponData[playerid]);
    return 1;
}

// ** COMMANDS:

CMD:weapondata(playerid, params[])
{
    GivePlayerWeapon(playerid, 24, 300);
    GivePlayerWeapon(playerid, 25, 300);
    GivePlayerWeapon(playerid, 31, 300);

    tmPrintPlayerWeaponData[playerid] = SetTimerEx("PrintPlayerWeaponData", 500, false, "i", playerid);
    return 1;
}

// ** FUNCTIONS

forward PrintPlayerWeaponData(playerid);
public PrintPlayerWeaponData(playerid)
{
    new string[144], weapon_id[13], weapon_ammo[13];
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weapon_id[i], weapon_ammo[i]);

        format(string, sizeof(string), "Slot: %d - Weapon ID: %d - Ammo: %d", i, weapon_id[i], weapon_ammo[i]);
        SendClientMessage(playerid, -1, string);
    }
    return 1;
}
I'm aware that workarounds are available. Thanks for sharing though
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)