3 ms·
I once encountered this problem when writing a video game as an never-published side project. Most in-game items and resources were very simple - largely expre
by RandomBK 4y ago
I once encountered this problem when writing a video game as an never-published side project.
Most in-game items and resources were very simple - largely expressible by a simple <item_type>:<quantity> dictionary. Others however, could support a never-ending variety of custom attributes and logic.
After much thought, I wound up pursuing a solution that turned out to be quite powerful and extendible:
Items would be represented via a <item_type>:<<attr_key>:<attr_value>> datastructure. Any system that needed to interact with an item would call an 'item handler' assigned to that item type, which exposed a standard interface like 'getQuantity', 'useItem', etc.
Most basic items shared a common handler that stored quantity as an attribute field. However, more complex items could implement custom logic. I guess this is somewhat similar to Mixins or Component Based Architecture.
I think this is partially covered under the 'more abstraction' option in the blog, but I've personally found this to be an interesting and valuable tradeoff that can be deployed in a lot of situations.