Help with OnPlayerKeyStateChange
#1

Hey people again. Here's my 'thingy'. I have this under OnPlayerKeyStateChange:
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SECONDARY_ATTACK)
    {
        for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            /*if(IsPlayerInAnyVehicle(playerid))
                return 1;*/


            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
                return 1;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
                return 1;
            }
        }
        for(new b = 1; b<=MAX_FACTIONS; b++)
        {
            /*if(IsPlayerInAnyVehicle(playerid))
                return 1;*/

               
            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[b][fEnterX], F_DATA[b][fEnterY], F_DATA[b][fEnterZ]))
            {
                SetPlayerPos(playerid, F_DATA[b][fExitX], F_DATA[b][fExitY], F_DATA[b][fExitZ]);
                SetPlayerInterior(playerid, F_DATA[b][fInt]);
                SetPlayerVirtualWorld(playerid, F_DATA[b][fVW]);
                return 1;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[b][fExitX], F_DATA[b][fExitY], F_DATA[b][fExitZ]) && pxVW == F_DATA[b][fVW])
            {
                SetPlayerPos(playerid, F_DATA[b][fEnterX], F_DATA[b][fEnterY], F_DATA[b][fEnterZ]);
                SetPlayerInterior(playerid, 0);
                SetPlayerVirtualWorld(playerid, 0);
                return 1;
            }
            //else return 1;
        }
    }
    return 1;
}
Now, the problem is that for some reason, my second loop just wouldn't be called. I tried about everything, nothing seems to work. The thing is that I want to enter in a Business (First loop) and in a faction HQ (Second loop)... So, any suggestions with what might be wrong?

ps: I tried with prints all over the code, the second loop isn't called. Even tried to change the loops order, (Faction HQs first and business second). Same thing, only the first loop is called.

How can I fix it to call the second loop aswell ?
Reply
#2

What's the value of the MAX_FACTIONS constant? 1?
Reply
#3

pawn Код:
#define MAX_BUYABLE_BUS 25
#define MAX_FACTIONS    20
I stated earlier, both loops work if I 'use' them separately ... When I use both at a time, the second one (Whichever it would be), isn't called...
Reply
#4

Remove this from all code anywhere in your for statements:

pawn Код:
return 1;
Returning will break the loop and end the execution of the function. You can use 'break;' instead to break a for loop without preventing the function from continuing from executing. So, either, remove 'return 1;' or replace it with 'break;'.
Reply
#5

Still, that doesn't fix my problem. The thing is, that after first LOOP, nothing else works from that callback. Tested it out and only the first loop works, rest is like nothing's there.
Reply
#6

Paste the new code?
Reply
#7

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SECONDARY_ATTACK)
    {
        for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            /*if(IsPlayerInAnyVehicle(playerid))
                return 1;*/

            printf("Business looped: %i", b);
            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
            }
        }
        for(new b = 1; b<=MAX_FACTIONS; b++)
        {
            /*if(IsPlayerInAnyVehicle(playerid))
                return 1;*/


            printf("Factions looped: %i", b);
            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[b][fEnterX], F_DATA[b][fEnterY], F_DATA[b][fEnterZ]))
            {
                SetPlayerPos(playerid, F_DATA[b][fExitX], F_DATA[b][fExitY], F_DATA[b][fExitZ]);
                SetPlayerInterior(playerid, F_DATA[b][fInt]);
                SetPlayerVirtualWorld(playerid, F_DATA[b][fVW]);
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[b][fExitX], F_DATA[b][fExitY], F_DATA[b][fExitZ]) && pxVW == F_DATA[b][fVW])
            {
                SetPlayerPos(playerid, F_DATA[b][fEnterX], F_DATA[b][fEnterY], F_DATA[b][fEnterZ]);
                SetPlayerInterior(playerid, 0);
                SetPlayerVirtualWorld(playerid, 0);
            }
            //else return 1;
        }
        if(IsPlayerInRangeOfPoint(playerid, 1.0, 1580.400, -1637.599, 13.899) && P_DATA[playerid][pFactionID] == 1)
        {
            SetPlayerPos(playerid, 1580.4083, -1637.1522, 13.5512);
            SetPlayerFacingAngle(playerid, 175.4082);
            ApplyAnimation(playerid,"HEIST9","Use_SwipeCard",2.0,0,1,1,1,1,0);
            if(!PDGateOpen)
            {
                PDGateOpen = true;
                MoveObject(PDGate1, 1592.599, -1638.099, 12.5, 0.5);
                MoveObject(PDGate2, 1578.800, -1638, 14.399, 0.5);
                SendClientMessage(playerid, -1, "Gate open.");
            }
            else
            {
                PDGateOpen = false;
                MoveObject(PDGate1, 1584.699, -1638.099, 12.5, 0.5);
                MoveObject(PDGate2, 1580.5, -1637.900, 14.399, 0.5);
                SendClientMessage(playerid, -1, "Gate closed.");
            }
        }
        else if(IsPlayerInRangeOfPoint(playerid, 1.0, 1592.699, -1639.300, 14) && P_DATA[playerid][pFactionID] == 1)
        {
            SetPlayerPos(playerid, 1592.2437, -1639.3275, 13.3377);
            SetPlayerFacingAngle(playerid, 283.2880);
            ApplyAnimation(playerid,"HEIST9","Use_SwipeCard",2.0,0,1,1,1,1,0);
            if(!PDGateOpen)
            {
                PDGateOpen = true;
                MoveObject(PDGate1, 1592.599, -1638.099, 12.5, 0.5);
                MoveObject(PDGate2, 1578.800, -1638, 14.399, 0.5);
                SendClientMessage(playerid, -1, "Gate open.");
            }
            else
            {
                PDGateOpen = false;
                MoveObject(PDGate1, 1584.699, -1638.099, 12.5, 0.5);
                MoveObject(PDGate2, 1580.5, -1637.900, 14.399, 0.5);
                SendClientMessage(playerid, -1, "Gate closed.");
            }
        }
        print("Stopped.");
    }
    return 1;
}
This is my whole callback. Now, if I move the loops to the bottom, first 'if's statement works. I can OPEN / CLOSE gate. If I move the loop first, the rest of the callback stops working.
Reply
#8

Comment the second loop out for now, try replace the code in the first loop with this:

pawn Код:
new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]) && pxVW == B_DATA[b][bEnterVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
            }
Does it work?
Reply
#9

Commented the second loop and no, it still refuses to continue after the loop.

LE:
pawn Код:
for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            print("Start debug.");
            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                print("Debug 1.");
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                print("Debug 2.");
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
            }
            print("Debug 3.");
            // HERE'S SOMETHING WRONG WITH THE CODE ! STOPS HERE
        }
I commented the line where's the 'suspicious' part ... where it 'refuses' to continue.
Reply
#10

Any other input / suggestions ? Thanks.
Reply
#11

how many positions does your structure B_DATA have?

does it have less than MAX_BUYABLE_BUS ? if yes, you might get 'array out of bounds''.
Reply
#12

No ... Here:
pawn Код:
new B_DATA[MAX_BUYABLE_BUS][B_ENUM_DATA];
The thing is that every loop works if it's alone in the whole callback. Whichever loop I put first, that one works, rest of the callback refuses to work. That means, the problem's somewhere in the loop, not with my enums.
Reply
#13

Try this code:

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SECONDARY_ATTACK)
    {
        new pxVW = GetPlayerVirtualWorld(playerid);
        for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            if(IsPlayerInRangeOfPoint(playerid, 1.0, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
                break;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1.0, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
                break;
            }
        }
        for(new c = 1; c<=MAX_FACTIONS; c++)
        {

            if(IsPlayerInRangeOfPoint(playerid, 1.0, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]))
            {
                SetPlayerPos(playerid, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]);
                SetPlayerInterior(playerid, F_DATA[c][fInt]);
                SetPlayerVirtualWorld(playerid, F_DATA[c][fVW]);
                break;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1.0, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]) && pxVW == F_DATA[c][fVW])
            {
                SetPlayerPos(playerid, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]);
                SetPlayerInterior(playerid, 0);
                SetPlayerVirtualWorld(playerid, 0);
                break;
            }
        }
    }
    return 1;
}
Reply
#14

Not working ... The problem is that, after the first loop, if I'm not in the range of any point, there's nothing to 'continue' with ... I was thinking about a loop in a loop? Something like:
pawn Код:
for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            printf("Business looped: %i", b);
            new pxVW = GetPlayerVirtualWorld(playerid);
            if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
                break;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
                break;
            }
            else for(new c = 1; c<=MAX_FACTIONS; c++)
            {
            printf("Factions looped: %i", c);
            new pxVW = GetPlayerVirtualWorld(playerid);
                   if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]))
                   {
                   SetPlayerPos(playerid, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]);
                   SetPlayerInterior(playerid, F_DATA[c][fInt]);
                   SetPlayerVirtualWorld(playerid, F_DATA[c][fVW]);
                   break;
               }
                       else if(IsPlayerInRangeOfPoint(playerid, 1, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]) && pxVW == F_DATA[c][fVW])
                      {
                   SetPlayerPos(playerid, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]);
                   SetPlayerInterior(playerid, 0);
                   SetPlayerVirtualWorld(playerid, 0);
                   break;
               }
        }
        }
Would something like this work? A loop in a loop?

Meh ... If I think better, no, it's not good, because the other if statements from the callback wouldn't work ...
Reply
#15

pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SECONDARY_ATTACK)
    {
        new pxVW = GetPlayerVirtualWorld(playerid);
        for(new b = 1; b<=MAX_BUYABLE_BUS; b++)
        {
            printf("Business looped: %d", b);
            if(IsPlayerInRangeOfPoint(playerid, 1.0, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]))
            {
                SetPlayerPos(playerid, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]);
                SetPlayerInterior(playerid, B_DATA[b][bExitInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bExitVW]);
                break;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1.0, B_DATA[b][bExitX], B_DATA[b][bExitY], B_DATA[b][bExitZ]) && pxVW == B_DATA[b][bExitVW])
            {
                SetPlayerPos(playerid, B_DATA[b][bEnterX], B_DATA[b][bEnterY], B_DATA[b][bEnterZ]);
                SetPlayerInterior(playerid, B_DATA[b][bEnterInt]);
                SetPlayerVirtualWorld(playerid, B_DATA[b][bEnterVW]);
                break;
            }
        }
        for(new c = 1; c<=MAX_FACTIONS; c++)
        {
            printf("Factions looped: %d", c);
            if(IsPlayerInRangeOfPoint(playerid, 1.0, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]))
            {
                SetPlayerPos(playerid, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]);
                SetPlayerInterior(playerid, F_DATA[c][fInt]);
                SetPlayerVirtualWorld(playerid, F_DATA[c][fVW]);
                break;
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1.0, F_DATA[c][fExitX], F_DATA[c][fExitY], F_DATA[c][fExitZ]) && pxVW == F_DATA[c][fVW])
            {
                SetPlayerPos(playerid, F_DATA[c][fEnterX], F_DATA[c][fEnterY], F_DATA[c][fEnterZ]);
                SetPlayerInterior(playerid, 0);
                SetPlayerVirtualWorld(playerid, 0);
                break;
            }
        }
    }
    return 1;
}
is the text "factions looped" displayed ?
Reply
#16

If I'm in the range of a point from the first loop, no, the Factions looped: is not printed. Only Business looped... Something like:
Код:
Business looped: 1
Business looped: 2
........................
Business looped: 25
But, if I'm in range of a business ... the loops works great. It prints:
Код:
Business looped: 1
Business looped: 2
........................
Business looped: 20 - I'm in range of this business, looped stopped here since I'm in range
And it prints:
Код:
Factions looped: 1
Factions looped: 2
Factions looped: 3
........................
Factions looped: 20
Reply
#17

It has to do with 'array out of bounds'' as I've said before.

If you increment with <= you'll try to access a position out of bounds

Because on
pawn Код:
B_DATA[25][B_ENUM_DATA];
You can only access from 0..24 NOT 25, so try the following code:

pawn Код:
for(new b = 1; b<MAX_BUYABLE_BUS; b++)

//and ..
for(new c = 1; c<MAX_FACTIONS; c++)
Reply
#18

Use the original code you had and just change <= to <

pawn Код:
for(new b = 1; b < MAX_BUYABLE_BUS; b++)
pawn Код:
for(new b = 1; b < MAX_FACTIONS; b++)
Reply
#19

@mademan, what's the point of writing again something I've just said?
Reply
#20

Quote:
Originally Posted by xxmitsu
Посмотреть сообщение
@mademan, what's the point of writing again something I've just said?
What's the point of this post? To start a flame war? To get higher post count?

If you want to speak with me, you can use private messages.

Anyway I wanted to point out, that there was nothing wrong with his original code, except the <= operator.
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)