Issues with a Weapon System
#1

Well, I have another issue now.

I started coding a weapon saving system so when the user logs off it should save his weapon and return it to him when he logs back in, the only problem with that, its not doing that.

Код:
enum pInv
{
	iWeapons[13],
	iWeaponammo[13]
}
new PlayerInv[MAX_PLAYERS][pInv][14];
This is the enum for the inventory, right now, just containing the weapons and ammo, but will be expanded when I fix this.

Код:
stock SavePlayerWeapons(playerid)
{
	for(new x; x < 13; x++)
	{
	    new weaponid = PlayerInv[playerid][iWeapons][x];
	    new ammo = PlayerInv[playerid][iWeaponammo][x];
	    GetPlayerWeaponData(playerid, x, weaponid, ammo);
    	    new INI:File = INI_Open(UserPath(playerid));
    	    INI_WriteInt(File,"Weapons",PlayerInv[playerid][iWeapons][13]);
    	    INI_WriteInt(File,"Weapon_Ammo",PlayerInv[playerid][iWeaponammo][13]);
    	    INI_Close(File);
	}
	return 0;
}
stock GivePlayerSavedWeapons(playerid)
{
	for(new x; x < 13; x++)
	{
	    SetPlayerArmedWeapon(playerid, 0);
	    new weaponid = PlayerInv[playerid][iWeapons][x];
	    new ammo = PlayerInv[playerid][iWeaponammo][x];
	    GivePlayerWeapon(playerid, weaponid, ammo);
	}
	return 0;
}
Those are the stocks for saving and providing the user with their weapons, those stocks are called appropriately:

Код:
public OnPlayerDisconnect(playerid, reason)
{
    new INI:File = INI_Open(UserPath(playerid));
    INI_SetTag(File,"Player Statistics");
    INI_WriteInt(File,"Cash",GetPlayerMoney(playerid));
    INI_WriteInt(File,"Admin",PlayerInfo[playerid][pAdminLevel]);
    INI_WriteInt(File,"Score",GetPlayerScore(playerid));
    INI_SetTag(File,"Player Inventory");
    INI_WriteInt(File,"Job_Certificate",PlayerInfo[playerid][pJobCert]);
    SavePlayerWeapons(playerid);
    INI_Close(File);
    VGod[playerid] = 0;
    return 1;
}
public OnPlayerSpawn(playerid)
{
    SetPlayerInterior(playerid,0);
    TogglePlayerClock(playerid,1);
    SetPlayerSkillLevel(playerid, WEAPONSKILL_PISTOL, 0);
    SetPlayerSkillLevel(playerid, WEAPONSKILL_PISTOL_SILENCED, 0
    if(GetPlayerTeam(playerid) == Cops)
    {
            SetPlayerColor(playerid, CopsColor);
            SetPlayerPos(playerid, 2267.9407,2448.7957,3.5313);
            PlayerTextDrawHide(playerid, Textdraw0);
            PlayerTextDrawHide(playerid, Textdraw1);
            PlayerTextDrawHide(playerid, Textdraw2);
            PlayerTextDrawHide(playerid, Textdraw3);
            PlayerTextDrawHide(playerid, Textdraw4);
            GivePlayerSavedWeapons(playerid);
    }
    else if(GetPlayerTeam(playerid) == Robbers)
    {
            SetPlayerColor(playerid, RobbersColor);
            SetPlayerRandomRobbersSpawn(playerid);
            PlayerTextDrawHide(playerid, Textdraw0);
            PlayerTextDrawHide(playerid, Textdraw1);
            PlayerTextDrawHide(playerid, Textdraw2);
            PlayerTextDrawHide(playerid, Textdraw3);
            PlayerTextDrawHide(playerid, Textdraw4);
            GivePlayerSavedWeapons(playerid);
    }
    else if(GetPlayerTeam(playerid) == BaitCar)
    {
            SetPlayerColor(playerid, BaitCarColor);
            SetPlayerPos(playerid, 2267.9407,2448.7957,3.5313);
            PlayerTextDrawShow(playerid, Textdraw0);
            PlayerTextDrawShow(playerid, Textdraw1);
            PlayerTextDrawShow(playerid, Textdraw2);
            PlayerTextDrawShow(playerid, Textdraw3);
            PlayerTextDrawShow(playerid, Textdraw4);
	    GivePlayerSavedWeapons(playerid);
    }
    return 1;
}
Those are the calls for the Weapon system, to Save and Give the weapons out, respectively.

I just cant figure out why the weapons aren't saving and being given out to the player, the only error I get is yini saying it cant find the player profile, but it can normally find it, I think thats something to do with the stock.

I'm pretty sure its something to do with the variables I'm using, also, credits for the Weapon stocks goto another user on the forums, I just changed them, it appears, for the worse of things lol.

Any help would be appreciated,
Thanks,
Zonoya.
Reply
#2

Do a debug and check if you are receiving the right values for saving.

Example:

pawn Код:
stock MyFunc(var1,var2)
{
    printf("I received: var1 = %d and var2 = %d",var1,var2");
    // your stock...
}
This way you will see if the problems is in saving system, or on the stock.

Post here the result.

Read more about debugging: https://sampwiki.blast.hk/wiki/Debugging
Reply
#3

Код:
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
Thats for loading, which isn't right, because that is all 13 weapon slots shown as empty.

Код:
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 31 and var2 = 1000
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
I have received var1 = 0 and var2 = 0
And that's when I log off, but this time YINI doesn't bug out and say it can't reach my data file, but when I look into my data file, I get this:

Код:
Weapons = 0
Weapon_Ammo = 0
[Player Statistics]
Cash = 0
Admin = 0
Score = 0
[Player Inventory]
Job_Certificate = 0
The order isn't right, but that's about all that's wrong, apart from the obvious problem that nothing is showing up in the weapons and weapon ammo section.

To sum up:
The script isn't saving into the correct file when It closes, because it receives the variable but doesn't put it in the file. And when it loads the file to give the gun, regardless of what is in that file, it thinks nothing is in there.
Reply
#4

Deleted Post.
Reply
#5

I've never worked with y_ini.

But re-reading you code, i found that you are saving by the wrong way.

pawn Код:
stock SavePlayerWeapons(playerid)
{
    for(new x; x < 13; x++)
    {
        new weaponid = PlayerInv[playerid][iWeapons][x]; // Why you are setting a value, if you will change them at GetPlayerWeaponData?
        new ammo = PlayerInv[playerid][iWeaponammo][x];  // Why you are setting a value, if you will change them at GetPlayerWeaponData?
        GetPlayerWeaponData(playerid, x, weaponid, ammo); // You will set them finally here.
        new INI:File = INI_Open(UserPath(playerid));
        INI_WriteInt(File,"Weapons",PlayerInv[playerid][iWeapons][13]); // This will always write the same value, at all the iterations.
        INI_WriteInt(File,"Weapon_Ammo",PlayerInv[playerid][iWeaponammo][13]); // This will always write the same value, at all the iterations.
        INI_Close(File);
    }
    return 0; // Why return 0? This func WILL always return 0.
}
It will not save the weapons properlyl, i've explained the errors, try to fix them

If have 13 slots, you should save it into 26 "tags", 13 for the weapons ID's and the other 13 for the ammo.
You are just saving in only one o.o'
Reply
#6

Thanks, mate, works like a charm now that I have it saving for all 13 weapons and their ammo.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)