SA-MP Forums Archive
Destroying a certain object - 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: Destroying a certain object (/showthread.php?tid=416177)



Destroying a certain object - dusk - 16.02.2013

Hello,im trying to create a drug system. The point of it is it creates a global object after player /plant it.And the command /harvest should destroy it and give drugs.But it doesn't destroy it,nor give drugs. Here's the command:
pawn Code:
CMD:harvest(playerid,params[])
{
    new Float:x,Float:y,Float:z;
    GetObjectPos(3409,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,5,x,y,z))
    {
        new rand = random(5);
        DestroyObject(3409);
        PlayerInfo[playerid][Marijuana]=PlayerInfo[playerid][Marijuana] + rand;
        SaveStats(playerid);
    }
    return 1;
}



Re: Destroying a certain object - DaRk_RaiN - 16.02.2013

Then the object ID is a fail,try to define the Object ID instead of putting the actual ID
E.G:
pawn Code:
new DrugObject;
pawn Code:
CMD:plant(playerid,params[])
{
//bla bla the code
DrugObject = CreateObjet(//the object
return 1;
}
Then the harvest becomes
pawn Code:
CMD:harvest(playerid,params[])
{
    new Float:x,Float:y,Float:z;
    GetObjectPos(3409,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,5,x,y,z))
    {
        new rand = random(5);
        DestroyObject(DrugObject);
        PlayerInfo[playerid][Marijuana]=PlayerInfo[playerid][Marijuana] + rand;
        SaveStats(playerid);
    }
    return 1;
}



Re: Destroying a certain object - dusk - 16.02.2013

i thought that might be the problem. But in this case, wont everyones objects get destroyed after one player types it?


Re: Destroying a certain object - DaRk_RaiN - 16.02.2013

Then use a player variable
pawn Code:
new DrugObject[MAX_PLAYERS];
pawn Code:
CMD:plant(playerid,params[])
{
//bla bla the code
DrugObject[playerid] = CreateObjet(//the object
return 1;
}
pawn Code:
CMD:harvest(playerid,params[])
{
    new Float:x,Float:y,Float:z;
    GetObjectPos(DrugObject[playerid],x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,5,x,y,z))
    {
        new rand = random(5);
        DestroyObject(DrugObject[playerid]);
        PlayerInfo[playerid][Marijuana]=PlayerInfo[playerid][Marijuana] + rand;
        SaveStats(playerid);
    }
    return 1;
}



Re: Destroying a certain object - dusk - 16.02.2013

Thanks for that BUT, what if i plant multiple trees? then i can only harvest the last one planted.


Re: Destroying a certain object - DaRk_RaiN - 16.02.2013

Then make an array
pawn Code:
new DrugObject[MAX_PLAYERS][10];
Then it will be
pawn Code:
CMD:plant(playerid,params[])
{
//bla bla the code
DrugObject[playerid][0] = CreateObjet(//the object
DrugObject[playerid][1] = CreateObjet(//the object2
return 1;
}



Re: Destroying a certain object - dusk - 16.02.2013

I'm still able only to destroy the last planted


Re: Destroying a certain object - DaRk_RaiN - 16.02.2013

You want to destroy all planted weed at the same time?


Re: Destroying a certain object - dusk - 16.02.2013

I want to destroy the that im near.

edit: the question would be,how do i get the position of a certain drug plant. Because GetObjectPos and then IsPlayerInRangeOfPoint is pretty annoying.Or is there no other way?


Re: Destroying a certain object - Babul - 16.02.2013

instead assigning multiple plantids to each playerid[][], why not assign ONE playerid to any planted seed?
instead of:
Code:
PlantID[playerid][0]=CreateObject(blabla..
...with the issue of multiple plants (those [0], [1] etc in the player-plant array), you can do this:
Code:
new PlantID=CreateObject(blabla..
PlantedByPlayerID[PlantID]=playerid;
pay attention to the new PlantID, the "new" is there for a reason. the (returned) PlantID is only required once: as pointer into the PlantedByPlayerID cell.
doing it this way, you dont need to worry about checking if a player plant [0], [1], [2], (or more plant-slots) are free for planting.
the only little downside is the requirement for a sort-of-big array for holding like 10000 playerid's, pointed by each single plant.
to avoid the need for checking 10000 plants to find the closest plant to a player, you can spawn a pickup below each plant, and store the PlantID (same as the returned value above) into the pickup array the same way.

if you're done, your plant script can:
-handle (almost) unlimited amount of plants (per player too, since they all share the same plants array),
-avoid loops and timers. no closest plant-check, no counting for free plantslots etc.

trading a few KBytes RAM (10000 plant-integer-object-ids plus the same for invisible pickups linking to the plants) for using no loops&timers at all, seems to be a good deal. the more plants you intent to use, the more speedup advantages over other plant scripts you'll notice. for free.