Sizeof 2D array
#1

I'm trying to loop through vehicles of the players job and unlocking them, so they're only locked for people without that job, right now i'm doing this:
Код:
// Garbage Truck Driver

JobVehicle[1][0] = AddStaticVehicleEx(408, 2182.3635, -1992.1925, 14.1022, 359.7592, 1, 1, 180000); // Trashmaster
JobVehicle[1][1] = AddStaticVehicleEx(408, 2187.0657, -1993.5066, 14.0923, 359.9882, 1, 1, 180000); // Trashmaster
JobVehicle[1][2] = AddStaticVehicleEx(408, 2192.1223, -1994.7883, 14.0956, 0.0472, 1, 1, 180000); // Trashmaster
JobVehicle[1][3] = AddStaticVehicleEx(408, 2165.1445, -1972.4277, 14.1021, 179.3826, 1, 1, 180000); // Trashmaster

// Delivery Driver

JobVehicle[2][0] = AddStaticVehicleEx(499, 2486.5935, -2115.5938, 13.5393, 359.8707, 1, 1, 180000); // Benson
JobVehicle[2][1] = AddStaticVehicleEx(499, 2481.7615, -2115.6641, 13.5385, 356.5260, 1, 1, 180000); // Benson
JobVehicle[2][2] = AddStaticVehicleEx(456, 2476.6794, -2114.4895, 13.7211, 1.6723, 1, 1, 180000); // Yankee
Код:
for(new v = 0; v < sizeof(JobVehicle[PlayerInfo[playerid][Job]]); v ++)
{
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0, 0);
}
But the loop gives errors:
Quote:

(2731) : error 001: expected token: "]", but found "-integer value-"
(2731) : error 029: invalid expression, assumed zero
(2731) : error 029: invalid expression, assumed zero
(2731) : fatal error 107: too many error messages on one line

So what is the proper way of doing this?
Reply
#2

for(new v = 0; v < sizeof(JobVehicle[PlayerInfo[playerid][Job]]))
Reply
#3

Try this:

pawn Код:
for(new v=0; v <= sizeof(JobVehicle[PlayerInfo[playerid][pJob]]) v++) {
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0 0);
}
You forgot to to create the actual loop by adding "v++" to it. I also added "<=" as the first vehicle in the array else wouldn't be affected by the loop (if i'm not mistaken. Was a long time ago I did programming).
Reply
#4

Quote:
Originally Posted by introzen
Посмотреть сообщение
Try this:

pawn Код:
for(new v=0; v <= sizeof(JobVehicle[PlayerInfo[playerid][pJob]]) v++) {
    if(IsValidVehicle(v)) SetVehicleParamsForPlayer(v, playerid, 0 0);
}
You forgot to to create the actual loop by adding "v++" to it. I also added "<=" as the first vehicle in the array else wouldn't be affected by the loop (if i'm not mistaken. Was a long time ago I did programming).
Sorry i forgot to include that in the post, edited.
Still the same errors.
Reply
#5

An array only has one size which is determined at compile time. Therefore you can't use an index. Indices that aren't explicitly assigned a value are implicitly initialized to 0. You can use:
PHP код:
sizeof(JobVehicle
PHP код:
sizeof(JobVehicle[]) 
To get the size of the first and second dimension, respectively.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
An array only has one size which is determined at compile time. Therefore you can't use an index. Indices that aren't explicitly assigned a value are implicitly initialized to 0. You can use:
PHP код:
sizeof(JobVehicle
PHP код:
sizeof(JobVehicle[]) 
To get the size of the first and second dimension, respectively.
According to this information (which I didn't know appearently, thank you btw) would this work perhaps?

pawn Код:
for(new v=0; v <= sizeof(JobVehicle[]) v++) {
    if(IsValidVehicle(v) && JobVehicle[] == PlayerInfo[playerid][pJob]) SetVehicleParamsForPlayer(v, playerid, 0 0);
}
Reply
#7

You can use indices inside the loop, just not in combination with the sizeof operator itself. Which means what is likely desired is:
PHP код:
for(new 0sizeof(JobVehicle[]); v++) {
    
SetVehicleParamsForPlayer(JobVehicle[PlayerInfo[playerid][pJob]][v], playerid00);

Assuming that 0 <= PlayerInfo[playerid][pJob] < sizeof JobVehicle.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)