dialog input
#1

well i made an input dialog,but i got a problem.
What i want to do is,i want to link the inputtext with playerids.
i know how to do it for a definite number of floats with sscanf.
But if i type in the input dialog box "15 20 12 8",i want to do some functions on those players with those ids.
not just 4,i want it to have no limit,i must be able to type 1 id or 100 ids,but still it should still have effect on every id i typed.

(sorry im really confused,so i cannot type in proper english right now)
Reply
#2

You can do this easily with ******'s sscanf plugin.

First read this for better understanding: https://github.com/Y-Less/sscanf/wiki/%22a%22-Specifier

Then you might try to do something like this:
pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case MY_COMMAND: //the id of your command dialog
        {
            if(response) //if you pressed the first button, like "OK"
            {
                if(strlen(inputtext)) //if you entered at least 1 character
                {
                    new cmdplayers[MAX_PLAYERS], i=0;
                    sscanf(inputtext, "A<i>(-1)[MAX_PLAYERS]", cmdplayers);
                    //You put the ids you introduced in the array, and set the rest to -1 cuz 0 might be a playerid.
                    while(cmdplayers[i++]!=-1) //change for each playerid you introduced
                    {
                        //do what you have to do with the playerid
                    }
                }
            }
        }
    }
    return 1;
}
^I had no time to test this but i think i did it ok.
Reply
#3

Quote:
Originally Posted by Koala818
Посмотреть сообщение
pawn Код:
while(cmdplayers[i++]!=-1)
Using ++ after the variable returns the old value. So it will start by checking if "cmdplayers[0]" is not -1 and in the code inside the loop "i" will be 1. You could declare "i" with default value of -1 and use "++i" instead.

But if any value is not -1, the loop will stop so it won't work even you had changed the above. Use a for loop instead and check if the value is not -1 in an if statement.
Reply
#4

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Using ++ after the variable returns the old value. So it will start by checking if "cmdplayers[0]" is not -1 and in the code inside the loop "i" will be 1. You could declare "i" with default value of -1 and use "++i" instead.

But if any value is not -1, the loop will stop so it won't work even you had changed the above. Use a for loop instead and check if the value is not -1 in an if statement.
^I think you wanted yo say that the loop won't stop. Wich is true cuz i forgot to add && i<MAX_PLAYERS. And the first thing is also true, it slipped off my mind. I don't even know why i didn't used a for with a break.

So the right code should look like:

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid)
    {
        case MY_COMMAND: //the id of your command dialog
        {
            if(response) //if you pressed the first button, like "OK"
            {
                if(strlen(inputtext)) //if you entered at least 1 character
                {
                    new cmdplayers[MAX_PLAYERS], i=-1;
                    sscanf(inputtext, "A<i>(-1)[MAX_PLAYERS]", cmdplayers);
                    //You put the ids you introduced in the array, and set the rest to -1 cuz 0 might be a playerid.
                    while(cmdplayers[++i]!=-1 && i<MAX_PLAYERS) //change for each playerid you introduced
                    {
                        //do what you have to do with the playerid
                    }
                }
            }
        }
    }
    return 1;
}
Reply
#5

If the value of "cmdplayers[++i]" is not -1 (has an ID of player), the expression is false and thus the loop will stop.
Use a for loop instead.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
If the value of "cmdplayers[++i]" is not -1 (has an ID of player), the expression is false and thus the loop will stop.
Hmm. Correct me if i am wrong but as i know the while works like:
pawn Код:
while(cmdplayers[++i]!=-1 && i<MAX_PLAYERS) //change for each playerid you introduced
{
                        //do what you have to do with the playerid
}
cmdplayers[50];
cmdplayers[0]=12; //the while will execute for this
cmdplayers[1]=23; //the while will execute for this
cmdplayers[2]=42; //the while will execute for this
cmdplayers[3]=-1; //the while will stop
cmdplayers[4]=-1;
.
.
.
cmdplayers[49]=-1;
So you will execute the command for all the players you introduced. What's wrong more exatly
Reply
#7

Sorry, I thought it somehow differently so it's my mistake. Yes, in that case the loop will be called 3 times but in case the array's indexes are full (Let's say MAX_PLAYERS is 10 and the array stores all the players), it will be caused a run time error 4. In that case, checking if i is less than MAX_PLAYERS first is the solution:
pawn Код:
while (++i < MAX_PLAYERS && cmdplayers[i] != -1)
Reply
#8

thank you both,my command is now working perfectly.
Reply
#9

hmm...just a doubt ,how do i add multiple playerids in the inputtext and have the same effect on all inputted players?
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)