06.05.2015, 11:51
Hey! Interesting question.
This is indeed a very good way, but can be improved to use maximally ONE function call to determine whether a vehicle is valid and streamed in for the player. Since if a vehicle is streamed in for someone, it is definitely valid, the IsValidVehicle check can be omitted as it is done internally anyways (this was not the case in SA-MP 0.1 with some natives and the PAWN programmer had to implement some extra sanity checks, though).
This is a situation similar to:
which can be simplified to
------
Coming back to the original problem, the OP has already found a viable solution by running the code infrequently, so the algorithm won't be a problem even if it is slow.
If you desire to make the 3-second interval smaller and allow the player to quickly open-close the trunk, you could, for example, keep information about vehicles close to them somewhere and recalculate that data after some interval.
If you were to do it in PAWN, it would indeed be an array of MxN (M - number of players, N - number of vehicles) cells. I believe one cell is 4 bytes (32 bits), so the array would take up 400000 cells in standard situations (200 max players with 2000 vehicles) and up to 1562 kilobytes. This is of no importance in terms of the computer memory as the heap has times more space available, but it will also make your AMX large and slow down compilation.
So it is without doubt that a standard PAWN array will not do. Even though a char array provides some relief, it would still be a pain and not very well scalable. So you'd have to look into some dynamic method of storage.
From C++, I can think of std::bitset, which would very much be your friend in such situation. For 200 players, having a bitset for 2000 vehicles would take up 400000 bits or 48 kilobytes, which is far less.
Some of my data may be a little off, it has been a while since I last did programming in PAWN.
Quote:
In my experience, this is the best way:
pawn Код:
|
pawn Код:
for(new i = 0, j = GetVehiclePoolSize(); i != j; i++)
{
if(!IsVehicleStreamedIn(i, playerid)) {
continue;
}
// code
}
pawn Код:
if(IsPlayerInAnyVehicle(playerid))
{
new vID = GetPlayerVehicleID(playerid);
// code
}
pawn Код:
new vID;
if((vID = GetPlayerVehicleID(playerid)) != 0) {
// code
}
Coming back to the original problem, the OP has already found a viable solution by running the code infrequently, so the algorithm won't be a problem even if it is slow.
If you desire to make the 3-second interval smaller and allow the player to quickly open-close the trunk, you could, for example, keep information about vehicles close to them somewhere and recalculate that data after some interval.
If you were to do it in PAWN, it would indeed be an array of MxN (M - number of players, N - number of vehicles) cells. I believe one cell is 4 bytes (32 bits), so the array would take up 400000 cells in standard situations (200 max players with 2000 vehicles) and up to 1562 kilobytes. This is of no importance in terms of the computer memory as the heap has times more space available, but it will also make your AMX large and slow down compilation.
So it is without doubt that a standard PAWN array will not do. Even though a char array provides some relief, it would still be a pain and not very well scalable. So you'd have to look into some dynamic method of storage.
From C++, I can think of std::bitset, which would very much be your friend in such situation. For 200 players, having a bitset for 2000 vehicles would take up 400000 bits or 48 kilobytes, which is far less.
Some of my data may be a little off, it has been a while since I last did programming in PAWN.