Need help with AdminVehicle Spawn and Delete
#1

Hello Community

Right now im making a veh but don't want them to remain on the server so i wanna make a delete command

Now i now that i need a variable to save the spawned cars in like "new adminCar[MAX_VEHICLE];"

so but how can i build my veh command.

That what i have so far

Код:
dcmd_veh(playerid,params[])
{
	if(isPlayerAnAdmin(playerid,1))
	{
		new vehid, color1, color2;
		
		if(sscanf(params,"iii",vehid,color1,color2)) SendClientMessage(playerid,Color_Info,"Usage: /veh <vehid> <color1> <color2>");
		else if(vehid == INVALID_VEHICLE_ID) SendClientMessage(playerid, Color_Error,"Falsche VehicleId");
		else 
		{
			if(vehid >400 || vehid < 611)
			{
				new Float:x,Float:y,Float:z;
				GetPlayerPos(playerid,x,y,z);
				
				new cCar = CreateVehicle(vehid, x, y, z, 90, color1, color2, -1);	// That the part i dont know what to do	
				
				
				PutPlayerInVehicle(playerid,cCar,0);
			}
			
		}
	}
	else 
	{
		SendClientMessage(playerid,Color_Error,"Du bist kein Admin!");
		return 1;
	}
	return 1;
}
Would be nice if you can help me
Reply
#2

CreateVehicle returns the ID of the vehicle that has been created. Here you're storing the vehicle ID on a static variable since it's no longer used anywhere else. Store it on a global array so that it can be used elsewhere.
pawn Код:
//Initialize a global array containing created vehicle Ids.
new
    g_VehicleIds[MAX_VEHICLES] = {INVALID_VEHICLE_ID, ...} //INVALID_VEHICLE_ID is their default value.
;

//your create vehicle command:
//new cCar = CreateVehicle(vehid, x, y, z, 90, color1, color2, -1);
//Instead of the above, you loop g_VehicleIds and use the free slot it got.
new i; //We need 'i' to be used even after loop, so declare it before looping.
for(i = 0; i< MAX_VEHICLES; i++) {

    if(g_VehicleIds[i] == INVALID_VEHICLE_ID) { //We got an unused slot, then:
   
        g_VehicleIds[i] = CreateVehicle(vehid, x, y, z, 90, color1, color2, -1);
        break;
    }
    //On places where you use 'cCar', use g_VehicleIds[i]
So then, your destroying command can use g_VehicleIds array to destroy. The way of destroying is up to you, but here's an example by using vehicle ID as parameter.
pawn Код:
CMD:destroyvehicle(playerid, params[]) {

    if(!IsPlayerAdmin(playerid))
        return 0;
    new
        temp_VehicleID;

    if(sscanf(params, "i", temp_VehicleID))
        return SendClientMessage(playerid, 0xFF0000FF, "USAGE : /destroyvehicle [vehicleid]");
   
    for(new i = 0; i< MAX_VEHICLES; i++) { //Loop through vehicle ids.

        if(g_VehicleIds[i] == temp_VehicleID)) { //If the given vehicle ID matches with one among the list.

            DestroyVehicle(g_VehicleIds[i]); //Destroy vehicle.
            g_VehicleIds[i] = INVALID_VEHICLE_ID; //It's VERY IMPORTANT to reset the value of that index!
            SendClientMessage(playerid, -1, "Vehicle has been destroyed successfully!");
            return 1;
        }
    }
    //If your code reaches here, it means your parameter didn't match with one created or it isn't created.
    SendClientMessage(playerid, -1, "ERROR : Failed destroying the given vehicle ID.");
    return 1;
}
I strongly recommend you to use any other command processors than dcmd since it's deprecated. It can be slow if you have a lot of commands. zcmd was commonly used, I believe there are new ones faster than zcmd.
Reply
#3

Thanks that works great thread can be closed.
Reply
#4

Quote:
Originally Posted by Lordzy
Посмотреть сообщение
pawn Код:
for(new i = 0; i< MAX_VEHICLES; i++)
Vehicle ID's start from 1 and there's GetVehiclePoolSize so a better loop would be
Код:
for (new i = 1, j = GetVehiclePoolSize(); i <= j; i++) if (IsValidVehicle(i))
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)