PDA

View Full Version : Functions that return pointers



Joe123
03-16-2009, 03:28 PM
lweapon ofType(int ID){
lweapon Type;
for(int i=1;i<=Screen->NumLWeapons();i++){
Type = Screen->LoadLWeapon(i);
if(Type->ID == ID) return Type;
}
lweapon Null; return Null;
}

I was really quite surprised we could do this.
Does anyone have a better idea for 'none on screen' than returning one that doesn't point anywhere though?

pkmnfrk
03-16-2009, 05:55 PM
That's the cleanest way to do it. Besides, we always check "whatever->isValid()" before using it, right?

Right?

... right...?

-_-'''

Joe123
03-16-2009, 06:20 PM
Well, no.

If I was using that code I might do though.
Not that I probably will use that code, I was just surprised that it worked more than anything.

Could be useful for someone else?

pkmnfrk
03-16-2009, 07:04 PM
It's not a useless snippet. It returns the first lweapon of that kind. But, the convenience comes at a cost: 1. You don't get access to any other lweapons (maybe not a problem), and 2. You have to check to make sure the pointer is valid before you use it (again, maybe not a problem).

However, certainly, it's generally more useful to iterate over all weapons of a given type.

Joe123
03-16-2009, 07:20 PM
Well no, I didn't post it because I thought it was useless.

It wouldn't be hard to modify with NumLWeaponsOfType() and another parameter to let it know which of the lweapons of that type you wanted to return.

I only have one loop checking through LWeapons in my global script though anyway, checking through all lweapons on-screen multiple times per frame doesn't seem like a good idea.