Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ssandokan · Oct 27, 2014 at 05:48 PM · c#networkingbugnetworkinheritance

OnDisconnectedFromServer ignores inheritance

Hello, this particular problem made me a headache and i don't know if that's a bug or feature. So what i have is Controller scripts looking like this:

 public class BaseController: MonoBehaviour
 {
 
 }
 public class AController: BaseController
 {
   protected virtual void OnDisconnectedFromServer ( NetworkDisconnection info )
   {
     Debug.Log("Disconnected A");
   }
 }
 public class BController: AController
 {
   protected override void OnDisconnectedFromServer ( NetworkDisconnection info )
   {
     Debug.Log("Disconnected B");
   }
 }

Now my problem is that when i leave the server, OnDisconnectedFromServer (link ) is called, but it gets called both on AController and then on BController. I have tried even shadowing or calling other method instead like here but no matter what, i cant get to disable/override the behaviour of AController when i have instance of BController. Am i doing something wrong or is this unity bug?

Thank you very much.

Comment
Add comment · Show 5
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 Habitablaba · Oct 27, 2014 at 05:49 PM 0
Share

Because you are inheriting from AController in BController, you'll end up with the functionality of both (that's pretty much the point of inheritence).
What is the reason for having BController inherit from AController if you are just hiding the functionality?

Edit: I moved this to a comment and completely changed it because I totally misread the question.

avatar image Ssandokan Habitablaba · Oct 27, 2014 at 05:51 PM 0
Share

my problem is complete opposite, i WANT to override it and even when i dont call base.OnDisconnectedFromServer(); it still gets called somehow

avatar image mwbranna · Oct 27, 2014 at 07:23 PM 0
Share

Habitablaba, I think you are missing some of the point of inheritance. One of the points is to retain some functionality while being able to override other functionality.

avatar image Habitablaba · Oct 27, 2014 at 07:31 PM 0
Share

oh man, I had no idea. No but seriously, I still question the point of having BController inherit from AController if you're just going to hide the functionality of AController. Also, you want to talk about terrible coding practices, lets have a conversation about forcing a base class to know about its derived classes.

$$anonymous$$y next best guess would be to try swapping the 'override' keyword for the 'new' keyword in BController. This tells the compiler that you don't want to use the base class version of a method, and that you are providing entirely new functionality. I haven't tested it in Unity because, again, I think it is a terrible idea.

avatar image Ssandokan · Oct 27, 2014 at 07:33 PM 0
Share

i have tested new keyword already and it changed nothing, the bug still occured. Of course i am not just hiding functionality, there is plenty of code i did not include in the example. the problem is that i simply need to override behavior of player disconnection for BController to work as intended

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mwbranna · Oct 27, 2014 at 07:25 PM

For normal inheritance behavior, you aren't doing anything wrong.

Unity calls these delegate methods through reflection, not through compile-time code. The following sentence is speculation, but I'd say Unity internally detects both methods through reflection and manually calls them both. You can use "this" and "is" in each method to control which one will actually get called, but it's ugly :(

 public class AController: BaseController
  {
    protected virtual void OnDisconnectedFromServer ( NetworkDisconnection info )
    {
      if(this is BController)
          return;
      Debug.Log("Disconnected A");
    }
  }
  public class BController: AController
  {
    protected override void OnDisconnectedFromServer ( NetworkDisconnection info )
    {
      Debug.Log("Disconnected B");
    }
  }
 
Comment
Add comment · Show 1 · 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 Ssandokan · Oct 27, 2014 at 07:31 PM 0
Share

Yeah, it's ugly because if more classes inherit from AController i will need to check each one of them. It seems that this is the only solution for now though.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

remove spawned object from my networkView when timeScale = 0 0 Answers

Simple dedicated server on the VDS 1 Answer

Network call Method from Instants of Prefab 2 Answers

Where to put ShowFPS and ShowLatency for multiplayer game? 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