07.01.2014, 12:58
(
Последний раз редактировалось PowerPC603; 07.01.2014 в 15:08.
)
Quote:
MAX_VEHICLES is defined as 2000 so why not to use all of them (slots/indexes) but only 1999? I don't see any point of leaving the index 0 unused.
pawn Код:
|
I just tested it by spawning a little over 2000 vehicles.
The first ID was 1, the last was 1999.
Vehicle 2000 and over that got vehicleid 65535 (Invalid vehicle id).
So, in your array, you would have index 1999 unused (because you're setting vehicle id 1999 - 1 = 1998 to "true") instead of index 0, so no difference there.
Using index 0 for vehicle id 1 is also adding to your own confusion, because if you forget somewhere you've done that you're messing up all your data or getting the wrong data from your array.
I find it much simpler to use index 1 for vehicle id 1, index 5 for vehicle id 5, and so on.
You can never get confused this way.
And who cares about 4 bytes that aren't used when you have computers with 8GB RAM or more?
Unless you declare your array with [MAX_VEHICLES - 1] as well, to save 4 bytes of memory.
pawn Код:
// This command deletes all vehicles that are spawned using /v
COMMAND:cleanupcars(playerid, params[])
{
// If the player didn't login properly, fail the command
if (APlayerData[playerid][LoggedIn] == false) return 0;
// If the player has an insufficient admin-level (he needs level 3), exit the command
if (APlayerData[playerid][AdminLevel] < 3) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}Only admins level 3 can use this command");
// If the player is on the class-selection menu, block the command
if ((GetPlayerState(playerid) == PLAYER_STATE_WASTED) || (GetPlayerState(playerid) == PLAYER_STATE_NONE)) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}You cannot use this command while using class-selection");
// Setup local variables
new CarsDeleted, Msg[128];
// Loop through all vehicles
for (new vid; vid < MAX_VEHICLES; vid++)
{
// Check if this vehicle was spawned by the /v command
if (AVehicleData[vid][SpawnedByVCommand] == true)
{
// Count the cars that have been deleted
CarsDeleted++;
// Destroy the vehicle and clear the data
DestroyVehicle(vid);
AVehicleData[vid][SpawnedByVCommand] = false;
}
}
// Let the player know how many vehicles have been cleaned up
format(Msg, 128, "{00FF00}Total number of vehicles cleaned up: {FFFF00}%i", CarsDeleted);
SendClientMessage(playerid, 0xFFFFFFFF, Msg);
// Let the server know that this was a valid command
return 1;
}
I've tested this alot and it always does what it's meant to do.
EDIT:
I've tested another thing.
Using (vehicleid - 1) instead of (vehicleid) to access your array also adds extra compiler instructions which makes your amx file larger.
I've added this to my script to check it out.
pawn Код:
new vehicleid;
if (AVehicleData[vehicleid][SpawnedByVCommand] == true)
{
}
pawn Код:
new vehicleid;
if (AVehicleData[vehicleid - 1][SpawnedByVCommand] == true)
{
}
The second code actually made my amx file larger by 5 bytes.
So reducing your array size with 1 index (saving 4 bytes) but adding code to access data properly by using index 0 (for vehicles anyway) made it 5 bytes larger.
If you access your array 100 times throughout your script by adding "- 1" to access the proper data, you're actually adding 500 bytes to your amx file, which is loaded into memory as you start your server.
You see 1 index (index 0) in your array wasted memory (only 4 bytes)?
What about the extra code that takes 500 bytes of memory? And it makes your script confusing on top of that, or even buggy if you forgot to add the "- 1" somewhere, or even access the array beyond it's limits, resulting in Array-out-of-bound errors.