Loop issue
#1

This command is used when admin wants to sell Car Ownership car and for that he needs the car ID.

pawn Код:
if(strcmp(cmd, "/getcarid", true) == 0)
{
    if(IsPlayerConnected(playerid))
    {
        if(PlayerInfo[playerid][pAdmin] >= 4)
        {
            for(new c = 0; c < sizeof(CarInfo); c++)
            {
                new Float:x,Float:y,Float:z;
                new carid = CarInfo[c][cID]; //the car ID in AddStaticVehicle
                GetVehiclePos(carid, x, y, z);
                if(PlayerToPoint(3, playerid, x, y, z))
                {
                    format(string, sizeof(string), "Car ID: %d", c);
                    SendClientMessage(playerid, COLOR_WHITE, string);
                    return 1;
                }
                else
                {
                    SendClientMessage(playerid, COLOR_RED, "You are not near any Car Ownership vehicle.");
                    return 1;
                }
            }
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "Only admins with Adm Level 4 can use this command.");
            return 1;
        }
    }
    return 1;
}
But the problem is that it only works for car that is ID 0. When I change the loop to
pawn Код:
for(new c = 1; c < sizeof(CarInfo); c++)
Than it works for car id 1
pawn Код:
for(new c = 2; c < sizeof(CarInfo); c++)
For car id 2 and so on....

_________________________________________________

This CarInfo[c][cID] is used in here under OnGameModeInIt

pawn Код:
for(new c = 0; c < sizeof(CarInfo); c++)
{
  CarInfo[c][cID] = AddStaticVehicleEx(CarInfo[c][cModel], CarInfo[c][cLocationx], CarInfo[c][cLocationy], CarInfo[c][cLocationz], CarInfo[c][cAngle], CarInfo[c][cColor1], CarInfo[c][cColor2], -1);
}
Basicly it works perfectly for car id 0 but not for the other so the problem is in loop I think...
Any ideas what is the problem?

Tnx
Reply
#2

pawn Код:
if(strcmp(cmd, "/getcarid", true) == 0)
{
    if(IsPlayerConnected(playerid))
    {
        if(PlayerInfo[playerid][pAdmin] >= 4)
        {
            new Float:x,Float:y,Float:z;
            for(new c = 0; c < sizeof(CarInfo); c++)
            {
                GetVehiclePos(CarInfo[c][cID], x, y, z);
                if(PlayerToPoint(3, playerid, x, y, z))
                {
                    format(string, sizeof(string), "Car ID: %d", CarInfo[c][cID]);
                    SendClientMessage(playerid, COLOR_WHITE, string);
                    return 1;
                }
            }
            SendClientMessage(playerid, COLOR_RED, "You are not near any Car Ownership vehicle.");
            return 1;
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "Only admins with Adm Level 4 can use this command.");
            return 1;
        }
    }
    return 1;
}
The problem was that you used return even if the player wasn't near to the vehicle, it made the loop stop in the first loop. I moved that return outside the loop, so it says that message AFTER it has gone through the whole loop.

Also I moved floats x, y and z outside the loop, so it won't create them again and again. Saves time.
Reply
#3

Yea I just figure that out. I was looking at /edit command in God Father mode and I saw that there was no return, so I tried it and it worked. Thx anyway for help and explanation about the problem.
Reply
#4

Why don't you GetPlayerPos BEFORE the loop, instead of each loop iteration? That is very inefficient.
Reply
#5

Quote:
Originally Posted by 0rb
Why don't you GetPlayerPos BEFORE the loop, instead of each loop iteration? That is very inefficient.
What should I put here GetVehiclePos(CarInfo[c][cID], x, y, z);
Reply
#6

GetPlayerPos, not GetVehiclePos. You GetPlayerPos each iteration of the loop, but the player won't have the time to move during loop execution..so it's pointless to get his pos again and again.

And yeah ok sorry for that, you don't see GetPlayerPos, that is because it's used by the function PlayerToPoint.

GetPlayerPos before the loop, then use a simple distance function inside the loop.

ex:
pawn Код:
new Float:px, Float:py, Float:pz, Float:cx, Float:cy, Float:cz;
GetPlayerPos(playerid, px, py, pz);
GetVehiclePos(CarInfo[c][cID], cx, cy, cz);

for (blablabla)
  if (distance(px, py, pz, cx, cy, cz) < 3.0)
Reply
#7

never mind I get it.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)