- Home /
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.
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.
my problem is complete opposite, i WANT to override it and even when i dont call base.OnDisconnectedFromServer(); it still gets called somehow
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.
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.
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
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");
}
}
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
Follow this Question
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