їAlguno sabe como crear comandos con "Espacios"?
#1

Bueno.. tengo la siguiente duda, їAlguno sabe como crear comandos con espacios? Por que sobre lo que yo sй los comandos van sin espacios, es decir.. no puede ser /hola a todos, tiene que ser /holaatodos.. pero hay comandos en donde tu tecleas por ejemplo /bat, y te sale "/bat [1-5]", y se puede usar /bat 1, /bat 2.. etc.

Yo tengo un comando tipo asн en mi servidor, el problema es que lo copiй y lo peguй, y tambiйn lo editй.. pero no logrй captar como se logra.. їAlguno me explica y me dice como podrнa hacerlo? Por ejemplo yo quiero tener el comando /estilopelea, y que salga "/estilopelea [1-4]", y asн.. espero que me entiendan.

Gracias.

PD: YO USO "STRCMP".
Reply
#2

pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{

    if(strcmp(cmdtext, "/comando", true) == 0)
    {
        new opcion[10];
        new idx;
        opcion = strtok(cmdtext, idx);

        if(!strlen(opcion))
        {
            SendClientMessage(playerid, -1, "Escribe: /comando (1-5) ");
            return true;
        }
        if(strcmp(opcion, "1", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "2", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "3", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "4", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "5", true) == 0)
        {
        //TU FUNCION
        }
    }
   
    return false;
}
Reply
#3

pawn Код:
new fightstyle
esa es la variable(Puedes ponerle otro nombre) Luego le asginas un nъmero por ejemplo el "4" que es un estilo de pelea,lo pondriamos asн
pawn Код:
if(fightstyle > 4)
Aquн te dejo las ids
https://sampwiki.blast.hk/wiki/Fightingstyles
Aquн te dejo el ejemplo
pawn Код:
if(strcmp("/tomapelea", cmdtext, true, 10) == 0)
{
    if (Info[playerid][pAC] >= 4)
    {
        new string[128], giveplayerid, fightstyle;
        if(sscanf(params, "dd", giveplayerid, fightstyle))
        {
            SendClientMessageEx(playerid, COLOR_WHITE, "USA: /tomapelea[playerid] [fightstyle]");
            SendClientMessageEx(playerid, COLOR_GRAD2, "Fighting styles: 4, 5, 6, 7, 15, 26.");
            return 1;
        }

        if(fightstyle > 3 && fightstyle < 8 || fightstyle == 15 || fightstyle == 26)
        {
            format(string, sizeof(string), " Tu estilo de pelea ha sido cambiado al ID %d.", fightstyle);
            SendClientMessageEx(giveplayerid,COLOR_YELLOW,string);
            format(string, sizeof(string), " Cambiaste el estilo de pelea de %s a %d.", GetPlayerNameEx(giveplayerid), fightstyle);
            SendClientMessageEx(playerid,COLOR_YELLOW,string);
            SetPlayerFightingStyle(giveplayerid, fightstyle);
            Info[playerid][pFStyle] = fightstyle;
            return 1;
        }
    }
    else SendClientMessageEx(playerid, COLOR_GRAD2, "No puedes usar este comando!");
    return 1;
}
Reply
#4

Quote:
Originally Posted by OTACON
Посмотреть сообщение
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[])
{

    if(strcmp(cmdtext, "/comando", true) == 0)
    {
        new opcion[10];
        new idx;
        opcion = strtok(cmdtext, idx);

        if(!strlen(opcion))
        {
            SendClientMessage(playerid, -1, "Escribe: /comando (1-5) ");
            return true;
        }
        if(strcmp(opcion, "1", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "2", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "3", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "4", true) == 0)
        {
        //TU FUNCION
        }
        if(strcmp(opcion, "5", true) == 0)
        {
        //TU FUNCION
        }
    }
   
    return false;
}
pawn Код:
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(56) : error 017: undefined symbol "strtok"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(56) : error 033: array must be indexed (variable "cmd")
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(76) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(85) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(239) : warning 219: local variable "idx" shadows a variable at a preceding level
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(240) : error 017: undefined symbol "strtok"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(240) : error 033: array must be indexed (variable "opcion")
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(245) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(289) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(292) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(292) : error 004: function "OnPlayerSpawn" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(296) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : error 004: function "OnPlayerDeath" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(301) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(304) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(304) : error 017: undefined symbol "SetupPlayerForClassSelection"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(313) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(313) : error 004: function "OnPlayerRequestClass" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(315) : error 017: undefined symbol "SetupPlayerForClassSelection"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(316) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : error 004: function "OnPlayerPickUpPickup" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(321) : error 017: undefined symbol "pickupid"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(327) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(330) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(330) : error 004: function "OnGameModeInit" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(858) : error 079: inconsistent return types (array & non-array)

Compilation aborted.Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


26 Errors.
Esos errores me tira..


Este es el code...

pawn Код:
if(strcmp(cmdtext, "/comando", true) == 0)
    {
        new opcion[10];
        new idx;
        opcion = strtok(cmdtext, idx);

        if(!strlen(opcion))
        {
            SendClientMessage(playerid, -1, "Escribe: /comando (1-5) ");
            return true;
        }
        if(strcmp(opcion, "1", true) == 0)
        {
       if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "2", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "3", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "4", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "5", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
    }

    return false;
}
Reply
#5

Quote:
Originally Posted by HunterCash97
Посмотреть сообщение
pawn Код:
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(56) : error 017: undefined symbol "strtok"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(56) : error 033: array must be indexed (variable "cmd")
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(76) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(85) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(239) : warning 219: local variable "idx" shadows a variable at a preceding level
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(240) : error 017: undefined symbol "strtok"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(240) : error 033: array must be indexed (variable "opcion")
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(245) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(289) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(292) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(292) : error 004: function "OnPlayerSpawn" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(296) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(299) : error 004: function "OnPlayerDeath" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(301) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(304) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(304) : error 017: undefined symbol "SetupPlayerForClassSelection"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(313) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(313) : error 004: function "OnPlayerRequestClass" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(315) : error 017: undefined symbol "SetupPlayerForClassSelection"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(316) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : warning 225: unreachable code
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(319) : error 004: function "OnPlayerPickUpPickup" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(321) : error 017: undefined symbol "pickupid"
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(327) : error 079: inconsistent return types (array & non-array)
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(330) : error 029: invalid expression, assumed zero
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(330) : error 004: function "OnGameModeInit" is not implemented
D:\Archivos\Simon\Server samp\gamemodes\bare.pwn(858) : error 079: inconsistent return types (array & non-array)

Compilation aborted.Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


26 Errors.
Esos errores me tira..


Este es el code...

pawn Код:
if(strcmp(cmdtext, "/comando", true) == 0)
    {
        new opcion[10];
        new idx;
        opcion = strtok(cmdtext, idx);

        if(!strlen(opcion))
        {
            SendClientMessage(playerid, -1, "Escribe: /comando (1-5) ");
            return true;
        }
        if(strcmp(opcion, "1", true) == 0)
        {
       if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "2", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "3", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "4", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
        if(strcmp(opcion, "5", true) == 0)
        {
        if (strcmp(cmdtext, "/normal", true) == 0)
  {
  SetPlayerFightingStyle (playerid, FIGHT_STYLE_NORMAL);
  SendClientMessage(playerid, COLOR_ROJO, "Tu nuevo estilo de pelea es Normal");

        }
    }

    return false;
}
їPor quй pusiste otro strcmp en cada comparaciуn? ЎAbsurdo!
Aparte de que te tira errores del Gamemode en general, їborraste todo?
Tendrнas que aprender las nociones de programaciуn bбsica antes de lanzarte por completo a programar en PAWN.
ЎCualquier consulta estoy para ayudarte!

ЎSaludos!
Reply
#6

Aqui un esquema casi completo:
pawn Код:
if(!strcmp(cmdtext, "/micomando", true, strlen("/micomando")))//comparamos solo la primer parte del string => ver funcionamiento de strcmp
{
    new
        idx = strlen("/micomando") + 1;//Indicamos en una variable la posicion a partir de la cual leeremos
       
    print(cmdtext[idx]);
    if(!strcmp(cmdtext[idx], "opcion 1", true))
    {
        //accion 1
        printf("opcion 1");
    }
    else if(!strcmp(cmdtext[idx], "opcion 2", true))
    {
        //accion 2
        printf("opcion 2");
    }
    else
    {
        //accion 3
        printf("error");
    }
    return 1;
}
La idea es evitar el uso de strtok y a su vez poder tener opciones de textos con espacios si quisieramos, por este motivo, accedemos directamente al string desde la posicion que nos interesa, en este caso omitiriamos todo el texto en rojo: "/micomando {este texto que sigue a lo rojo es variable}"

PD: podrias tranquilamente crear un comando que sea: "/hola a todos" de la siguiente forma:
pawn Код:
if(!strcmp(cmdtext, "/hola a todos", true))
Esto se debe a que la funcion strcmp compara texto caracter a caracter y los espacios, son caracteres validos.
Reply
#7

Quote:
Originally Posted by the_chaoz
Посмотреть сообщение
Aqui un esquema casi completo:
pawn Код:
if(!strcmp(cmdtext, "/micomando", true, strlen("/micomando")))//comparamos solo la primer parte del string => ver funcionamiento de strcmp
{
    new
        idx = strlen("/micomando") + 1;//Indicamos en una variable la posicion a partir de la cual leeremos
       
    print(cmdtext[idx]);
    if(!strcmp(cmdtext[idx], "opcion 1", true))
    {
        //accion 1
        printf("opcion 1");
    }
    else if(!strcmp(cmdtext[idx], "opcion 2", true))
    {
        //accion 2
        printf("opcion 2");
    }
    else
    {
        //accion 3
        printf("error");
    }
    return 1;
}
La idea es evitar el uso de strtok y a su vez poder tener opciones de textos con espacios si quisieramos, por este motivo, accedemos directamente al string desde la posicion que nos interesa, en este caso omitiriamos todo el texto en rojo: "/micomando {este texto que sigue a lo rojo es variable}"

PD: podrias tranquilamente crear un comando que sea: "/hola a todos" de la siguiente forma:
pawn Код:
if(!strcmp(cmdtext, "/hola a todos", true))
Esto se debe a que la funcion strcmp compara texto caracter a caracter y los espacios, son caracteres validos.
Muchas gracias The_Chaoz por esa forma, no se me habнa ocurrido y nunca la habнa visto.

Saludos!
Reply
#8

Habria que poner alguna restricciуn que verifique si se han igresado los demбs parбmetros porque si no habria una violacion de acceso de memoria en ese cуdigo.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)