Explain this loop please.
#1

I've been looking at this code for a while now but couldn't make sense of it.

Код:
	     
new CreatedCars[MAX_VEHICLES] = {INVALID_VEHICLE_ID, ...};


   new currentVehicle = GetPlayerVehicleID(playerid);
            if(currentVehicle == 0) return SendClientMessage(playerid,COLOR_GREY, "You must be in a vehicle to destroy it.");
            new check;
            for(new i = 0; i < sizeof(CreatedCars); i++)
			{
				if(CreatedCars[i] == currentVehicle)
				{
				    check = 1;
				    CreatedCars[i] = INVALID_VEHICLE_ID;
				    break;
				}
			}
			if(!check) return SendClientMessage(playerid, COLOR_GRAD1, "You may only destroy a vehicle that was created with /veh.");

	        gDestroyVehicle[currentVehicle] = 1;
			SetVehicleToRespawn(currentVehicle);
	        return 1;
		}
Код:
  

           new CreatedCars[MAX_VEHICLES] = {INVALID_VEHICLE_ID, ...};        

            new check;
            for(new i = 0; i < sizeof(CreatedCars); i++)
			{
				if(CreatedCars[i] == currentVehicle)
				{
				    check = 1;
				    CreatedCars[i] = INVALID_VEHICLE_ID;
				    break;
				}
			}
^ Cut out the loop for clarity.

What's the loop for? Does it check for the created cars and the invalid vehicle id's? I am really not sure what's that loop supposed to do.

PS: This is a command to destroy a spawned vehicle.
Reply
#2

The loop seems to be checking through the 'created vehicles' array until it finds a value in there matching the player's vehicle ID. (Checked by this line of code:)

PHP код:
if(CreatedCars[i] == currentVehicle
Lets say it gets to CreatedCars[10], which contains vehicle ID 123. The player's vehicle ID is also 123.

When it finds the player's vehicle ID, it would be setting that value (CreatedCars[10]) to INVALID_VEHICLE_ID, in this line:

PHP код:
CreatedCars[i] = INVALID_VEHICLE_ID
Because the vehicle is about to be destroyed. So the vehicle ID will no longer be valid until someone re-spawns a vehicle afterward.

The line:

PHP код:
check 1
Is used for the next 'IF' statement to decide if the vehicle check was successful or not.
Reply
#3

if(!check) return SendClientMessage(playerid, COLOR_GRAD1, "You may only destroy a vehicle that was created with /veh.");

It is obvious what it "checks", isn't it ? It even provides a msg if check fails.
Reply
#4

Quote:
Originally Posted by Nate4
Посмотреть сообщение
The loop seems to be checking through the 'created vehicles' array until it finds a value in there matching the player's vehicle ID. (Checked by this line of code

PHP код:
if(CreatedCars[i] == currentVehicle
Lets say it gets to CreatedCars[10], which contains vehicle ID 123. The player's vehicle ID is also 123.

When it finds the player's vehicle ID, it would be setting that value (CreatedCars[10]) to INVALID_VEHICLE_ID, in this line:

PHP код:
CreatedCars[i] = INVALID_VEHICLE_ID
Because the vehicle is about to be destroyed. So the vehicle ID will no longer be valid until someone re-spawns a vehicle afterward.

The line:

PHP код:
check 1
Is used for the next 'IF' statement to decide if the vehicle check was successful or not.
Thanks! That was great help, appreciate it.


Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
if(!check) return SendClientMessage(playerid, COLOR_GRAD1, "You may only destroy a vehicle that was created with /veh.");

It is obvious what it "checks", isn't it ? It even provides a msg if check fails.
Uh.. I was talking about the loop..?
Reply
#5

The loop just sets the array-index where the current vehicle is stored to INVALID.
But I don't see code to actually destroy the vehicle.
It only gets respawned, not destroyed.

I don't get why some people add vehicle ID's as a value to an array.
And to find the vehicle, they have to loop the entire array to find it.

Just setup an array with size MAX_VEHICLES and use the vehicle-id directly as index for the array.
Much simpler.

PHP код:
// Setup enum and array
enum TVehicle
{
    
SpawnedWithVeh,
    
Float:Fuel
}
new 
Vehicles[MAX_VEHICLES][TVehicle];
// To destroy the player's vehicle, reset all data about the vehicle and destroy it, using the vehicleid as index for the array
new currentvehicle GetPlayerVehicleID(playerid);
if (
Vehicles[currentvehicle][SpawnedWithVeh] == 1)
{
    
// Reset all data
    
Vehicles[currentvehicle][SpawnedWithVeh] = 0;
    
Vehicles[currentvehicle][Fuel] = 0.0;
    
// Destroy the vehicle
    
DestroyVehicle(currentvehicle);

Reply
#6

Quote:
Originally Posted by Black Axe
Посмотреть сообщение
Uh.. I was talking about the loop..?
Yup, that line I put there is the whole reason that loop exists, as it says there, you can only delete /veh created vehicles with this cmd, so the loop checks if it's a veh created vehicle or not.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)