SA-MP Forums Archive
Destroy attached objects when deleting a car - 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: Destroy attached objects when deleting a car (/showthread.php?tid=440990)



Destroy attached objects when deleting a car - [..MonTaNa..] - 01.06.2013

Removed.


Re: Destroy attached objects when deleting a car - Scenario - 01.06.2013

Of course there's a solution, there's almost always a solution!

You need to create a variable that stores the object ID's of the objects attached to the vehicle. When you destroy the vehicle, run through the variable and delete any object's associated with it. You can do this automatically by hooking the DestroyVehicle function with ALS.


Re: Destroy attached objects when deleting a car - [..MonTaNa..] - 01.06.2013

Removed.


Re: Destroy attached objects when deleting a car - Scenario - 01.06.2013

I did this using a foreach iterator, it seemed like the most logical option to me! So, you'll need to download the latest version of the YSI library and include YSI\y_iterate.

pawn Код:
new
    Iterator:VehicleAttachedObjects[MAX_VEHICLES]<MAX_OBJECTS>; // you MAY need to increase MAX_OBJECTS if you're using a streamer to avoid the limit
   
public OnGameModeInit()
{
    Iter_Init(VehicleAttachedObjects);
    return 1;
}

CMD:siren(playerid, params)
{
    new
        v = GetPlayerVehicleID(playerid),
        tempID;

    Iter_Add(VehicleAttachedObjects[v], tempID = CreateDynamicObject(19419, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
    AttachDynamicObjectToVehicle(tempID, v, 0.000000, -1.349998, 0.354999, 0.000000, 0.000000, 0.000000); // sultan (light bar)
    Iter_Add(VehicleAttachedObjects[v], tempID = CreateDynamicObject(18646, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
    AttachDynamicObjectToVehicle(tempID, v, 0.000000, 0.789999, 0.439999, 0.000000, 0.000000, 0.000000); // Sultan (single light)
    return 1;
}

// when you want to remove an object, you'll need to use Iter_Remove(VehicleAttachedObjects[vehicleid], OBJECT_ID);

public OnVehicleDeath(vehicleid) // removes the objects when the vehicle dies (i.e. enters water, blows up)
{
    foreach(new i : VehicleAttachedObjects[vehicleid]) DestroyDynamicObject[i];
    Iter_Clear(VehicleAttachedObjects[vehicleid]);
    return 1;
}

// ------ DestroyVehicle hook ------ //

stock realDestroyVehicle(vehicleid)
{
    foreach(new i : VehicleAttachedObjects[vehicleid]) DestroyDynamicObject(i);
    Iter_Clear(VehicleAttachedObjects[vehicleid]);
   
    return DestroyVehicle(vehicleid);
}

#if defined _ALS_DestroyVehicle
    #undef DestroyVehicle
#else
    #define _ALS_DestroyVehicle
#endif
#define DestroyVehicle realDestroyVehicle



Re: Destroy attached objects when deleting a car - [..MonTaNa..] - 01.06.2013

Removed.


Re: Destroy attached objects when deleting a car - Scenario - 01.06.2013

You'll never learn if you don't do it yourself...