One variable handling two types without a mess
Hello!
I am trying to build up a new scene and reuse the scripts written for the old scene as much as possible.
There are two different scripts 'A_Player' and 'B_Player' which share few content. Now I would like an old script 'Info' to be able to handle both types of player in the same variable 'player'. But I don't want to use inheritance since then the fictitious base class 'Player' would have to have all the variables public which any script which is supposed to handle both player types could possibly use. This would make a gigantic mess in my code.
I basically want to change the type of a variable 'player' in 'Info' (at all occurences in the code of 'Info') at the time of the scene loading based on which Scene is currently active. I don't want to have e.g. a seperate variable for both types of player and check which one isn't null at all occurences or similar inelegant solutions.
I feel like I am missing something here but I thought about this a lot and couldn't come up with a pleasing solution. I hope you guys can help me.
Greetings,
Daniel
Answer by scottishrob13 · Jul 28, 2020 at 10:33 PM
What's the problem with inheritance, exactly? Aren't those public fields already public in A_Player and B_Player anyway? With inheritance, you could plop whichever player you want into there. It's designed to solve the problem you're having.
If you don't want to do it that way, you could always make an Interface (see: https://www.tutorialsteacher.com/csharp/csharp-interface) and have both player classes implement the interface. If you do it that way, your Info variable would be typed to the interface and you can use either player class in there. This would be a pretty unusual way to use interfaces though, so be aware of the maintainability issues that arise from that.
If it's just data that you want to access, I recommend using Unity's Scriptable Objects and filling them out differently per-player (they can also benefit from inheritance), but it doesn't sound like that's what you're trying to do.
Thank you for your answer.
I guess inheritance does solve my problem and I was just hesitant because of the amount of work needed to restructure my code at this point. I would have only the conflicting variables in the base class and use this only when necessary, else I would use the derived classes as data types whenever possible. I will just chase the rat tail for a day or two and make that happen.
I tried interfaces but I think they would just be an unnecessary extra step and I would have to use inheritance to solve the core problem anyway.
Scriptable Objects are new to me, but I guess I can proceed without them.
Thank you!
Answer by FullMe7alJacke7 · Jul 28, 2020 at 11:34 PM
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected
Protected variables are accessible to inherited classes while remaining hidden from the rest of your application.
Thank you for your answer. Sadly I don't understand how this would solve my problem.