Previously I would have a list bound to every item that was created. That list contain names of the items allowed to be used on the said object. Is there a better, more efficient way of doing this?
Pages 1
You are not logged in. Please login or register.
AudioGames.net Forum → Developers room → Using items on each other
Previously I would have a list bound to every item that was created. That list contain names of the items allowed to be used on the said object. Is there a better, more efficient way of doing this?
A possible solution is method names. For example: pistol.ammo_used() or lockbox.key_used(). I'm assuming you use inheritance, so each item is a child of the generic item. You can use getattr to get the method name, or else print some generic message like "A banana and a pistol? No way." or something like that.
Alternatively, I guess you could have a module or dictionary dedicated to these functions, so like pistol_with_ammo or pizza_with_knife if your items are just names or if you prefer that for some reason. That also makes it a little bit easier for the order to be flipped around--what if someone used the ammo with the pistol instead of the other way around?
What I usually do is add an abstract method to my item base class which is i.e. called is_usable and receives someobject. This method overriden in the different item subclasses then decide upon this item can be used onto something else, like another item, an enemy, a tile, the player, whatsoever. A pythonic example could look like the following:
from abc import ABC
class item(ABC):
@abstractmethod
def is_usable(self, obj):
return False
class dagger(item):
def is_usable(self, obj):
if isinstance(obj, player):
return True # daggers should only be usable on players, nothing else
return False
I don't have to create lists of multiple objects that way (those could actually create problems with garbage collection due to possible circular references etc), but instead update my is_usable() checks each time something changes. Whereas you could enhance that methods that much so that you don't need to update those alot, like create a weapon class which inherits item and is further derived by e.g. a dagger, a pistol or whatsoever and check on those weapon subclasses to see if any weapon works with a given item. There are so many possibilities to solve your problem, as always . It always depends on your situation.
Best Regards.
Hijacker
@3, holy shit man.
Okay... sorry. I never heard of this before. This could be really, really useful in the future. Thanks.
AudioGames.net Forum → Developers room → Using items on each other
Generated in 0.019 seconds (41% PHP - 59% DB) with 10 queries