Gates loading properly out of database yet for loop not showing all.
#1

Hello, so I have a gate system that loads normally out the database, i've printed stuff, everything was loading fine. But then when /open 'ing the gate, it's only for the first gate that got loaded from the database.

Here the command and the loadfunction:

pawn Код:
stock LoadGates()
{
    new query[300],line[1000],totalgates;
    format(query, sizeof(query), "SELECT * FROM gates");
    mysql_query(query);
    mysql_store_result();
    new GID=0;
    while(mysql_fetch_row(line))
    {
        new Owner2[MAX_PLAYER_NAME];
        sscanf(line, "p<|>ds[24]dffffffffffffd",
        sGates[GID][GateID],
        Owner2,
        sGates[GID][GateModel],
        sGates[GID][OLocX],
        sGates[GID][OLocY],
        sGates[GID][OLocZ],
        sGates[GID][OLocRX],
        sGates[GID][OLocRY],
        sGates[GID][OLocRZ],
        sGates[GID][CLocX],
        sGates[GID][CLocY],
        sGates[GID][CLocZ],
        sGates[GID][CLocRX],
        sGates[GID][CLocRY],
        sGates[GID][CLocRZ],
        sGates[GID][Airline]);
        sGates[GID][Closed] = true;
        format(sGates[GID][Owner], 24 ,"%s", Owner2);
        printf("%s",sGates[GID][Owner]);
        GateObject[GID] = CreateObject(sGates[GID][GateModel],sGates[GID][CLocX],sGates[GID][CLocY],sGates[GID][CLocZ],sGates[GID][CLocRX],sGates[GID][CLocRY],sGates[GID][CLocRZ]);
        printf("%d", GID);
        GID=GID+1;
        totalgates++;
    }
    printf("Loaded %d gates from the database",totalgates);
    mysql_free_result();
    for(new gi; gi<MAX_GATES; gi++)
    {
        printf("%s",sGates[gi][Owner]);
        printf("%f", sGates[gi][CLocX]);
        printf("%f", sGates[gi][CLocY]);
        printf("%f", sGates[gi][CLocZ]);
    }
    return 1;
}
Command:
pawn Код:
if(!strcmp(cmdtext, "/o", true,2))
    {
        for(new gi; gi< sizeof(sGates); gi++)
        {
            printf("%s",sGates[gi][Owner]);
            printf("%f", sGates[gi][CLocX]);
            printf("%f", sGates[gi][CLocY]);
            printf("%f", sGates[gi][CLocZ]);
            if(IsPlayerInRangeOfPoint(playerid, 25.0, sGates[gi][CLocX], sGates[gi][CLocY], sGates[gi][CLocZ]))
            {
                if(sGates[gi][Closed] == true)
                {
                    if(sGates[gi][Airline] != 0)
                    {
                        if(sGates[gi][Airline] == PInfo[playerid][Airline] || PInfo[playerid][AdminLevel] > 0)
                        {
                            MoveGate(gi);
                            return 1;
                        }
                        else
                        {
                            SendClientMessage(playerid, COLOR_ERROR, "You're not part of this airline");
                            return 1;
                        }
                    }
                    else
                    {
                        if(!strcmp(PlayerName(playerid),sGates[gi][Owner], true) || PInfo[playerid][AdminLevel] > 0)
                        {
                            MoveGate(gi);
                            SendClientMessage(playerid, COLOR_GREEN, "Gate is being opened... Please wait.");
                            return 1;
                        }
                        else
                        {
                            SendClientMessage(playerid, COLOR_ERROR,"This is not your gate");
                            return 1;
                        }
                    }
                }
                else return SendClientMessage(playerid, COLOR_ERROR, "Gate is already being opened.");
            }
            return 1;
        }
        return 1;
    }
Log when printing the for loop:
Код:
[07:36:11] Kevin(0) - (/o)
[07:36:11] Kevin
[07:36:11] 1239.869995
[07:36:11] -740.719970
[07:36:11] 95.290000

This means it only prints out the first one of the database while we have 4 gates in total in the database.
Can anyone tell me what I did wrong? I've tried heaps of things but none of them actually worked.

Thanks!

-Kevin
Reply
#2

Never, ever put a return statement inside a loop body unless you explicitly want to break out of the loop. I think this might work:

pawn Код:
for(new gi; gi< sizeof(sGates); gi++)
{
    printf("%s",sGates[gi][Owner]);
    printf("%f", sGates[gi][CLocX]);
    printf("%f", sGates[gi][CLocY]);
    printf("%f", sGates[gi][CLocZ]);
   
    if(!IsPlayerInRangeOfPoint(playerid, 25.0, sGates[gi][CLocX], sGates[gi][CLocY], sGates[gi][CLocZ]))
        continue;
   
    if(sGates[gi][Closed] == true)
    {
        if(sGates[gi][Airline] != 0)
        {
            if(sGates[gi][Airline] == PInfo[playerid][Airline] || PInfo[playerid][AdminLevel] > 0)
            {
                MoveGate(gi);
                return 1;
            }
            else
            {
                SendClientMessage(playerid, COLOR_ERROR, "You're not part of this airline");
                return 1;
            }
        }
        else
        {
            if(!strcmp(PlayerName(playerid),sGates[gi][Owner], true) || PInfo[playerid][AdminLevel] > 0)
            {
                MoveGate(gi);
                SendClientMessage(playerid, COLOR_GREEN, "Gate is being opened... Please wait.");
                return 1;
            }
            else
            {
                SendClientMessage(playerid, COLOR_ERROR,"This is not your gate");
                return 1;
            }
        }
    }
    else return SendClientMessage(playerid, COLOR_ERROR, "Gate is already being opened.");
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)