Loop question/problem
#1

Hello, i am making dynamic teleport system, and now i ran into a problem.
I want to devide system into 3 teleport categories. And i do, i list all of them correctly, but the problem is with IDs, when i put all teleport locations into one dialog, i can do something like

new id = listitem;

and then it goes from 0, but if i devide it into 3 categories i have no idea how to get an id for each.

code:

pawn Код:
CMD:createport(playerid, params[])
{
    if(PI[playerid][Admin] < 6) return SCM(playerid, ERRORCOLOR, "ERROR: Niste ovlasteni za upotrebu ove komande - nemate admin level.");

    new Float:Poz[3], kate, ime[36], upit[256], inter, vw, id = TeleportaUcitano, str[128];
   
    if(sscanf(params, "is[36]", kate, ime)) return SCM(playerid, UPUTABOJA, "UPUTA: /createport [Kategorija 1-3] [Ime Lokacije]");

    GetPlayerPos(playerid, Poz[0], Poz[1], Poz[2]);
    inter = GetPlayerInterior(playerid);
    vw = GetPlayerVirtualWorld(playerid);
   
    Teleport[id][ID] = id;
    strmid(Teleport[id][tpIme], ime, 0, strlen(ime), 255);
    Teleport[id][PozX] = Poz[0];
    Teleport[id][PozY] = Poz[1];
    Teleport[id][PozZ] = Poz[2];
    Teleport[id][Interior] = inter;
    Teleport[id][VW] = vw;
    Teleport[id][Kategorija] = kate;
   
    mysql_format(konekt, upit, sizeof(upit), "INSERT INTO `Ports` (`ID`, `Ime`, `PozX`, `PozY`, `PozZ`, `Interior`, `VW`, `Kategorija`) VALUES ('%d', '%e', '%f', '%f', '%f', '%d', '%d', '%d')", id, ime, Poz[0], Poz[1], Poz[2], inter, vw, kate);
    mysql_tquery(konekt, upit);
   
    TeleportaUcitano ++;
   
    format(str, sizeof(str), "ID: %d | TP Created: %s | %f | %f | %f | %d | %d | %d", Teleport[id][ID], Teleport[id][tpIme], Teleport[id][PozX], Teleport[id][PozY], Teleport[id][PozZ], Teleport[id][Interior], Teleport[id][VW], Teleport[id][Kategorija]);
    SCM(playerid, RED, str);
    return 1;
}

CMD:port(playerid, params[])
{
    if(PI[playerid][Admin] == 0) return SCM(playerid, ERRORCOLOR, "ERROR: Niste ovlasteni za upotrebu ove komande - nemate admin level.");
   
    SPD(playerid, DIALOG_PORTLISTA, DIALOG_STYLE_LIST, "Odaberite kategoriju", "Javna mesta\nOrganizacije\nOstalo", "Odaberi", "Izlaz");
   
    /*new info[2100];
    for(new i = 0; i < 35; i++)
    {
        new str[128];
        format(str, sizeof(str), "%s\n", Teleport[i][tpIme]);
        strcat(info, str, sizeof(info));
    }
    SPD(playerid, DIALOG_KATE1PORT, DIALOG_STYLE_LIST, "Lokacije", info, "Odaberi", "");*/

    return 1;
}
dialogs:

pawn Код:
case DIALOG_PORTLISTA:
        {
            new info[2100];
            if(response)
            {
                switch(listitem)
                {
                    case 0:
                    {
                        for(new i = 0; i < 50; i++)
                        {
                            if(Teleport[i][Kategorija] == 1)
                            {
                                new str[128];
                                format(str, sizeof(str), "%s\n", Teleport[i][tpIme]);
                                strcat(info, str, sizeof(info));
                            }
                        }
                        SPD(playerid, DIALOG_KATE1PORT, DIALOG_STYLE_LIST, "Lokacije", info, "Odaberi", "");
                    }
                    case 1:
                    {
                        for(new i = 0; i < 50; i++)
                        {
                            if(Teleport[i][Kategorija] == 2)
                            {
                                new str[128];
                                format(str, sizeof(str), "%s\n", Teleport[i][tpIme]);
                                strcat(info, str, sizeof(info));
                            }
                        }
                        SPD(playerid, DIALOG_KATE1PORT, DIALOG_STYLE_LIST, "Lokacije", info, "Odaberi", "");
                    }
                    case 2:
                    {
                        for(new i = 0; i < 50; i++)
                        {
                            if(Teleport[i][Kategorija] == 3)
                            {
                                new str[128];
                                format(str, sizeof(str), "%s\n", Teleport[i][tpIme]);
                                strcat(info, str, sizeof(info));
                            }
                        }
                        SPD(playerid, DIALOG_KATE1PORT, DIALOG_STYLE_LIST, "Lokacije", info, "Odaberi", "");
                    }
                }
            }
        }
        case DIALOG_KATE1PORT:
        {
            if(response)
            {
                new str[128];

                new id = listitem;
               
                if(IsPlayerInAnyVehicle(playerid))
                {
                    SetPlayerVehPos(GetPlayerVehicleID(playerid), Teleport[id][PozX],Teleport[id][PozY],Teleport[id][PozZ]);
                }
                else
                {
                    SetPlayerPos_H(playerid,Teleport[id][PozX],Teleport[id][PozY],Teleport[id][PozZ]);
                }
                format(str, sizeof(str), "Teleportovani ste do %s.", Teleport[id][tpIme]);
                SCM(playerid, -1, str);
               
                id = -1;
            }
        }
Reply
#2

All you need is a variable that gets increased by 1 every time the category is the same (in the loop). When the value of the variable matches listitem, that's the teleport location.

Few things to point out:
- Don't create variables in loops.
- Show dialog once after the loop is finished.
- Better add "sizeof Teleport" instead of 50. Decreasing the size in the future will get you problems with run time error 4.
Reply
#3

Hmm, thanks for the tips, but i dont quite understand how that with variable would actually work
Reply
#4

I'll give you an example to get the idea.

Normally if you were to show the teleport locations of the category 2 you would loop through all the teleports, check if the category is equal to 2 and "join" the name of the location to the string to show the dialog.

Player chooses the listitem 3 from the list so using Teleport[listitem] would be wrong because the categories are mixed up.

On dialog response now, you have the listitem so you loop through the teleports again and if the category is 2, we increase the variable until its value is equal to the listitem so you get the index (3rd item of category 2) to use in Teleport array.
Reply
#5

I may sound like a jerk but as I'm reading this it actually makes no sense to me.

So, what you're saying is that after i do this:

pawn Код:
case 1:
                    {
                        for(new i = 0; i < sizeof(Teleport); i++)
                        {
                            if(Teleport[i][Kategorija] == 2)
                            {
                                format(str, sizeof(str), "%s\n", Teleport[i][tpIme]);
                                strcat(info, str, sizeof(info));
                            }
                        }
                        SPD(playerid, DIALOG_KATE1PORT, DIALOG_STYLE_LIST, "Teleport Destinacije", info, "Odaberi", "Izlaz");
                    }
I should also loop through the categories again on the dialog response, i mean here:

pawn Код:
case DIALOG_KATE1PORT:
        {
            if(response)
            {
                new str[128];

                new id = listitem;
               
                if(IsPlayerInAnyVehicle(playerid))
                {
                    SetPlayerVehPos(GetPlayerVehicleID(playerid), Teleport[id][PozX],Teleport[id][PozY],Teleport[id][PozZ]);
                }
                else
                {
                    SetPlayerPos_H(playerid,Teleport[id][PozX],Teleport[id][PozY],Teleport[id][PozZ]);
                }
                format(str, sizeof(str), "You have been teleported to %s.", Teleport[id][tpIme]);
                SCM(playerid, -1, str);
               
                id = -1;
            }
        }
Reply
#6

Tried it like this but its not working

pawn Код:
new id = 0;
                for(new i = 0; i < sizeof(Teleport); i++)
                {
                    if(Teleport[i][Kategorija] == 2)
                    {
                        id ++;
                    }
                   
                    if(listitem == id)
                    {

                        SetPlayerVehPos(playerid, Teleport[id][PozX],Teleport[id][PozY],Teleport[id][PozZ]);

                        new str[127];
                        format(str, sizeof(str), "Teleportovani ste do %s.", Teleport[id][tpIme]);
                        SCM(playerid, -1, str);
                    }
                }

Database if of any help

Reply
#7

Anyone ?
Reply
#8

Don't forget that listitem starts from 0 and you increase to 1 when it starts:
PHP код:
new id = -1;
for (new 
0sizeof(Teleport); i++)
{
    if (
Teleport[i][Kategorija] == && ++id == listitem)
    {
        
SetPlayerVehPos(playeridTeleport[i][PozX], Teleport[i][PozY], Teleport[i][PozZ]);
        new 
str[127];
        
format(strsizeof(str), "Teleportovani ste do %s."Teleport[i][tpIme]);
        
SCM(playerid, -1str);
        break;
    }

Reply
#9

That's what i was afraid of, i did it like this before, but it's not working, see the images:

http://imgur.com/a/Bpi36
Reply
#10

First of all, make sure the category you selected is 2 because if you didn't do the same for the rest of the categories, obviously it will not work.
Debug it to see what the problem might be. Print inputtext, listitem and what we retrieved ("i", name of location for "i" index) in dialog response to see if it matches with the item you selected from the dialog.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)