PDA

View Full Version : Destroy Object


Blackazur
22/02/2014, 02:17 PM
Hello, i have a command to add a object before my feet, that works, i have made then a command to destroy this object with DestroyObject(trap[playerid]); The Object is so defined: new trap[MAX_PLAYERS];
but it won't destroy if i use the add object command 2 or 3 times, then the other 2 objects from this object won't destroy. Can you say me why?

S4t3K
22/02/2014, 02:21 PM
When you create your object, do you do like that ?



trap[playerid] = CreateObject();

// OR

trap[playerid] = SetPlayerAttachedObject();


And if, add these lines below the object creating


if(IsValidObject(trap[playerid]))
{
print("Trap object successfuly created");
}


Also add these lines below the object destroy



if(!IsValidObject(trap[playerid]))
{
print("Trap object successfuly destroyed !");
}

If the strings print in the console, it means that this is a display bug, not a scripting bug.

Blackazur
22/02/2014, 02:26 PM
Like that:

trap[playerid] = CreateObject

If i add this object before my feet it works if i destroy it but if i add this object 2 times before my feet the 2 object won't destroy. Can you help me with that?

S4t3K
22/02/2014, 02:33 PM
I don't really understand.

You want to place your object at your feet. But when you create the object twice before your feet ?

What do you mean by twice ?

BigGroter
22/02/2014, 02:33 PM
It's because you only have one variable for more than one object.

Blackazur
22/02/2014, 03:52 PM
And how can i change it? If i create the object twice before my feet and i try to delete it with the command for deleting the object, it only delete the 1 object not the object that i created twice. But i want that it delete all the objects that i created.

S4t3K
22/02/2014, 03:55 PM
If you want to delete the object you've created for all players, use a for


for(new p = 0; p < MAX_PLAYERS; p++)
{
DestroyObject(trap[p]);
if(!IsValidObject(trap[p])) return print("All traps destroyed !");
else return DestroyObject(trap[p]);
}

Vince
22/02/2014, 04:00 PM
And how can i change it? If i create the object twice before my feet and i try to delete it with the command for deleting the object, it only delete the 1 object not the object that i created twice. But i want that it delete all the objects that i created.

What you're doing is similar to this:

new a;

a = 1;
a = 2;
a = 3;

printf("a = %d", a); // prints: "a = 3";


You're overwriting the variable. I hope you understand that?

S4t3K
22/02/2014, 04:06 PM
I think BigGroter has made him understand it over his explaination ^^

If you want to create an object linked to a var, you need to create a var for each object. Either, if you use the same var twice for two objects differents, the var will be linked to the last object created. The first one won't be destroyable through DestroyObject(objectid);

To take an example



new object;

object = CreateObject(model, x, y, z, 0.0, 0.0, 0.0);

object = CreateObject(othermodel, x, y, z, 0.0, 0.0, 0.0);

DestroyObject(object); // Will destroy the "othermodel", not "model".

Blackazur
22/02/2014, 04:14 PM
What you're doing is similar to this:

new a;

a = 1;
a = 2;
a = 3;

printf("a = %d", a); // prints: "a = 3";


You're overwriting the variable. I hope you understand that?

And how can i change it that it deletes all this objects if i used this command twice?

Blackazur
22/02/2014, 08:33 PM
Anyone?

Konstantinos
22/02/2014, 08:38 PM
Delete the object and create a new one.


// global:
new trap[MAX_PLAYERS];

// OnPlayerConnect:
trap[playerid] = INVALID_OBJECT_ID;

// the command you create the object:
if (IsValidObject(trap[playerid])) DestroyObject(trap[playerid]);
trap[playerid] = CreateObject(...);

Blackazur
22/02/2014, 08:53 PM
~fixed~