Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by JGMEYER · Mar 22, 2018 at 01:24 AM · inheritancepolymorphism

Polymorphism on member attribute

This issue has been difficult for me to search, largely because the keywords bring up similar, but not exactly the same scenarios.
.

Basically what I'm trying to do is have a single GameManager class with a list of Player objects, but each class that inherits from GameManager will have some child of Player as well. How can I construct an array in GameManager to reduce or eliminate the amount of casting necessary on the list in a GameManager child.
.

Here's an example. The scene containing GameManagerChild is guaranteed to only contain PlayerChild objects.
.

 public class GameManager : MonoBehavior
 {
     protected Player[] _players;

     protected void Start()
     {
         InitializePlayers();
     }

     protected void InitializePlayers()
     {
         _players = FindObjectsOfType(typeof(Player)) as Player[];
     }
 }

 public class GameManagerChild : GameManager
 {
     void Update()
     {
         DoSomethingWithPlayers();
     }

     void DoSomethingWithPlayers()
     {
         foreach (PlayerChild player in _players)
         {
             player.SomeMethodSpecificToPlayerChild();
         }
     }
 }

.

One solution could be to make InitializePlayers abstract and each child of GameManager would need to populate the list with appropriate children of Player, but this would still require casting each time we want to act on an item in _player from a GameManager child.
.

Any better design suggestions?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 22, 2018 at 02:31 AM 0
Share

I don't quite understand the usage of your classes here. Isn't a Game$$anonymous$$anager meant to be a singleton? Why do you derive other classes from this class?

avatar image JGMEYER Bunny83 · Mar 22, 2018 at 02:40 AM 0
Share

@Bunny83, the game is actually a series of $$anonymous$$igames, so each game will have entirely different mechanics, but contain similar actions at their core. e.g. each game has a set of players, a win condition, the ability to exit to the game selection screen, etc

The only singleton currently is a GameControls$$anonymous$$anager singleton which keep controls for each player universal between the scenes.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 22, 2018 at 02:40 AM

I'm not sure what's the point of all this. Maybe you should choose a better / more descriptive name of your class. Does your class really "mangage the game"? If not you should use a different name.


However from your description you may want to do something like this:

 public class GameManager<TItemType> : MonoBehavior where TItemType : Player
 {
     protected TItemType[] _players;
     protected void Start()
     {
         InitializePlayers();
     }
     protected void InitializePlayers()
     {
         _players = FindObjectsOfType<TItemType>();
     }
 }
 
 public class GameManagerChild : GameManager<PlayerChild>
 {
     void Update()
     {
         DoSomethingWithPlayers();
     }
     void DoSomethingWithPlayers()
     {
         foreach (PlayerChild player in _players)
         {
             player.SomeMethodSpecificToPlayerChild();
         }
     }
 }

Note that you can't attach a "GameManager" to a gameobject as generic classes are not supported. However derived concrete classes do work just fine. So "GameManagerChild" can be attached to a gameobject.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JGMEYER · Mar 22, 2018 at 04:10 AM 0
Share

I believe the Game$$anonymous$$anager is appropriately named. The classes I included in the example are stripped-down versions to focus entirely on the question, so its understandable that the intention isn't clear.

The Game$$anonymous$$anagers be made to manage the scene, players, and win conditions of each $$anonymous$$igame. Basically the whole flow of the game and high-level interactions between the objects.

I think the construction above would work. I'm still picking up C#, so I'll need to read up on Generic vs. Derived Concrete Classes, since at first glance this just looks like a Generic class to me, but I think I see the distinction.

I guess TItemType is just a common na$$anonymous$$g convention for these constructs? Still trying to find a clear answer through google.

avatar image JGMEYER · Mar 25, 2018 at 06:21 PM 0
Share

This worked for what we needed. Appreciate the comments on the design. I'll keep thinking this over.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help me to get rid of repetitive code 1 Answer

[SOLVED] JSON Serialization of Derived Classes 2 Answers

GetComponent( ) and Polymorphism 1 Answer

Class structure for diferent item types and items that do different things. 0 Answers

"I am getting a null reference exception error for Pathfollowertest.instance.centerslot.Add(Cube.transform) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges