Timeline for How can I store a list of instances of different classes?
Current License: CC BY-SA 4.0
13 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 10 at 14:10 | vote | accept | Pow | ||
Feb 10 at 14:00 | answer | added | DMGregory♦ | timeline score: 2 | |
Feb 10 at 13:50 | comment | added | Pow | I see. I thought only classes could | |
Feb 10 at 13:43 | comment | added | DMGregory♦ | Pow "you can't have instances of an interface" yes, you can. Any instance of a class implementing an interface is an instance of that interface. | |
Feb 10 at 13:41 | comment | added | Pow |
Wait would a List<object> work?
|
|
Feb 10 at 13:36 | comment | added | Pow | Hmm, I'll give it a go, but I don't think it would work because you cant have instances of an interface. However, perhaps a class would work, instead of an attribute maybe I could make some sort of inheritable class. | |
Feb 10 at 13:07 | comment | added | Zibelas | @Pow If you have a generic ISave interface and two classes TypeA and TypeB who are both of type ISave, you can have a List<ISave> and add both TypeA and TypeB to the same list. | |
Feb 10 at 13:00 | comment | added | Pow |
@DMGregory "but other serializers are better structured for this" if so, what do you suggest. Before I was using the BinaryFormatter (before I found out it was obselete), but its hard to find good serialization methods online, I was thinking perhaps a CryptoStream but I'm not 100% sure that would work.
|
|
Feb 10 at 12:58 | comment | added | Pow | @Zibelas I'm unsure what interfaces would do to help solve this problem. | |
Feb 10 at 12:40 | comment | added | DMGregory♦ | What are you using to actually write the data for the save file currently? Unity's default serializer and JSON serialization tools have very limited polymorphic serialization capabilities without adding extra callbacks to handle things manually, but other serializers are structured better for this. | |
Feb 10 at 12:39 | comment | added | Zibelas | Have you taken a look at interfaces? | |
Feb 10 at 12:38 | history | edited | DMGregory♦ | CC BY-SA 4.0 |
Distinguish the class and instances of that class
|
Feb 10 at 11:24 | history | asked | Pow | CC BY-SA 4.0 |