Getting model id of spawned object
#1

Hello all, i am writing here because today I've got a problem with my objects in game.
I had already created a command to spawn objects and it works perferct, but the problem is writing them in a file:

My spawning command:
Код:
dcmd_object(playerid,params[])
{
        if(Player[playerid][admin] < 4)
		{
		return SendClientMessage (playerid,COLOR_RED,"Command unavailable");
		}
		new object;
		if(sscanf(params,"d",object))
		{
			return SendClientMessage (playerid,GELTONA,"Usage: /object [object model id]");
		}
		new Float:bx, Float:by, Float:bz, Float:ba; 
                GetPlayerPos(playerid, bx, by, bz); 
                GetPlayerFacingAngle(playerid, ba); 
                bx += (floatsin(-ba, degrees)); 
                by += (floatcos(-ba, degrees));
		CreateObject(object, bx, by, bz-0.5, 0, 0, ba); 
                ApplyAnimation(playerid, "BOMBER","BOM_Plant_In",4.0,0,0,0,0,0);
		return 1;

}
And there is my save objects to file command:

Код:
dcmd_saveobjects(playerid,params[])
{
    if(Player[playerid][admin] < 4)
	{
	 	return SendClientMessage(playerid,COLOR_RED,"Command unavailable");
 	}
	new File:obj = fopen("objects.txt", io_append);
	for(new i = 0; i < MAX_OBJECTS; i++)                     //checking all objects
	{
		for(new a=0; a < IsValidObject(i); a++)              //choosing those, whose spawned
		{
			new Float:objx, Float:objy, Float:objz, Float:objrotx, Float:objroty, Float:objrotz,string[256];
			GetObjectPos(i,objx,objy,objz);
			GetObjectRot(i,objrotx,objroty,objrotz);
			format(string,sizeof(string), "CreateObject(%d,%f,%f,%f,%f,%f,%f)\r\n",Missing Object model id,objx,objy,objz,objrotx,objroty,objrotz);
			fwrite(obj, string);
		}
	}
	new string2[256];
	format(string2,sizeof(string2), "/////////////////////////////////////////END/////////////////////////////////// \r\n");
	fwrite(obj, string2);
	fclose(obj);
	return 1;

}
Now i am getting that in my objects.txt
Код:
CreateObject(-306.100402,1529.891235,74.859375,0.000000,0.000000,201.506210)       //as you can see there is no object model id after the "("
CreateObject(-304.312042,1531.305175,74.859375,0.000000,0.000000,95.911888)
CreateObject(-302.114410,1519.946289,74.859375,0.000000,0.000000,184.658599)
/////////////////////////////////////////END///////////////////////////////////
So i am out of ideas of how to get all spawned objects model id's and put them in the file
I would be very pleased and thankful if any of you could help me

Sorry for my bad english.
Reply
#2

pawn Код:
new Obj;
new ObjInGame[MAX_OBJECTS_IN_GAME][2];
in dcmd_object(playerid,params[])

pawn Код:
ObjInGame[Obj][0] = object;
ObjInGame[Obj][1] = CreateObject(object, bx, by, bz-0.5, 0, 0, ba);
Obj++;
in dcmd_saveobjects(playerid,params[])

pawn Код:
new Float:O[6],string[256];
for(new i = 0; i < Obj; i++)                     //checking all objects
{
    GetObjectPos(ObjInGame[i][1],O[0],O[1],O[2]);
    GetObjectRot(ObjInGame[i][1],O[3],O[4],O[5]);
    format(string,sizeof(string), "CreateObject(%d,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f);\r\n",ObjInGame[i][0],O[0],O[1],O[2],O[3],O[4],O[5]);
    fwrite(obj, string);
}
Obj = 0;
Reply
#3

Thank you Jefff, for your help. Everything is working fine, but now there is another problem

I have created a command to delete selected object:
pawn Код:
if (strcmp("/delete", cmdtext, true, 10) == 0)
    {
        if(Player[playerid][admin] < 3)
        {
            return SendClientMessage(playerid, COLOR_RED,"Command unavailable");
        }
        SelectObject(playerid);
        SendClientMessage(playerid, GELTONA,"Select object, which you want to delete");
        return 1;
    }
pawn Код:
public OnPlayerSelectObject(playerid, type, objectid, modelid, Float:fX, Float:fY, Float:fZ)
{
                if(type == SELECT_OBJECT_GLOBAL_OBJECT)
        {
            DestroyObject(objectid);
        }
        else
        {
            DestroyPlayerObject(playerid, objectid);
        }
        SendClientMessage(playerid, 0xFFFFFFFF, "You have deleted selected object!");

return 1;
}
So when i spawn objects and deleting lets say one of them, then doing /saveobjects - deleted object still is in my objects.txt EXAMPLE:

---SPAWNED 3 objects and the last one(2343) was deleted with /delete and then everything is saveded with /saveobjects---
Код:
CreateObject(2341,-306.7627,1520.7591,74.8593,0.0000,0.0000,161.7124);
CreateObject(2342,-309.4980,1520.4886,74.8593,0.0000,0.0000,119.7254);
CreateObject(2343,-311.4164,1522.8221,74.8593,0.0000,0.0000,84.0050);
As you can see, there is still object CreateObject(2343,-311.4164,1522.8221,74.8593,0.0000,0.0000,84.0050); even if it was deleted.

So could you help me with that problem? Thanks again if you could, I would be very pleased again

And of course sorry for my english
Reply
#4

pawn Код:
new Obj;
new ObjInGame[MAX_OBJECTS_IN_GAME][2];

dcmd_object(playerid,params[])
{
    new object;
    if(Player[playerid][admin] < 4) SendClientMessage (playerid,COLOR_RED,"Command unavailable");
    else if(sscanf(params,"d",object)) SendClientMessage (playerid,GELTONA,"Usage: /object [object model id]");
    else{
        new Float:bx, Float:by, Float:bz, Float:ba;
        GetPlayerPos(playerid, bx, by, bz);
        GetPlayerFacingAngle(playerid, ba);
        bx += (floatsin(-ba, degrees));
        by += (floatcos(-ba, degrees));
        ObjInGame[Obj][0] = object;
        ObjInGame[Obj][1] = CreateObject(object, bx, by, bz-0.5, 0, 0, ba);
        Obj++;
        ApplyAnimation(playerid, "BOMBER","BOM_Plant_In",4.0,0,0,0,0,0);
    }
    return 1;
}

dcmd_saveobjects(playerid,params[])
{
    if(Player[playerid][admin] < 4) SendClientMessage(playerid,COLOR_RED,"Command unavailable");
    else{
        new File:obj = fopen("objects.txt", io_append);
        new Float:O[6],string[256];
        for(new i = 0; i < Obj; i++)                     //checking all objects
        {
            GetObjectPos(ObjInGame[i][1],O[0],O[1],O[2]);
            GetObjectRot(ObjInGame[i][1],O[3],O[4],O[5]);
            format(string,sizeof(string), "ID: %d - CreateObject(%d,%.4f,%.4f,%.4f,%.4f,%.4f,%.4f);\r\n",ObjInGame[i][1],ObjInGame[i][0],O[0],O[1],O[2],O[3],O[4],O[5]);
            fwrite(obj, string);
        }
        Obj = 0;
        fwrite(obj,"/////////////////////////////////////////END///////////////////////////////////\r\n");
        fclose(obj);
    }
    return 1;
}

RemoveObjectFromFile(objectid)
{
    new File:F[2],str[128],objid[15],len;
    F[0] = fopen("objects.txt",io_read);
    if(!F[0]) return;
    F[1] = fopen("temporary_objects_file.txt",io_write);

    format(objid,sizeof(objid),"ID: %d -",objectid);
    len = strlen(objid);

    while(fread(F[0],str))
    {
        if(!strcmp(str,objid,true,len)) continue;
        fwrite(F[1],str);
    }

    fclose(F[0]);
    fclose(F[1]);

    F[0] = fopen("temporary_objects_file.txt",io_read);
    F[1] = fopen("objects.txt",io_write);

    while(fread(F[0],str))
        fwrite(F[1],str);

    fclose(F[0]);
    fclose(F[1]);
    fremove("temporary_objects_file.txt");
}

public OnPlayerSelectObject(playerid, type, objectid, modelid, Float:fX, Float:fY, Float:fZ)
{
    if(type == SELECT_OBJECT_GLOBAL_OBJECT)
    {
        DestroyObject(objectid);
        RemoveObjectFromFile(objectid);
    }
    else
    {
        DestroyPlayerObject(playerid, objectid);
    }
    SendClientMessage(playerid, 0xFFFFFFFF, "You have deleted selected object!");
    return 1;
}
Reply
#5

Thank you again, Jefff, very nice job, but still not working. The object which was deleted is still written in the file.
I think my system is too complicated, so maybe someone could help me creating a new one, which just gets all spawned objects info and stores it in a file. ) (So the system should be like: i am creating the objects with a command, then editing/ deleting them and after all hard work of mapping i saving all existing objects in one text file)

If anyone could help me, give me an answer
Because I can't get done with that,just out of ideas getting model id... (everything else is simple because of GetObjectPos, GetObjectRot - but there isn't GetObjectModelID)
Reply
#6

Use prints for debugging whats wrong, U must do something wrong.

pawn Код:
public OnPlayerSelectObject(playerid, type, objectid, modelid, Float:fX, Float:fY, Float:fZ)
{
    if(type == SELECT_OBJECT_GLOBAL_OBJECT)
    {
        printf("Deleted objectid: %d",objectid);
        DestroyObject(objectid);
        RemoveObjectFromFile(objectid);
    }
    else
    {
        DestroyPlayerObject(playerid, objectid);
    }
    SendClientMessage(playerid, 0xFFFFFFFF, "You have deleted selected object!");
    return 1;
}
Reply
#7

Hi Jefff, again thank you for your hard work and advice, - finally it works But while testing it out, there occured another problem: When i spawn for example 3 objects then save them to file, and after that i delete one objcet, so logically there should be 2 objects in that file? - yes, that part works great, but problem is : when i spawn for exaple 3 objects then without /saveobjects i delete for exaple one of them and after doing that i type /saveobjects - in objects.txt there still is 3 objcets. So how to make that, if objects has been deleted, then when i type /saveobjects they wont be writen to the file automaticlly. For clarity: I spawn 4 objcets, then delete 2 of them and i should get this
Код:
ID: 1 - CreateObject(1422,-304.5164,1525.3256,74.8593,0.0000,0.0000,135.7054);
ID: 2 - CreateObject(978,-304.5164,1525.3256,74.8593,0.0000,0.0000,135.7054);
in my objects.txt

NOT THIS:

Код:
ID: 1 - CreateObject(1422,-304.5164,1525.3256,74.8593,0.0000,0.0000,135.7054);
ID: 2 - CreateObject(978,-304.5164,1525.3256,74.8593,0.0000,0.0000,135.7054);
ID: 3 - CreateObject(981,-300.5232,1527.4655,74.8593,0.0000,0.0000,287.0235);       //those been deleted before /saveobjects
ID: 4- CreateObject(983,-300.5232,1527.4655,74.8593,0.0000,0.0000,287.0235);
Reply
#8

Bump
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)