How to destroy only 1 closest object?
#1

I dont know how to make a script where by pressing a specially key and when player standing in distance of 1.5 an one object will be destroyed. I created a script where any object which is detected in distance 1.5 will disappear, but I need to destroy only 1 object from 2 which are near.

It would be fine also when 1 random object from 2 other in mentioned distance will be destroyed.

pawn Код:
//--------------------------------------------------------------------------------------------------------------
new PastaCans[6];

public OnFilterScriptInit()
{
    PastaCans[0] = CreateObject(1455, -1335.14, 2524.24, 86.12,   0.00, 0.00, 0.00);
    PastaCans[1] = CreateObject(1455, -1335.47, 2524.37, 86.12,   0.00, 0.00, 171.00);
    PastaCans[2] = CreateObject(1455, -1335.38, 2524.28, 86.12,   0.00, 0.00, 18.00);
    PastaCans[3] = CreateObject(1455, -1313.31, 2541.69, 86.81,   0.00, 0.00, 0.00);
    PastaCans[4] = CreateObject(1455, -1316.28, 2540.80, 86.81,   0.00, 0.00, 171.00);
    PastaCans[5] = CreateObject(1455, -1316.69, 2540.90, 86.81,   0.00, 0.00, 84.00);
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{

    if (newkeys & KEY_SECONDARY_ATTACK)
    {
    new Float:PastaCanX, Float:PastaCanY, Float:PastaCanZ;

        for(new PastaCansID=0; PastaCansID < sizeof(PastaCans); PastaCansID++)
        {
        GetObjectPos(PastaCans[PastaCansID], PastaCanX, PastaCanY, PastaCanZ);

            if(IsPlayerInRangeOfPoint(playerid, 1.5, PastaCanX, PastaCanY, PastaCanZ))
            {
            DestroyObject(PastaCans[PastaCansID]); // PROBLEM WITH AMOUNT OF DESTROYED OBJECTS
            }

        }

    }
    return 1;
}
//--------------------------------------------------------------------------------------------------------------
Help me, please...
Reply
#2

Hello.

So, the requirements of this piece of code will be:
Код:
When the player presses a specific key
    And there is an object within 1.5 distance from the player
        Destroy the closest object.
You already have most of the code, you just need a way to work out what object is closest to the player.

One way to do this is to use the for loop you've created.
Each time the loop finds an object within 1.5 distance of the player, it could also check to see if this is the closest object to the player.
You could create a variable, for example object_distance, to store the distance of the closest object to the player, and object_id for the ID of the closest object.

The first time the loop finds an object within 1.5 distance of the player, it could set the variable object_distance to the distance between the player and the object, and object_id to the object's ID.
Then, whenever it finds a new object within 1.5 distance, it should then compare the distance between the player and this new object to object_distance, to see if this new object is closer than the previous closest object.
If the new object is closer, it should then take this new object's distance and ID, then assign them to our two new object variables above.

So your code now looks like this:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys){
    if (newkeys & KEY_SECONDARY_ATTACK){
        new Float:PastaCanX, Float:PastaCanY, Float:PastaCanZ;
        new Float:object_distance, object_id = -1;//Used for working out the closest object to the player
        //We set object_id to -1 so we can tell during the loop if an object within 1.5 distance has not yet been found
        for(new PastaCansID=0; PastaCansID < sizeof(PastaCans); PastaCansID++)
        {
            GetObjectPos(PastaCans[PastaCansID], PastaCanX, PastaCanY, PastaCanZ);
            if(IsPlayerInRangeOfPoint(playerid, 1.5, PastaCanX, PastaCanY, PastaCanZ))
            {
                new Float:tempdist = GetPlayerDistanceFromPoint(playerid,PastaCanX, PastaCanY, PastaCanZ);
                //Assign the distance between the player and the new object to "tempdist"
                if(object_id == -1 || tempdist < object_distance){
                    //If there is not yet a closest object, or if it is closer than the previous closest object
                    object_id = PastaCansID;
                    object_distance = tempdist;
                    //Update the ID and distance
                }
            }
        }
        if(object_id != -1){//If an object has been found within 1.5 distance
            DestroyObject(PastaCans[object_id]);
        }
        //We remove the object here after the loop has finished, and the closest object has been found.
    }
    return 1;
}
I also indented it to make it easier to read. Read my signature for how to use PAWN tags in forum posts.

If you have any questions, feel free to ask.
Reply
#3

Thanks, you helped me. It works.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)