Re: creating an instance of an instance of a class with python

Right, which is another problem entirely.  You don't need to copy a mob, you need to be able to spawn a mob with some given properties, possibly with a unique name, possibly with some random variation.  Solution, forget about deep copy.  Do the following:
First, make a dictionary, called mob_types.  This is a dictionary where the keys are the mob names and the values are a second dictionary.  This second dictionary looks like this:
{"display_name": "knight", "attack_power":5, "another_property" : 6}

Next, you make your mob's class __init__ method have the following prototype:
def __init__(self, **kwargs)
Kwargs is now a dictionary of all keyword arguments passed to __init__.  Now, for each property you wish to be able to set at initialization, do the following:
self.property = kwargs["property'] if "property" in kwargs else default_value
This can be called in two ways.  First, we have mob(health = 5, attack_power = 10).  Second, we have this:
mob(**{"attack_power" : 10, "health" : 5})
So you do, mob(**my_mob_templates["mob_name"]), and you have a mob with the specified properties.

There are other ways of implementing this, but this has some advantages.  Most notably, you can load the mob type dictionary from xml, json, yml, etc.  Also, if you use getattr, setattr, and a few other cryptic functions, you can avoid typing the lines manually; however, there are downfalls with that approach too (namely, any property that you assume must always be set, i.e. health, must always be explicitly set instead of just allowing the system default).  use this latter approach only after very, very carefully considering the implications.

Let me know if this isn't clear.

URL: http://foru m.audiogames.net/viewtopic.php?pid=150396#p150396

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
http://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector

Reply via email to