SA-MP Forums Archive
Fors - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Fors (/showthread.php?tid=634115)



Fors - Ignaciodmr - 13.05.2017

Hello there, I was wondering the following:

I recently made a script which used a "for" to link some vehicles to an interior.

It was something like this:

Код:
for(new i; i >= 10; i++)
{
  LinkVehicleToInterior(rVehicles[i], 9);
  SetVehicleVirtualWorld(rVehicles[i], 8);
}
The thing is that it didn't work. The vehicles didn't link to the damn interior or the virtual world. Desperating.

When I removed the for and did it all like this
Код:
LinkVehicleToInterior(rVehicles[0], 9);
LinkVehicleToInterior(rVehicles[1], 9);
LinkVehicleToInterior(rVehicles[2], 9);
LinkVehicleToInterior(rVehicles[3], 9);

//etc.
Surprisingly it worked. Why does this happen?


Re: Fors - GoldenLion - 13.05.2017

Код:
for (new i; i >= 10; i++)
This will increment i while it's equal or more than 10 so it never gets incremented.
It has to be like this
Код:
for (new i; i <= 10; i++)



Re: Fors - Vince - 13.05.2017

Because your loop condition is wrong. It is checking if i is greater than or equal to 10 but since i is initialized to zero it never is. Thus the loop never runs. Also use sizeof instead of statically typed limits. Sizeof is an operator and has no impact on runtime performance.


Respuesta: Fors - Ignaciodmr - 13.05.2017

Aw, good to know lmao

Thanks + rep

Yeah i did use sizeof but just not to put it in the sample code