SA-MP Forums Archive
how do you condense this code - 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: how do you condense this code (/showthread.php?tid=430970)



how do you condense this code - horsemeat - 15.04.2013

well the title says it all how do you make this code smaller becuse it won't compile
pawn Код:
if((choicevehiclemid[playerid])==435||(choicevehiclemid[playerid])==441||(choicevehiclemid[playerid])==449||(choicevehiclemid[playerid])==450||(choicevehiclemid[playerid])==464||(choicevehiclemid[playerid])==465||(choicevehiclemid[playerid])==501||(choicevehiclemid[playerid])==537||(choicevehiclemid[playerid])==538||(choicevehiclemid[playerid])==539||(choicevehiclemid[playerid])==564||(choicevehiclemid[playerid])==569||(choicevehiclemid[playerid])==570||(choicevehiclemid[playerid])==584||(choicevehiclemid[playerid])==590||(choicevehiclemid[playerid])==591||(choicevehiclemid[playerid])==594||(choicevehiclemid[playerid])==606||(choicevehiclemid[playerid])==607||(choicevehiclemid[playerid])==608||(choicevehiclemid[playerid])==610||(choicevehiclemid[playerid])==611)
I could use 2 if statement but I like to see if there is an easier way


Re: how do you condense this code - iJumbo - 15.04.2013

pawn Код:
if(choicevehiclemid[playerid] == 435 || choicevehiclemid[playerid] == 441 ||
choicevehiclemid[playerid] == 449 || choicevehiclemid[playerid] == 450 ||
choicevehiclemid[playerid] == 464 || choicevehiclemid[playerid] == 465 ||
choicevehiclemid[playerid] == 501 || choicevehiclemid[playerid] == 537 ||
choicevehiclemid[playerid] == 538 || choicevehiclemid[playerid] == 539 ||
choicevehiclemid[playerid] == 564 || choicevehiclemid[playerid] == 569 ||
choicevehiclemid[playerid] == 570 || choicevehiclemid[playerid] == 584 ||
choicevehiclemid[playerid] == 590 || choicevehiclemid[playerid] == 591 ||
choicevehiclemid[playerid] == 594 || choicevehiclemid[playerid] == 606 ||
choicevehiclemid[playerid] == 607 || choicevehiclemid[playerid] == 608 ||
choicevehiclemid[playerid] == 610 || choicevehiclemid[playerid] == 611) {


}



Re: how do you condense this code - horsemeat - 15.04.2013

Quote:
Originally Posted by iJumbo
Посмотреть сообщение
pawn Код:
if(choicevehiclemid[playerid] == 435 || choicevehiclemid[playerid] == 441 ||
choicevehiclemid[playerid] == 449 || choicevehiclemid[playerid] == 450 ||
choicevehiclemid[playerid] == 464 || choicevehiclemid[playerid] == 465 ||
choicevehiclemid[playerid] == 501 || choicevehiclemid[playerid] == 537 ||
choicevehiclemid[playerid] == 538 || choicevehiclemid[playerid] == 539 ||
choicevehiclemid[playerid] == 564 || choicevehiclemid[playerid] == 569 ||
choicevehiclemid[playerid] == 570 || choicevehiclemid[playerid] == 584 ||
choicevehiclemid[playerid] == 590 || choicevehiclemid[playerid] == 591 ||
choicevehiclemid[playerid] == 594 || choicevehiclemid[playerid] == 606 ||
choicevehiclemid[playerid] == 607 || choicevehiclemid[playerid] == 608 ||
choicevehiclemid[playerid] == 610 || choicevehiclemid[playerid] == 611) {


}
Thanks I will use this it not exactly what I wanted but this at least fixes the code problem is there any other way of doing this so it is more condense


Re: how do you condense this code - MP2 - 16.04.2013

You'd generally use an array for this.

pawn Код:
stock IsChoiceVehMid(playerid)
{
    stock const gChoiceVehMid[] = {
    435,
    441,
    449,
    450,
    464,
    465,
    501,
    537,
    538,
    539,
    564,
    569,
    570,
    584,
    590,
    591,
    594,
    606,
    607,
    608,
    610,
    611
    };

    for(new i=0; i<sizeof(gChoiceVehMid); i++)
    {
        if(gChoiceVehMid[i] == choicevehiclemid[playerid]) return 1;
    }
    return 0;
}
Not tested, and I wasn't sure what to call the function/array etc. because I have no idea what the hell it is.

Simply replace your code with this:

pawn Код:
if(IsChoiceVehMid(playerid))