generic and inheritance

https://paste.mod.gg/fgpktwfizvwd/0 @Aaron its unity but i suppose its really doesnt matter right?
BlazeBin - fgpktwfizvwd
A tool for sharing your source code with the world!
26 Replies
StalinTheThird
StalinTheThirdOP2mo ago
i want the children of entity have its corresponding config
Aaron
Aaron2mo ago
okay you probably do not want your code to use inheritance like this
StalinTheThird
StalinTheThirdOP2mo ago
whats your experience dealing this scenario
Aaron
Aaron2mo ago
well what probably are you trying to solve with inheritance here
StalinTheThird
StalinTheThirdOP2mo ago
i want the children of entity have its corresponding config and entity can have the entity config
Buddy
Buddy2mo ago
Try to make a global scriptable object class for both enemies and heroes you can use instead of using too much inheritance, or use a separate one for enemy and one for hero without the inheritance It can easily become a mess if you use too much inheritance
StalinTheThird
StalinTheThirdOP2mo ago
a global scriptable object? not sure i understand
Buddy
Buddy2mo ago
One that you can use on both heroes and enemies nonetheless, what exactly isn't working?
StalinTheThird
StalinTheThirdOP2mo ago
doesnt compile atm public abstract class Entity<TConfig> : Entity where TConfig : EntityConfigSO<Entity> public abstract class Entity : MonoBehaviour like this ? the non generic entity can be used on both i guess
Buddy
Buddy2mo ago
What properties would differ from Hero and Monster?
StalinTheThird
StalinTheThirdOP2mo ago
right now just upgrades it can have, but i certainly it gonna have more fr monster also have its spawn data thing in the config
Buddy
Buddy2mo ago
So, I would suggest to make the upgrades scriptable objects instead of messing with generics / inheritance which may cause problems reading and understanding the code and also adds complexity.
StalinTheThird
StalinTheThirdOP2mo ago
monster doesnt have upgrade and hero doesnt spawn you mean i should i throw all into entity config ?
Buddy
Buddy2mo ago
If possible, yes.
StalinTheThird
StalinTheThirdOP2mo ago
its possible yes one big class fr dont know if its right, so im just seperating them out atm
Buddy
Buddy2mo ago
custom inspectors are powerful, you can use that.
StalinTheThird
StalinTheThirdOP2mo ago
but the class still have leftover instances right
Buddy
Buddy2mo ago
Scriptable objects can have sub-scriptable objects. You can make it farily modular with scriptable objects, that way if you have designers they won't have to touch any code.
Buddy
Buddy2mo ago
Such as:
No description
Buddy
Buddy2mo ago
and I use no generics for that, I do however use some inheritance. But most of its functionality comes from the custom inspector
StalinTheThird
StalinTheThirdOP2mo ago
your Mage there, does it hold the prefab, and does this inherit from entity thing too
Buddy
Buddy2mo ago
It holds the scriptable object, yes. I use no inheritance for that. However, your system if you get it working might be really good but as mentioned, it adds complexity.
StalinTheThird
StalinTheThirdOP2mo ago
so your mage, warrior,... and mosnter are seprate class no inherit at all? im not sure that sound right no? for common stuff like take damage, move, changing states,... how did you combat this
Buddy
Buddy2mo ago
What do you mean? For taking damage I have an interface called IDamageable and a class called UnitHealth that inherits from MonoBehaviour and implements the IDamageable interface and a function to load the data from a scriptable object, no need to make separate classes for such things as they are all entities that should take damage, right? However, I see why you want to do it like that, you can have an interface or a base class you inherit from which uses no generics.
StalinTheThird
StalinTheThirdOP2mo ago
ok both of you suggest me to remove the strcture i will give this the last try today and ditch it have to go work atm fr thanks gonna big ah class instead fr it is what it is
Buddy
Buddy2mo ago
If you removed Entity's type argument, it might work as you want it to and use an interface as the Config But I doubt Unity will serialize that Which is where a custom inspector comes in and helps as currently all your types requires a type argument Or at least both the abstract classes do It might work if you just put EntityConfigSO in Config Nope, nevermind that is also dependant on generics

Did you find this page helpful?