SA-MP Forums Archive
calling enum with index - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: calling enum with index (/showthread.php?tid=603611)



calling enum with index - mcreed - 25.03.2016

Hi, I wanted to ask you a question.

Wanted to know if the enum have indices bone to not call them by how we declare it, but by a number.

Example:

pawn Код:
enum SI
{
    MaxPing, // INDEX 0
    MaxPacket, // INDEX 1
    MinFPS // INDEX 2
};
new ServerInfo[SI];

ServerInfo[0] = value;
now I tried to do so, but throws me the following errors:
pawn Код:
dir(863) : warning 213: tag mismatch
dir(863) : error 006: must be assigned to an array

This is my code:
pawn Код:
case DIALOG_MENUSERVER:
        {
            if(response)
            {
                IDL = listitem;
                if(ConfigVal(IDL) == "0" || ConfigVal(IDL) == "1")
                {
                    new params[1];
                    cmd_configmenu(playerid,params);
                }
                else
                {
                    ShowPlayerDialog(playerid,DIALOG_VALORCONFIG,DIALOG_STYLE_INPUT,"Config","Insert new value:","Accept","Exit");
                }
            }
        }
        case DIALOG_VALOROONFIG:
        {
            if(response)
            {
                ServidorInfo[IDL] = inputtext;
            }
        }
Thakns, and sorry for my bad english.


Re: calling enum with index - Mencent - 25.03.2016

Hi!

PHP код:
IDL listitem
to
PHP код:
IDL strval(listitem); 
and this:
PHP код:
ServidorInfo[IDL] = strval(inputtext); 



Respuesta: Re: calling enum with index - mcreed - 25.03.2016

Quote:
Originally Posted by Mencent
Посмотреть сообщение
Hi!

PHP код:
IDL listitem
to
PHP код:
IDL strval(listitem); 
and this:
PHP код:
ServidorInfo[IDL] = strval(inputtext); 
Errors persist:
pawn Код:
tag mismatch
Line: IDL = strval(listitem);
pawn Код:
argument type mismatch (argument 1)
Line: ServerInfo[IDL] = strval(inputtext);



Re: calling enum with index - Mencent - 25.03.2016

Is IDL a string or an integer?


Respuesta: calling enum with index - mcreed - 25.03.2016

type Integer:
pawn Код:
new IDL;



Re: calling enum with index - Mencent - 25.03.2016

Sorry, it should be like this:
PHP код:
case DIALOG_MENUSERVER:
{
    if(
response)
    {
        
IDL listitem;
        if(
ConfigVal(IDL) == "0" || ConfigVal(IDL) == "1")
        {
            new 
params[1];
            
cmd_configmenu(playerid,params);
        }
        else
        {
            
ShowPlayerDialog(playerid,DIALOG_VALORCONFIG,DIALOG_STYLE_INPUT,"Config","Insert new value:","Accept","Exit");
        }
    }
}
case 
DIALOG_VALORCONFIG:
{
    if(
response)
    {
        
ServidorInfo[IDL] = strval(inputtext);
    }




Respuesta: Re: calling enum with index - mcreed - 25.03.2016

Quote:
Originally Posted by Mencent
Посмотреть сообщение
Sorry, it should be like this:
PHP код:
case DIALOG_MENUSERVER:
{
    if(
response)
    {
        
IDL listitem;
        if(
ConfigVal(IDL) == "0" || ConfigVal(IDL) == "1")
        {
            new 
params[1];
            
cmd_configmenu(playerid,params);
        }
        else
        {
            
ShowPlayerDialog(playerid,DIALOG_VALORCONFIG,DIALOG_STYLE_INPUT,"Config","Insert new value:","Accept","Exit");
        }
    }
}
case 
DIALOG_VALORCONFIG:
{
    if(
response)
    {
        
ServidorInfo[IDL] = strval(inputtext);
    }

Error:
pawn Код:
tag mismatch
Line: ServidorInfo[IDL] = strval(inputtext);



Re: calling enum with index - Misiur - 25.03.2016

pawn Код:
ServidorInfo[IDL] = SI:strval(inputtext);
or

pawn Код:
enum sI
{
    MaxPing, // INDEX 0
    MaxPacket, // INDEX 1
    MinFPS // INDEX 2
};
new ServerInfo[sI];

ServerInfo[0] = value;
Notice the lower case first letter in enum name.

Also if inputtext is something other than 0, 1, 2, you'll get invalid array index access, crashing your server, so better sanitize/validate input first.


Re: calling enum with index - Gammix - 25.03.2016

Integers can't be directly used to specify which enum is being called since they can have different sizes.

Код:
ServidorInfo[SI:0] = strval(inputtext);
Is actually:
Код:
ServidorInfo[MaxPing] = strval(inputtext);
And your code is wrong, this is how you should do it:
pawn Код:
case DIALOG_MENUSERVER:
        {
            if(response)
            {
                if(ConfigVal(listitem) == "0" || ConfigVal(listitem) == "1")
                {
                    new params[1];
                    cmd_configmenu(playerid,params);
                }
                else
                {
                    ShowPlayerDialog(playerid,DIALOG_VALORCONFIG,DIALOG_STYLE_INPUT,"Config","Insert new value:","Accept","Exit");
                }
            }
        }
        case DIALOG_VALOROONFIG:
        {
            if(response)
            {
                ServidorInfo[SI:listitem] = inputtext;
            }
        }



Respuesta: Re: calling enum with index - mcreed - 25.03.2016

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Integers can't be directly used to specify which enum is being called since they can have different sizes.

Код:
ServidorInfo[SI:0] = strval(inputtext);
Is actually:
Код:
ServidorInfo[MaxPing] = strval(inputtext);
And your code is wrong, this is how you should do it:
pawn Код:
case DIALOG_MENUSERVER:
        {
            if(response)
            {
                if(ConfigVal(listitem) == "0" || ConfigVal(listitem) == "1")
                {
                    new params[1];
                    cmd_configmenu(playerid,params);
                }
                else
                {
                    ShowPlayerDialog(playerid,DIALOG_VALORCONFIG,DIALOG_STYLE_INPUT,"Config","Insert new value:","Accept","Exit");
                }
            }
        }
        case DIALOG_VALOROONFIG:
        {
            if(response)
            {
                ServidorInfo[SI:listitem] = inputtext;
            }
        }
It shows values that have nothing to do with the stored in the variable.

Thanks for all !!