Hi,
Does anyone have any insight to which would be better to use in my situation/most cases in general. In my save game I can use 3 methods to get the data I need, in this case all the items (Content) of my inventory. I may have to use a cast anyway for the play time which is tracked by a timer on the player character, but most of the information that I need to save is held on components. Would there be any need to strictly choose one method or maybe have a combination of all of them? Does one method have a significant advantage over the rest?
Any information or advice would be great, thanks.
a cast will create a hard dependency between the classes. This wont be an issue if these two classes will always exist together. but for instance if you have a character which is only used in two levels of the game but the game mode which is used in every level of the game has a hard link, now that one character will be loaded to memory even when it is not present in the level.
It’s a bit inefficient, however it is better to test and see if it actually creates a problem for you before complicating your code significantly.
Interface makes it so classes only depend on the interface. This way if you nuke some class the others won’t care. But if you change the functions in the interface you’ll need to go update the classes which use it. So it is a little more flexible.
With component you just have a hard reference to the component class. If your components are small and dumb that should also give you a lot of flexibility if you need to change code later.
I think the most important thing is to use a consistent architecture. For example dont use an interface to handle class to class communications in one case, and then in another use component with no clear reason why one versus the other.
With interfaces you have common function/event that can be handled unique ways by each subscriber.
with component you have common logic/data shared by each subscriber.
Cast you are essentially checking that an object is of a type (either the same class or a child class) and then grabbing it’s public contents.
2 Likes
Wow, thankyou for your detailed response. I will definitely try to bear this all in mind going forward. I think I’ll try and use component by class and ofc with testing I could try and determine what is better