Recursion - 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: Recursion (
/showthread.php?tid=657022)
Recursion -
ToiletDuck - 30.07.2018
Is this a bad example of a Function?
I got an error about recursion.
Код:
Function::RandomHospitalLoot()
{
new ran = Random(0, MAX_LOOTITEMS-1);
switch(ran)
{
case ITEM_MEDKIT, ITEM_BONDAGE, ITEM_FIRSTAID: return ran;
default: return RandomHospitalLoot();
}
return ran;
}
Re: Recursion -
ToiletDuck - 30.07.2018
Quote:
Originally Posted by ******
No you don't.
|
Recursion doesn't affects the server performance?
Re: Recursion -
ToiletDuck - 30.07.2018
Quote:
Originally Posted by ******
In general, no, but that code is terrible. Why not just do random(3)?
|
because those 3 items has different ids and their id is not 1,2,3
Код:
#define ITEM_MEDKIT 7
#define ITEM_FIRSTAID 8
#define ITEM_BONDAGE 9
is there any way to optimize this?
Re: Recursion -
ball - 30.07.2018
Search for randomEx
Код:
randomEx(ITEM_MEDKIT, ITEM_BONDAGE)
Re: Recursion -
Calisthenics - 30.07.2018
Quote:
Originally Posted by ball
Search for randomEx
Код:
randomEx(ITEM_MEDKIT, ITEM_BONDAGE)
|
This will return either 7 or 8. You have to do it like this:
Код:
randomEx(ITEM_MEDKIT, ITEM_BONDAGE + 1)
for 7-9 values.
Re: Recursion -
ToiletDuck - 30.07.2018
Quote:
Originally Posted by ******
Код:
RandomHospitalLoot()
{
const items[] = { ITEM_MEDKIT, ITEM_BONDAGE, ITEM_FIRSTAID };
return items[random(sizeof (items))];
}
|
Wow thank you! this is better than my code why I haven't thought this one
Items[] = { ITEM_MEDKIT, ITEM_BONDAGE, ITEM_FIRSTAID } ; // ID: Array starts on 0 so 0,1,2
items[random(sizeof(items))]; sizeof getting the total value in items array and then it it random from 0-2 if im not mistaken and then if it random to 1 then ITEM_BONDAGE will return of its ID right?
^ enlighten me please