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:
- Deter cheating by implementing a hash check on the output of the YAML file
- Aid in debugging by having all unit information come from a single, authoritative source.
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
typeto create subclasses dynamically:Edit: To understand how
typeworks, you just need to translate how would you write the class and translate that into atypecall. For example, if you want to write something like:then, that would be translated to:
where: