string overwrites the last one in file -
HondaCBR - 10.04.2012
when creating an object, it overwrites the previous one
pawn Код:
if(dialogid == OBIEKT2)
{
if(response)
{
new object = 0;
if(IsNumeric(inputtext) || strval(inputtext) > 0 || strval(inputtext) < 19470)
{
object ++;
new File:hFile;
new Float:X, Float:Y, Float:Z;
GetPlayerPos(playerid, X, Y, Z);
new obj2[MAX_OBJECTS_2];
SelectedO[playerid] = object;
Selected1[playerid] = strval(inputtext);
obj2[object] = CreateObject(strval(inputtext), X+1, Y+1, Z+1, 0.0,0.0,0.0);
EditObject(playerid, obj2[object]);
hFile = fopen("/Ustawienia/obiekty.ini", io_write);
new line[128];
format(line, sizeof(line), "%d,%f,%f,%f,%f,%f,%f,%d\r\n",strval(inputtext), X+1,Y+1,Z+1, 0,0,0,object);
fwrite(hFile, line);
return 1;
}
}
}
Re: string overwrites the last one in file -
ReneG - 10.04.2012
io_write clears all text.
io_append writes to a file, without clearing any text.
Feel free to look at the link below for more information on file functions
https://sampwiki.blast.hk/wiki/File_Functions
pawn Код:
hFile = fopen("/Ustawienia/obiekty.ini", io_append);
Re: string overwrites the last one in file -
HondaCBR - 11.04.2012
Thx man. But I still have another question lets say those are my objects:
Код:
17000,1192.570190,-1339.622924,14.399080,0.000000,0.000000,0.000000,1
17200,1192.570190,-1339.622924,14.399080,0.000000,0.000000,0.000000,2
17300,1192.570190,-1339.622924,14.399080,0.000000,0.000000,0.000000,3
The ideas are coded ingame as Object[1] while adding them, lets say I want to edit object[2] I move it and click the save icon, how can I update just X Y Z XR YR ZR only for the line that has 2 at the end.
Re: string overwrites the last one in file -
ReneG - 11.04.2012
Instead of writing it how you are. You need to loop through all the objects that are being made, and write them like that. Very difficult to only replace one line in a file.
Re: string overwrites the last one in file -
HondaCBR - 11.04.2012
How can I do that?
Re: string overwrites the last one in file -
ReneG - 11.04.2012
I haven't experienced too much with file writing, but I reckon this is how it would go
pawn Код:
stock SaveObjects()
{
// You would make new variables that will hold the object info
new
modelid,
Float:x,
Float:y,
Float:z,
Float:rotX,
Float:rotY,
Float:rotZ,
coordstring[128]
;
for(new i=0;i<11;i++) // Make a loop through 11 objects.
{
GetObjectRot(i, rotX, rotY, rotZ);
GetObjectPos(i, x, y, z);
new File:objects = fopen("objects.txt",io_append);
format(coordstring,sizeof(coordstring),"%d,%f,%f,%f,%f,%f,%f\r\n",i,x,y,z,rotX,rotY,rotZ);
fwrite(objects, coordstring);
fclose(objects);
}
return 1;
}
So I'm guessing you would add SaveObjects(); in your edit object command?
This is just an example.
Re: string overwrites the last one in file -
HondaCBR - 11.04.2012
I think I went wrong somewhere :/
pawn Код:
public OnPlayerEditObject(playerid, playerobject, objectid, response, Float:fX, Float:fY, Float:fZ, Float:fRotX, Float:fRotY, Float:fRotZ)
{
if(!IsValidObject(objectid)) return;
MoveObject(objectid, fX, fY, fZ, 10.0, fRotX, fRotY, fRotZ);
if(response == 1)//saving on click
{
new
modelid,
Float:x,
Float:y,
Float:z,
Float:rotX,
Float:rotY,
Float:rotZ,
coordstring[128]
;
for(new i=0;i<11;i++) // Make a loop through 11 objects.
{
GetObjectRot(i, rotX, rotY, rotZ);
GetObjectPos(i, x, y, z);
new File:objects = fopen("/Ustawienia/obiekty.txt",io_append);
format(coordstring,sizeof(coordstring),"%d,%f,%f,%f,%f,%f,%f\r\n",i,modelid,x,y,z,rotX,rotY,rotZ);
fwrite(objects, coordstring);
fclose(objects);
}
}
}
pawn Код:
public LoadObiekty()
{
new arrCoords[8][64];
new strFromFile2[256];
new File: file = fopen("/Ustawienia/obiekty.txt", io_read);
if (file)
{
new idx;
while (idx < sizeof(ObiektInfo))
{
fread(file, strFromFile2);
split(strFromFile2, arrCoords, ',');
ObiektInfo[idx][ObiektUID] = strval(arrCoords[0]);
ObiektInfo[idx][OModelID] = strval(arrCoords[1]);
ObiektInfo[idx][fX2] = floatstr(arrCoords[2]);
ObiektInfo[idx][fY2] = floatstr(arrCoords[3]);
ObiektInfo[idx][fZ2] = floatstr(arrCoords[4]);
ObiektInfo[idx][fRotX2] = floatstr(arrCoords[5]);
ObiektInfo[idx][fRotY2] = floatstr(arrCoords[6]);
ObiektInfo[idx][fRotZ2] = floatstr(arrCoords[7]);
new ObiektFF[MAX_OBIEKTOW];
ObiektFF[ObiektInfo[idx][ObiektUID]] = CreateObject(ObiektInfo[idx][OModelID],ObiektInfo[idx][fX2],ObiektInfo[idx][fY2],ObiektInfo[idx][fZ2],ObiektInfo[idx][fRotX2],ObiektInfo[idx][fRotY2],ObiektInfo[idx][fRotZ2]);
idx++;
}
printf("Obiekty from file: %d",idx);
fclose(file);
}
return 1;
}
pawn Код:
if(dialogid == OBIEKT2)
{
if(response)
{
new SettingsFile12[35];
format(SettingsFile12,sizeof(SettingsFile12),"Ustawienia/settings.ini");
new uid = dini_Int(SettingsFile12,"Obiektow");
if(IsNumeric(inputtext) || strval(inputtext) > 0 || strval(inputtext) < 19470)
{
new
modelid,
Float:x,
Float:y,
Float:z,
Float:rotX,
Float:rotY,
Float:rotZ,
coordstring[128]
;
// for(new i=0;i<11;i++)
// {
new Obiekt[MAX_OBIEKTOW];
uid ++;
dini_IntSet(SettingsFile12,"Obiektow",uid);
GetPlayerPos(playerid, x,y,z);
Obiekt[uid] = CreateObject(strval(inputtext), x+1, y+1, z+1, 0.0,0.0,0.0);
GetObjectRot(Obiekt[uid], rotX, rotY, rotZ);
GetObjectPos(Obiekt[uid], x, y, z);
EditObject(playerid, Obiekt[uid]);
new File:objects = fopen("/Ustawienia/obiekty.txt",io_append);
format(coordstring,sizeof(coordstring),"%d,%d,%f,%f,%f,%f,%f,%f\r\n",uid,strval(inputtext),x,y,z,rotX,rotY,rotZ);
fwrite(objects, coordstring);
fclose(objects);
// }
return 1;
}
}
}
Basically I want to createobjects and save them to file, then when they load, if you move them to update in that file to new position.