SA-MP Forums Archive
Create in_array - 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: Create in_array (/showthread.php?tid=496997)



Create in_array - Noliax8 - 24.02.2014

Hi,

I want create a function.... look:

pawn Код:
// CMD
new array[1] = {FACT_TYPE_POLICE};
if(!in_array(array, pInfo[playerid][pP__faction])) return SendClientDialog(playerid, DEFAULT_TIME, "~r~Vous n'avez pas accиs а cette commande.");

// FUNCTION
stock in_array(array[], value)
{
    for(new i; i < sizeof(array); i++)
    {
        if(array[i] == value) return 1;
    }
    return 0;
}
indeterminate array size in "sizeof" expression (symbol "")

Idea ?
Thanks


Re: Create in_array - Misiur - 24.02.2014

pawn Код:
stock in_array(array[], value, size = sizeof array)
{
    for(new i; i < size; i++)
    {
        if(array[i] == value) return 1;
    }
    return 0;
}
Why? Because sizeof is resolved at compile time, not run time. You have to pass the size when you call the function. You might be interested with custom iterators from y_iterate/foreach. But that's for bigger dynamic arrays. Also, you could simply return index at the same time, with -1 when not found.