Looping through 2000+ Iterations
#1

MAX_VEHICLES is 2000

Код:
for(new v = 1; v < MAX_VEHICLES; v++)
- Works fine, but this would loop would end at 1999 iterations because 1999 is less than 2000, but 2000 is not less than 2000

Код:
for(new v = 1; v <= MAX_VEHICLES; v++)
- Would be the most convenient, but this causes script to stop for some reason

I can, however, get the loop to run through exactly 2000 iterations by not initializing the variable "v" ("v" would start at 0 instead of 1)
But this is not very convenient when working with vehicleid's because vehicleid's start at 1.

Anybody else experiencing this problem?

UPDATE: This also seems to be true for while
Reply
#2

Your script is crashing because it's trying to access array[MAX_VEHICLES] which is outside the array. I think the maximum number of vehicles is actually MAX_VEHICLES - 1 (it was in 0.2), so the first loop is correct. Initialise the variable to 1 and ignore the first element, one wasted slot out of 2000 isn't really that much of a problem.
Reply
#3

Well, the whole point of a while statement is that the statement must be true before it will run. For instance, if I have 2000 < 2000, that would return false, and thus the while statement will end.

On the other hand, there's something called a do. A do/while statement means that something will be run before checked, so there is a guaranteed run of at least one.

pawn Код:
new
    iCount = 1;

do
{
    // What do you want here?
    ++iCount;
}
while(iCount < 2000);
Reply
#4

Quote:
Originally Posted by Dabombber
Your script is crashing because it's trying to access array[MAX_VEHICLES] which is outside the array. I think the maximum number of vehicles is actually MAX_VEHICLES - 1 (it was in 0.2), so the first loop is correct. Initialise the variable to 1 and ignore the first element, one wasted slot out of 2000 isn't really that much of a problem.
I've tried using actual constants instead of MAX_VEHICLES, and it still stops the script.
But the limit does say 2000. Meaning that there could be one vehicle that would be excluded from all loops. Unless the 2000 is a lie and it is actually 1999, I smell a conspiracy...

Quote:
Originally Posted by /^We(stie|z+[e|a
r)$/ ]
On the other hand, there's something called a do. A do/while statement means that something will be run before checked, so there is a guaranteed run of at least one.

pawn Код:
new
    iCount = 1;

do
{
    // What do you want here?
    ++iCount;
}
while(iCount < 2000);
I forgot all about do, but I don't see why this can't be properly accomplished with loop.
Reply
#5

What do you mean properly? If you create an array of 2000 cells, the first cell is 0, and the last is 1999.. that is 2000 cells.
Reply
#6

Vehicle IDs start at one, so there should be 2001 cells.
Reply
#7

What do you do inside that loop?
Reply
#8

Quote:
Originally Posted by 0rb
What do you mean properly? If you create an array of 2000 cells, the first cell is 0, and the last is 1999.. that is 2000 cells.
Not if you change the operation to <=, and still start at 0, because you would stop at 1999.

As Westie said, vehicleid's start at 1.
So it would be logical to make the loop start at 1 as well, no?
Reply
#9

I suppose to keep you happy...

pawn Код:
const
  MAX_LOOP_VEH = (MAX_VEHICLES + 1);

for(new v = 1; v < MAX_LOOP_VEH; v++)
{
}
Anyone?
Reply
#10

Like I said before, the vehicle ids start at 1 and end at 1999. MAX_VEHICLES might be 2000, but the vehicle limit is 1999. If you're really worried saving a tiny bit of memory you can do

pawn Код:
new gVehicleModel[MAX_VEHICLES-1];
for(new vehicleid = 1; vehicleid < MAX_VEHICLES; vehicleid++) {
    gVehicleModel[vehicleid - 1] = GetVehicleModel(vehicleid);
}
but it's easier to just ignore the first array element

pawn Код:
new gVehicleModel[MAX_VEHICLES];
for(new vehicleid = 1; vehicleid < MAX_VEHICLES; vehicleid++) {
    gVehicleModel[vehicleid] = GetVehicleModel(vehicleid);
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)