SA-MP Forums Archive
New files overwrite eachother - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: New files overwrite eachother (/showthread.php?tid=363025)



New files overwrite eachother - Devix - 26.07.2012

Hey guys! I have the following script snippet:

Код:
	new h;
	while (h < sizeof(HouseInfo))
	{
		new randvir = 2 + random(1999999);
		new Float:x, Float:y, Float:z;
		new Float:a;
		GetPlayerPos(playerid,x,y,z);
		HouseInfo[h][hX] = x;
		HouseInfo[h][hY] = y;
		HouseInfo[h][hZ] = z;
		HouseInfo[h][hA] = a;
		HouseInfo[h][hDes] = name;
		HouseInfo[h][hPrice] = price;
		HouseInfo[h][hWorld] = randvir;
		HouseInfo[h][hRent] = 0;
		HouseInfo[h][hOwner] = 0;
		HouseInfo[h][hOwned] = 0;
		SaveHouses(h);
		h++;
		return 1;
	}
But it creates a file with the name "1" but not "2" etc, it basically rewrites the first file.

Any ideas?


Re: New files overwrite eachother - Devix - 26.07.2012

Anyone? Please, I have had this issue for a long time and I am driving myself crazy trying all sorts of things :P.


Re: New files overwrite eachother - AmigaBlizzard - 26.07.2012

Post your SaveHouses function.
The code you've posted doesn't include the saving system, you're only presetting the data for a house.


Re: New files overwrite eachother - Devix - 26.07.2012

SaveHouses(houseid)
{
new path[256];
format(path, 35, HOUSES_FILE, houseid);
if(INI_Exists(path) == 0)
{
houseid++;
format(path, 68, "Houses/%i.ini", houseid);
new INI:HouseFile = INI_Open(path);
INI_WriteString(HouseFile,"Owner", HouseInfo[houseid][hOwner]);
INI_WriteString(HouseFile,"Description", HouseInfo[houseid][hDes]);
INI_WriteInt(HouseFile,"Price", HouseInfo[houseid][hPrice]);
INI_WriteInt(HouseFile,"Rent", HouseInfo[houseid][hRent]);
INI_WriteInt(HouseFile,"World", HouseInfo[houseid][hWorld]);
INI_WriteInt(HouseFile,"Interior", HouseInfo[houseid][hInterior]);
INI_WriteFloat(HouseFile,"X", HouseInfo[houseid][hX]);
INI_WriteFloat(HouseFile,"Y", HouseInfo[houseid][hY]);
INI_WriteFloat(HouseFile,"Z", HouseInfo[houseid][hZ]);
INI_WriteFloat(HouseFile,"A", HouseInfo[houseid][hA]);
INI_Close(HouseFile);
return 1;
}
new INI:HouseFile = INI_Open(path);
INI_WriteString(HouseFile,"Owner", HouseInfo[houseid][hOwner]);
INI_WriteString(HouseFile,"Description", HouseInfo[houseid][hDes]);
INI_WriteInt(HouseFile,"Price", HouseInfo[houseid][hPrice]);
INI_WriteInt(HouseFile,"Rent", HouseInfo[houseid][hRent]);
INI_WriteInt(HouseFile,"World", HouseInfo[houseid][hWorld]);
INI_WriteInt(HouseFile,"Interior", HouseInfo[houseid][hInterior]);
INI_WriteFloat(HouseFile,"X", HouseInfo[houseid][hX]);
INI_WriteFloat(HouseFile,"Y", HouseInfo[houseid][hY]);
INI_WriteFloat(HouseFile,"Z", HouseInfo[houseid][hZ]);
INI_WriteFloat(HouseFile,"A", HouseInfo[houseid][hA]);
INI_Close(HouseFile);
print("Houses saved");
return 1;
}


Re: New files overwrite eachother - AmigaBlizzard - 26.07.2012

Try this instead:

Код:
SaveHouses(houseid)
{
	new path[128];

	houseid++;
	format(path, 128, "Houses/%i.ini", houseid);

	new INI:HouseFile = INI_Open(path);
	INI_WriteString(HouseFile,"Owner", HouseInfo[houseid][hOwner]);
	INI_WriteString(HouseFile,"Description", HouseInfo[houseid][hDes]);
	INI_WriteInt(HouseFile,"Price", HouseInfo[houseid][hPrice]);
	INI_WriteInt(HouseFile,"Rent", HouseInfo[houseid][hRent]);
	INI_WriteInt(HouseFile,"World", HouseInfo[houseid][hWorld]);
	INI_WriteInt(HouseFile,"Interior", HouseInfo[houseid][hInterior]);
	INI_WriteFloat(HouseFile,"X", HouseInfo[houseid][hX]);
	INI_WriteFloat(HouseFile,"Y", HouseInfo[houseid][hY]);
	INI_WriteFloat(HouseFile,"Z", HouseInfo[houseid][hZ]);
	INI_WriteFloat(HouseFile,"A", HouseInfo[houseid][hA]);
	INI_Close(HouseFile);

	print("Houses saved");

	return 1;
}
There is no need to check if your housefile already exists for saving.
If it doesn't exist, it will be created.
If it does exist, it will get overwritten.

Is the HouseID++ needed? You can also delete this if needed, except if you want to save your first house with ID 0 as "1.ini".

Also, first checking if HOUSES_FILE exists (whatever that holds), then re-creating the actual path and housefile-name is weird as well.


Re: New files overwrite eachother - Devix - 26.07.2012

It now gives me:


Owner =
Description =
Price = 0
Rent = 0
World = 0
Interior = 0
X = 0.000000
Y = 0.000000
Z = 0.000000
A = 0.000000

And its still being re-written I guess.

And yes, I want the files to be created with 1.ini 2.ini etc (house ID's as filename).

HOUSES_FILE is #define HOUSES_FILE "/Houses/%d.ini".


Re: New files overwrite eachother - Devix - 26.07.2012

Anyone ? Please I appreciate all of your help!


Re: New files overwrite eachother - AmigaBlizzard - 26.07.2012

You might wanna get rid of the "houseid++".

Let's say you want to save house with ID 0.
Adding 1 to the houseid would mean you're gonna save the data in the file 1.ini, instead of 0.ini.
It also means you're gonna save the data stored for houseid 1 into the datafile for houseid 0.
But the data for houseid 1 in your array isn't set yet.
That's why you always get empty strings, and integer/float values of 0.

Also, assume you only have 10 houses, with id 0 to 9.
Using the houseid++ in the save-function to save houseid 9 would mean you're gonna create a file called 10.ini.
You would also save the data stored at index 10 of your array, which doesn't exist as the array only has index 0 to 9.

Just get rid of the houseid++ and it will work properly.

Код:
SaveHouses(houseid)
{
	new path[128];

	format(path, 128, "Houses/%i.ini", houseid);

	new INI:HouseFile = INI_Open(path);
	INI_WriteString(HouseFile,"Owner", HouseInfo[houseid][hOwner]);
	INI_WriteString(HouseFile,"Description", HouseInfo[houseid][hDes]);
	INI_WriteInt(HouseFile,"Price", HouseInfo[houseid][hPrice]);
	INI_WriteInt(HouseFile,"Rent", HouseInfo[houseid][hRent]);
	INI_WriteInt(HouseFile,"World", HouseInfo[houseid][hWorld]);
	INI_WriteInt(HouseFile,"Interior", HouseInfo[houseid][hInterior]);
	INI_WriteFloat(HouseFile,"X", HouseInfo[houseid][hX]);
	INI_WriteFloat(HouseFile,"Y", HouseInfo[houseid][hY]);
	INI_WriteFloat(HouseFile,"Z", HouseInfo[houseid][hZ]);
	INI_WriteFloat(HouseFile,"A", HouseInfo[houseid][hA]);
	INI_Close(HouseFile);

	print("Houses saved");

	return 1;
}



Re: New files overwrite eachother - Devix - 26.07.2012

Alright now its indeed called 0.ini, but it still overwrites data.
1.ini is not getting added.


Re: New files overwrite eachother - Devix - 27.07.2012

Tried more stuff but nothing works!