Recursion
#1

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;
}
Reply
#2

Quote:
Originally Posted by ******
Посмотреть сообщение
No you don't.
Recursion doesn't affects the server performance?
Reply
#3

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?
Reply
#4

Search for randomEx

Код:
randomEx(ITEM_MEDKIT, ITEM_BONDAGE)
Reply
#5

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.
Reply
#6

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)