'Looping' Problem!
#1

Please somebody tell me why this problem happens, i've been so long trying to fix that!
I made a command to remove the closest roadblock but it only works on the first roadblock that i created and the other ones it send the client message: There is no roadblock close enough to you.

Here are a samples for the commands:
pawn Код:
CMD:rb(playerid, params[]) // creating roadblock command!
{
    CreateRoadblock(978,x,y,z,a); //it's just an example so no need for the whole command!
    return 1;
}

CMD:rrb(playerid, params[]) // removing closest roadblock command
{
    for(new i = 0; i < sizeof(RBInfo); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, 5.0, RBInfo[i][rX], RBInfo[i][rY], RBInfo[i][rZ])) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}Error: {FFFFFF}There is no roadblock close enough to you.");
        DeleteClosestRoadblock(playerid);
        return 1;
    }
    return 1;
}
Reply
#2

You're checking the distance from RBInfo[a][rX], rY and rZ but at no point are you setting them. You're setting the roadblock location in Roadblocks[i][rX], rY and rZ.
Reply
#3

Quote:
Originally Posted by Zezombia
Посмотреть сообщение
You're checking the distance from RBInfo[a][rX], rY and rZ but at no point are you setting them. You're setting the roadblock location in Roadblocks[i][rX], rY and rZ.
The /rb command is already sitting the floats for the object like:
pawn Код:
CMD:rb(playerid, params[])
{
    new Float:x,Float:y,Float:z,Float:a;
    GetPlayerPos(playerid,x,y,z);
    GetPlayerFacingAngle(playerid, a);
    CreateRoadblock(978,x,y,z,a);
    return 1;
}
Reply
#4

Yeah, it removes only the first block because you kill the loop (which is on your command) (using return) once you realise that a block isn't close to a player. Instead of killing the loop, you should let it continue checking on other blocks. I dunno how the for-loop in your command is needed, check this:

pawn Код:
CMD:rrb(playerid, params[]) // removing closest roadblock command
{
    new result = DeleteClosestRoadblock(playerid);
    if(result == 0)
        // failure - no close enough roadblocks
    else
        // success - found a close roadblock and deleted it
    return 1;
}
Also, if you want it to remove more than one roadblock, you should remove the (return 1;) on your DeleteClosestRoadblock function.
Reply
#5

I think there is no need to create a new variable in the /rrb command, the problem should be in the looping method!
Reply
#6

Can you replace your CMD:rrb with mine and test it out?
Reply
#7

Quote:
Originally Posted by [KHK]Khalid
Посмотреть сообщение
Can you replace your CMD:rrb with mine and test it out?
lol i really don't know how it worked! also i don't know why it didn't work when using loops!
anyway thanks mate!
Reply
#8

I've already told you why it didn't work. Look, I will try to make it simple:

pawn Код:
// you type this command once and the first roadblock (ID 0 gets removed)
// but when you use it for the second time
// it's gonna check the first roadblock (ID 0) which DOESN'T exist now
// so it will basically be like this
 if(!IsPlayerInRangeOfPoint(playerid, 5.0, RBInfo[0][rX], RBInfo[0][rY], RBInfo[0][rZ])) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}Error: {FFFFFF}There is no roadblock close enough to you.");
// and ofc that's not gonna give good results since roadblock 0 doesn't really exist then at this point you kill the loop (return) and stop it from checking other roadblocks
// hence this code "DeleteClosestRoadblock(playerid);" would never be executed if roadblock ID 0 didn't exist.
CMD:rrb(playerid, params[]) // removing closest roadblock command
{
    for(new i = 0; i < sizeof(RBInfo); i++)
    {
        if(!IsPlayerInRangeOfPoint(playerid, 5.0, RBInfo[i][rX], RBInfo[i][rY], RBInfo[i][rZ])) return SendClientMessage(playerid, COLOR_WHITE, "{FF0000}Error: {FFFFFF}There is no roadblock close enough to you.");
        DeleteClosestRoadblock(playerid);
        return 1;
    }
    return 1;
}
Reply
#9

Guys sorry for bumping this after it got a solution, but somebody tell me how the command should work with loops without using any stocks! It's gonna make me crazy, it should has any method.
pawn Код:
stock DeleteClosestRoadblock(playerid)
{
    for(new i = 0; i < sizeof(Roadblocks); i++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 7.0, Roadblocks[i][rX], Roadblocks[i][rY], Roadblocks[i][rZ]))
        {
            if(Roadblocks[i][rCreated] == 1)
            {
                Roadblocks[i][rCreated] = 0;
                Roadblocks[i][rX] = 0.0;
                Roadblocks[i][rY] = 0.0;
                Roadblocks[i][rZ] = 0.0;
                DestroyObject(Roadblocks[i][rObject]);
                return 1;
            }
        }
    }
    return 0;
}
Reply
#10

You don't need to create a loop inside the command as the loop is inside that stock however remove the loop inside the command and keep the function and inside the function put instead of return 1; ---> break;
To perform a better performance.
About the message, create an integer and set it to -1 and once the roadblock found set it to 1 and out of the loop check if it still -1 and if it does send the message that says he's not near roadblock

I hope I helped any feedback would be appreciated!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)