Generic Object Creation
I want to implement a generic method to create instance of ItemComponents from Items. Here is my design.
Now, I want to instantiate specific component for specific item.
Of course, the code is not instantiating the child classes, they are just instances of UItemComponentBase. How can I achieve this? Is it possible to implement a generic method for that or do I need to check the type and write different code for each class type?
Edit: I can only achieve this by type checking and writing different methods for each UItemComponent subclass.
checking for the type of class in a switch case, is a sign that your breaking encapsulation and not properly delegating tasks. your design makes ASomeActor a high-maintenance, all-knowing class, that needs new code for every new item subclass you create.
instead, you should put a virtual function in the item base class that returns its corresponding component. then, when you add a new item subclass, you just need to set the ItemComponentClass variable in the default properties.
each item should be responsible for knowing its own related parts. external objects that communicate with items, should not know anything about specific items or their related parts, they should just know how to communicate with the interface of the base item.
Follow this question
Once you sign in you will be able to subscribe for any updates here