I'm creating a game in which I have a somewhat complex method for creating entities.
When a level is loaded, the loading code reads a bunch of YAML files that contain attributes of all the different possible units. Using the YAML file, it creates a so-called EntityResource
object. This EntityResource object serves as the authoritative source of information when spawning new units. The goal is twofold:
These EntityResource
objects are then fed into an EntityFactory
object to produce units of a specific type.
My question is as follows. Is there a way to create sublcasses of EntityResource
dynamically, based on the contents of the YAML file being read in?
Also, I would like each of these YAML-file-derived subclasses to be assigned a singleton metaclass. Any caveats?
I'm not sure if this is what you're looking for, but you can use type
to create subclasses dynamically:
SubClass = type('SubClass', (EntityResource,), {})
Edit: To understand how type
works, you just need to translate how would you write the class and translate that into a type
call. For example, if you want to write something like:
class SubClass(EntityResource):
A=1
B=2
then, that would be translated to:
SubClass = type('SubClass', (EntityResource,), {'A': 1, 'B': 2})
where: