SA-MP Forums Archive
Which option to use? - 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: Which option to use? (/showthread.php?tid=660764)



Which option to use? - darkHero - 12.11.2018

These are just examples.

Option 1:

Код:
new ObjectFood[] = { 10, 2000, 400, 200 };


IsFoodObjectPlayer(playerid)
{
	for(new i = 0; i < 4; i++)
	{
	    if(VarPlayerFood == ObjectFood[i]) return 1;
	}
	return 0;
}
Option 2:

Код:
IsFoodObjectPlayer(playerid, VarPlayerFood)
{
	switch(VarPlayerFood)
	{
		case 10, 2000, 400, 200: { return 1; }
	}
	return 0;
}



Re: Which option to use? - khRamin78 - 12.11.2018

fisrt one is have more clear script but a little more memory usage from second one


Re: Which option to use? - CJ101 - 12.11.2018

If you use the second one, you don't have to store data.

Either way, it's not a big deal if you store the data anyway - You are only putting four entries into your array.


Re: Which option to use? - Gammix - 12.11.2018

If your entries are small in number, say only 5-10 entries, i would use the second option.

But if you have more entries or the number of entries may change in future, option one looks more ideal for this.

Also if you didn't know this already:
pawn Код:
// no need to hardcode the size (i.e. 4), you can get the size with sizeof
for(new i = 0; i < sizeof(ObjectFood); i++)



Re: Which option to use? - Nero_3D - 12.11.2018

The second one, no matter how many items, because you are comparing with constant values
The for loop is only neccessary if you are dealing with non-constant values