Monday, 4 April 2016

That pesky inheritance

I had an idea to test how easy it would be to inherit Paper class from Item class. The slight issue I have with scroll items that only they have a scroll component in Item class which is left empty when the item is not a scroll (or readable item).

Inheriting from Item is easy, but what happens next is not. The problem is that even if you could store Paper type items in a Item list and then cast it to Paper, there are even deeper problems. Perhaps the most difficult one is the way items are saved as a list of items. When you load items, an empty Item class is created and currently there is no way to tell what the next item is going to be. Obviously you would have to create Paper instance for readable items, but to do that you would need to save the type of object, then load if before loading the actual object data.

I don't know how people solve this. Maybe using some kind of obscure pattern trick who knows. Sometimes I'm dreaming of removing inheritance from game objects and creating one gigantic base class for all object types. Then everything could be placed everywhere without the fear of object slicing, you could stuff small monsters into your backpack, carry around a detached door and use it as weapons etc.

No comments:

Post a comment