[y_commands] /commands loop..
#1

Hi again. I ask a question here which is about loops.

I got this loop:
pawn Код:
for (new i = 0; i != count; ++i)
    {
        format(string,sizeof(string),"/%s",Command_GetNext(i, playerid));
        SendInfoMessage(playerid, string);
    }
Which adds up all commands in a list like this:
Код:
/commands
/help
/admins
and so on.

What I want is to make the loop count +5 everytime it finishes.
So that I can change the command format to:
pawn Код:
for (new i = 0; i != count; ++i)
    {
        format(string,sizeof(string),"/%s /%s /%s /%s /%s",Command_GetNext(i, playerid),Command_GetNext(i+1, playerid),Command_GetNext(i+2, playerid),Command_GetNext(i+3, playerid),Command_GetNext(i+4, playerid));
        SendInfoMessage(playerid, string);
    }
Any help is accepted.

EDIT: Also, how do I check if the "Command_GetNext" returns nothing, so that I don't end up with:
pawn Код:
/help /commands / / /
if there's only 2 commands available.
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
If "Command_GetNext" returns nothing, then the return will be empty:

pawn Код:
new buffer[YSI_MAX_STRING];
buffer = Command_GetNext(idx, pid);
if (buffer[0]) /* whatever */
Thanks. Do you have any idea on the loop?
Reply
#3

Just increase the loop by 5:
pawn Код:
for (new i = 0; i != count; i += 5)
Reply
#4

Thanks I'll try to put the rest together myself.

EDIT: How do I put the buffer[0] thing inside the string?

pawn Код:
for (new i = 0; i < count; i+=4)
    {
        format(string,sizeof(string),"/%s /%s /%s /%s /%s",Command_GetNext(i, playerid),Command_GetNext(i+1, playerid),Command_GetNext(i+2, playerid),Command_GetNext(i+3, playerid),Command_GetNext(i+4, playerid));
        SendInfoMessage(playerid, string);
    }
I don't want to end up with
pawn Код:
/help /commands / / /
Reply
#5

bump
Reply
#6

Yeah, I understand that but what I don't understand is how to format the string depending on how many commands are available?

Right now I end up with having the last line in my /commands like this:
pawn Код:
/veh / / / /
because the format looks like:
pawn Код:
/%s /%s /%s /%s /%s
So basically, I just need to know how to ADD info to a string depending on how many commands there is on the last line. E.g.

pawn Код:
if(buffer[0]) { AddToString(str,"/%s",buffer[0]) } else {return 1;}
if(buffer[1]) { AddToString(str,"/%s",buffer[1]) } else {return 1;}
if(buffer[2]) { AddToString(str,"/%s",buffer[2]) } else {return 1;}
if(buffer[3]) { AddToString(str,"/%s",buffer[3]) } else {return 1;}
if(buffer[4]) { AddToString(str,"/%s",buffer[4]) } else {return 1;}
EDIT: I searched wiki and found "Strins" would that work?
Reply
#7

Now I have this command:

pawn Код:
YCMD:commands(playerid, params[], help)
{
    if(help) return SendInfoMessage(playerid,"Shows all available commands to a certain player.");
    new count = Command_GetPlayerCommandCount(playerid);
    new string[128],str[128];
    format(string,sizeof(string),"List of commands for {a9c4e4}%s{FFFFFF}:",pName(playerid));
    SendInfoMessage(playerid,"Type /help [command] for more info.");
    SendInfoMessage(playerid,string);
    for (new i = 0; i < count; i+=5)
    {
        new buffer[YSI_MAX_STRING];
        buffer = Command_GetNext(i, playerid);
        format(str, sizeof (str), "%s /%s", str, buffer);
        SendInfoMessage(playerid, str);
    }
    return 1;
}
Which sends the string each time the loop starts over. And if I change it to:

pawn Код:
YCMD:commands(playerid, params[], help)
{
    if(help) return SendInfoMessage(playerid,"Shows all available commands to a certain player.");
    new count = Command_GetPlayerCommandCount(playerid);
    new string[128],str[128];
    format(string,sizeof(string),"List of commands for {a9c4e4}%s{FFFFFF}:",pName(playerid));
    SendInfoMessage(playerid,"Type /help [command] for more info.");
    SendInfoMessage(playerid,string);
    for (new i = 0; i < count; i+=5)
    {
        new buffer[YSI_MAX_STRING];
        buffer = Command_GetNext(i, playerid);
        format(str, sizeof (str), "%s /%s", str, buffer);
    }
        SendInfoMessage(playerid, str);
    return 1;
}
I end up with getting only 1 row of commands.
Reply
#8

love you (Y)

Final Result:

pawn Код:
YCMD:commands(playerid, params[], help)
{
    if(help) return SendInfoMessage(playerid,"Shows all available commands to a certain player.");
    new count = Command_GetPlayerCommandCount(playerid);
    new string[128],str[128];
    format(string,sizeof(string),"List of commands for {a9c4e4}%s{FFFFFF}:",pName(playerid));
    SendInfoMessage(playerid,"Type /help [command] for more info.");
    SendInfoMessage(playerid,string);
    for (new i = 0; i < count; i+=5)
    {
        new buffer[YSI_MAX_STRING];
        buffer = Command_GetNext(i, playerid);
        if(buffer[0] != 0) format(str, sizeof (str), "%s /%s", str, buffer);
        buffer = Command_GetNext(i+1, playerid);
        if(buffer[0] != 0) format(str, sizeof (str), "%s /%s", str, buffer);
        buffer = Command_GetNext(i+2, playerid);
        if(buffer[0] != 0) format(str, sizeof (str), "%s /%s", str, buffer);
        buffer = Command_GetNext(i+3, playerid);
        if(buffer[0] != 0) format(str, sizeof (str), "%s /%s", str, buffer);
        buffer = Command_GetNext(i+4, playerid);
        if(buffer[0] != 0) format(str, sizeof (str), "%s /%s", str, buffer);
        SendInfoMessage(playerid, str);
        format(str,sizeof(str),"");
    }
    return 1;
}
Hope someone more than me got use of it.
Thank you ******.
Reply
#9

What does that do? Save CPU power?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)