Loop breaks by itself
#1

Here's my problem;
I have created a function to loop through arrays with vehicle model ID's.
I'm checkng if the vehicle matches one of the groups of models I've specified so that I can
conditionally specify whether a vehicle should be created with a certain parameter.

For example, If the vehicle is enlisted in either VEHICLES or VEHICLES_2, i might want it to be all red when It's created. I use the function right before CreateVehicle(..) function.

I've created two loops for this, in the same function. My problems are;
- Only the first loop runs, no matter if it returns 1 or nothing. This is the main problem...
- It will only move on to CreateVehicle (next function after I use IsInVehiclesList(403) for example) if i return 1

And when the loops are done, I return 0 so that I can return "no match" and know if i , for example, want to specify red color in CreateVehicle(...)
pawn Код:
new VEHICLES[4] =
{
    403,
    414,
    451,
    565
};

new VEHICLES_2[4] =
{
    420,
    438,
    437,
    485
};

IsInVehiclesList(modelid)
{
    for(new i = 0; i <= sizeof(VEHICLES); i++)
    {
        if(modelid == VEHICLES[i]){ return 1; }
    }
    // Want it to go on to the next if there's no match, but it doesn't...
    for(new i = 0; i <= sizeof(VEHICLES_2); i++)
    {
        if(modelid == VEHICLES_2[i]){ return 1; }
    }
    return 0;
}
Reply
#2

I made some debug print(..)
The first image shows what it does if there's no match in the first loop, it is supposed to say "After IsInVehiclesList" when Both of the loops are done.


Reply
#3

You are looping one past the end of the vehicle arrays.

Incorrect:
Код:
for(new i = 0; i <= sizeof(VEHICLES); i++)
Correct:
Код:
for(new i = 0; i < sizeof(VEHICLES); i++)
Use < (less than).
Reply
#4

You have an array out of bounds error. Your loops will try to access index 4, which does not exist. When using sizeof in a loop the right operator to use is nearly always < instead of <=.

Edit: ^ beat me.
Reply
#5

Thank you both, I forgot about that! (It works now!)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)