!HELP! Inventory System
#8

1.Define an enum

pawn Код:
enum iinfo
{
  money,
  bank,
  mats,
  drugs
}
Inventory[MAX_PLAYERS][iinfo];
2.Now to create the file functions. First, you make a forward for them.

pawn Код:
forward CreateInventoryFile(playerid);
forward LoadInventoryFile(playerid);
forward SaveInventoryFile(playerid);
forward Split(const strscr[], strdest[][], delimiter);
3.Then make the functions.

pawn Код:
public CreateInventoryFile(playerid)
{
    new file[128];
    new filestr[128];

    new name[maxplayername];
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "%s.inv", name);
    new File: pfile = fopen(file, io_write);
    if(fexist(file))
    {
        Inventory[playerid][money]   = random(500) + 500;
        Inventory[playerid][bank]   = random(500);
        Inventory[playerid][mats]  = 500;
        Inventory[playerid][drugs]   = 0;

        format(filestr, sizeof(filestr),
            "%d/%d/%d/%d",
            Inventory[playerid][money],
            Inventory[playerid][bank],
            Inventory[playerid][mats],
            Inventory[playerid][drugs]
        );

        fwrite(pfile, filestr);
        fclose(pfile);
    }
    return 1;
}
public LoadInventoryFile(playerid)
{
    new file[128];
    new name[maxplayername];
    GetPlayerName(playerid, name, sizeof(name));

    format(file, sizeof(file), "/inventory/%s.inv", name);
    new File: pfile = fopen(file, io_read);
    if(fexist(file))
    {
        new filestr[128];
        new filearray[4][12]; // <- the first number is always one more than the lowest filearray[] below. The second is how long you want each variable in the array to be.
        fread(pfile, filestr);
        Split(filestr, filearray, '/'); // You can replace '/' with another symbol such as '|' if you want. but just make sure you update how the file is written to match that symbol.

        Inventory[playerid][money]  = strval(filearray[0]);
        Inventory[playerid][bank]   = strval(filearray[1]);
        Inventory[playerid][mats]   = strval(filearray[2]);
        Inventory[playerid][drugs]  = strval(filearray[3]);

    fclose(pfile);
    }
    return 1;
}
public SaveInventoryFile(playerid)
{
    new file[128];
    new name[maxplayername];
    GetPlayerName(playerid, name, sizeof(name));
    format(file, sizeof(file), "/inventory/%s.inv", name);
    if(fexist(file))
    {
        new filestr[128];
        new File: pfile = fopen(file, io_write);

        format(filestr, sizeof(filestr),
            "%d/%d/%d/%d",
            Inventory[playerid][money],
            Inventory[playerid][bank],
            Inventory[playerid][mats],
            Inventory[playerid][drugs]
        );

        fwrite(pfile, filestr);
        fclose(pfile);
    }
    return 1;
}
public Split(const strscr[], strdest[][], delimiter)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc))
    {
      if(strsrc[i]==delimiter || i==strlen(strsrc))
        {
        len = strmid(strdest[aNum], strsrc, li, i, 128);
        strdest[aNum][len] = 0;
        li = i+1;
        aNum++;
        }
        i++;
    }
    return 1;
}
4.Now just make sure you place the CreateInventoryFile(playerid); into your registration so that the file is made. You will now be able to set values to any of those variables you listed just like they are shown in the example below.

Ex.
pawn Код:
public examplegive100dollarsfunction(playerid)
{
  new amount = Inventory[playerid][money] += 100;
  GivePlayerMoney(playerid, amount);
  return 1;
}
Enjoy, you can modify this to make any type of file system. Sadly it is not very dynamic and if you change the way the file is, you have to either create the new one, or have a function to convert it to the new one, or do it manually.
Reply


Messages In This Thread
!HELP! Inventory System - by notec100 - 01.03.2009, 16:16
Re: !HELP! Inventory System - by notec100 - 01.03.2009, 22:07
Re: !HELP! Inventory System - by JaYmE - 02.03.2009, 15:06
Re: !HELP! Inventory System - by notec100 - 02.03.2009, 18:19
Re: !HELP! Inventory System - by mirkoiz - 03.03.2009, 13:58
Re: !HELP! Inventory System - by notec100 - 05.03.2009, 06:44
Re: !HELP! Inventory System - by MaXx001 - 05.03.2009, 07:10
Re: !HELP! Inventory System - by Kinetic - 05.03.2009, 18:47
Re: !HELP! Inventory System - by mirkoiz - 05.03.2009, 18:51
Re: !HELP! Inventory System - by notec100 - 05.03.2009, 21:19

Forum Jump:


Users browsing this thread: 1 Guest(s)