Pawn can't handle 2 loops?
#1

Hey there everybody,
I have just found a kinda wierd problem, it seems like pawn can't handle 2 for-loops in 1 callback.
Like:
Код:
OnGameModeInit()
{
for(new i; i <= vehicles; i++)
{
AddStaticVehicle(model, X, Y*5, Z, A, 1, 0);
}

for(new i; i <= vehicles2; i++)
{
AddStaticVehicle(model, X, Y*5, Z, A, 1, 0);
}
return 1;
}
This is just an example, not my real code!
With this code only the first loop wel be executed.

Anyone here that knows a solution for this?
Reply
#2

It can, you are probably doing something wrong, can you post your actual code?

Also, here is a way to show it:

pawn Код:
for(new i; i <= 10; i++)
{
    print("Loop1");
}
for(new s; s <= 10; s++)
{
    print("Loop2");
}
It will print Loop1 10 times and Loop2 10 times.
Reply
#3

Код:
OnGameModeInit()
{
for(new i=0; i <= vehicles; i++)
{
AddStaticVehicle(model, X, Y*5, Z, A, 1, 0);
}

for(new i=0; i <= vehicles2; i++)
{
AddStaticVehicle(model, X, Y*5, Z, A, 1, 0);
}
return 1;
}
If vehicles and vehicles2 is set to a proper value both loops will be executed and in afterwards OnGameModeInit will return 1.
Reply
#4

for me it won't, anyways here is my code.
pawn Код:
public OnPlayerPickUpPickup(playerid, pickupid)
{
    for(new i; i <= FACTIONS; i++)
    {
      if(pickupid == gFPickup[i]) //if the pickup is the one from the faction base
      {
        new string[256], fname[128];
        format(fname, sizeof(fname), "/factions/%i", i);
        string = dini_Get(fname, "name");
        format(string, sizeof(string), "%s base", string);
        GameTextForPlayer(playerid, string, 2000, 0);
        SetPlayerPos(playerid, dini_Float(fname, "intX"), dini_Float(fname, "intY")+5, dini_Float(fname, "intZ"));
        SetPlayerInterior(playerid, dini_Int(fname, "intid"));
      }
      else continue; //if not, continue the loop
    }

    for(new j; j <= FACTIONS; j++)
    {
        if(pickupid == gFIntPickup[j])
        {
          SendClientMessage(playerid, COLOR_BLUE, "Aren't you outside yet?!");
          new fname[128];
          format(fname, sizeof(fname), "/factions/%i", j);
            SetPlayerInterior(playerid, 0);
            SetPlayerPos(playerid, dini_Float(fname, "X"), dini_Float(fname, "Y"), dini_Float(fname, "Z"));
        }
        else continue;
    }
  return 1;
}
Reply
#5

Just try it out by using the example TheKiller gave you - it works just fine:

Код:
for(new i=0; i <= 10; i++)
{
	print("Loop1");
}
for(new s=0; s <= 10; s++)
{
	print("Loop2");
}
Reply
#6

hmm thats wierd, cuz mine doesn't
Reply
#7

it's probably because you're using <= instead of <, so..

Код:
for(new i=0; i <= 10; i++)
{
	print("Loop1");
}
for(new s=0; s <= 10; s++)
{
	print("Loop2");
}
the code above will print loop1 and loop2 eleven times. (0, 1, ..., 9, 10)

if you used < instead, it would perform the loop from 0 to 9

to your problem, it's probably:
Код:
if(pickupid == gFPickup[i])
imagine this would do gFPickup[FACTIONS], if FACTIONS would be 10, and you defined gFPickup like this:
Код:
new gFPickup[FACTIONS];
the code would abort at that point


Hope you understand that, I'm kinda tired
Reply
#8

actually i did define it as
Код:
new gFPickup[FACTIONS];
But i dont fully understand you, you mean that if the for-loop reaches his end that the whole callback is stopped?
Reply
#9

Could be true what MaVe mentioned. Try altering your code to:

Код:
public OnPlayerPickUpPickup(playerid, pickupid)
{
	for(new i; i < FACTIONS; i++)
	{
	  if(pickupid == gFPickup[i]) //if the pickup is the one from the faction base
	  {
	  	new string[256], fname[128];
	  	format(fname, sizeof(fname), "/factions/%i", i);
	  	string = dini_Get(fname, "name");
	  	format(string, sizeof(string), "%s base", string);
	  	GameTextForPlayer(playerid, string, 2000, 0);
	  	SetPlayerPos(playerid, dini_Float(fname, "intX"), dini_Float(fname, "intY")+5, dini_Float(fname, "intZ"));
	  	SetPlayerInterior(playerid, dini_Int(fname, "intid"));
	  }
	  else continue; //if not, continue the loop
	}

	for(new j; j < FACTIONS; j++)
	{
		if(pickupid == gFIntPickup[j])
		{
		  SendClientMessage(playerid, COLOR_BLUE, "Aren't you outside yet?!");
		  new fname[128];
		  format(fname, sizeof(fname), "/factions/%i", j);
			SetPlayerInterior(playerid, 0);
			SetPlayerPos(playerid, dini_Float(fname, "X"), dini_Float(fname, "Y"), dini_Float(fname, "Z"));
		}
		else continue;
	}
  return 1;
}
(( Just "<=" changed to "<" ))

Try that out!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)