05.03.2009, 18:47
1.Define an enum
2.Now to create the file functions. First, you make a forward for them.
3.Then make the functions.
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.
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.
pawn Код:
enum iinfo
{
money,
bank,
mats,
drugs
}
Inventory[MAX_PLAYERS][iinfo];
pawn Код:
forward CreateInventoryFile(playerid);
forward LoadInventoryFile(playerid);
forward SaveInventoryFile(playerid);
forward Split(const strscr[], strdest[][], delimiter);
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;
}
Ex.
pawn Код:
public examplegive100dollarsfunction(playerid)
{
new amount = Inventory[playerid][money] += 100;
GivePlayerMoney(playerid, amount);
return 1;
}

