Search Unity

Networkbehaviour adds requiredcomponent NetworkIdentity

Discussion in 'Multiplayer' started by wuzibu, Jul 16, 2015.

  1. wuzibu

    wuzibu

    Joined:
    Dec 15, 2014
    Posts:
    10
    Hello guys!
    The fact that NetworkBehaviour adds NetworkIdentity as a requiredcomponent brings a lot of pain for us. We have an EntityComponent (which inherited MonoBehaviour in the past) class, which is parent to every component in the game. Well now with Unet we were happy to change this EntityComponent's parent to NetworkBehaviour and (besides the network overhead) everything was fine, until we noticed that NetworkBehaviour forces every component to add NetworkIdentity without the possibilty to remove it again, because it is Required. The only way around this would mean to refactor about 200 classes just because this thing is marked as Required. Even though you get Errors when you happen to forget to add it. You don't plan on making this optional by any chance?
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    You should not simply make all monobehaviour a networkbehaviour. This is ultimately a bad idea. What you want to do is only make scripts a networkBehaviour when they actually NEED to network something. A lot of scripts will happen to not require this. What you should do instead is create your own NetworkEntityComponent or something like that which inherits from NetworkBehaviour, if you want to keep a similar route. This will probably mean some duplicate code in your case between EntityComponent and NetworkEntityComponent, but seems to be your easiest fix so far. A better idea yet is to work more towards interfaces, but this will require a lot more refactoring.

    Every networkBehaviour will add up overhead to the networking system which you will really want to keep to a minimum, because of bandwidth considerations (not everyone's got a 120mb/s connection) , CPU time and memory to a certain extent.
     
  3. wuzibu

    wuzibu

    Joined:
    Dec 15, 2014
    Posts:
    10
    Yeah that's exactly what I am saying. Thanks for your input, though. I was already pretty concerned about the overhead and you proved that thought. Guess I have to refactor the whole thing *sigh*.
     
  4. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I'm currently experiencing a problem (which I'll post in a new thread) but just to be clear @wasstraat65, you're saying for example, our player object should have only one network identity and behaviour? I have a player and its child weapon, both have a networkbehaviour and identity (another problem is I can't spawn the weapon or it spawns the whole player with it).

    Is that bad? Should I send all the data from the gun (like hits and damage) to the player script, if I want it doing network stuff like applying damage to the other player?
     
  5. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    You can attach multiple NetworkBehaviours to your objects. It just needs a networkIdentity attached (which Unity will attach automatically when you attach a NetworkBehaviour).

    What I did was adding a ´PlayerInput´ script (a NetworkBehaviour) to the root player object that simply sends keyboard input to the server. All the processing for shooting weapon, changing weapons etc is done on the server only. The server sends messages back to the client when they get hit. Thats where the client takes over again to respond by displaying damage HUD and stuff like that. This way I could network stuff without having to resort to networked child objects etc. This also is a more server authoritive way of doing things. If you still want to network child objects too, they will need their own networkIdentity and scripts attached. Note that only the root object can be your 'Player' in that case, unless you are on the Unity 5.2 beta where you can have local authority over more than one object.
     
    ChrisSch likes this.
  6. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Yes I noticed that, but I meant more like player having the identity, and player's child (the gun) also having one. Which causes a problem for me, because when I spawn the player, the gun is there but it isn't spawned(there's that spawn button in the inspector when it runs), and I don't know if it affects the game. Means its there but has no observers.

    I'm having a problem where upon death the player(ship) spawns its destroyed version, which works fine for the host, but all the clients get a double spawn, one correct and synced, and a dummy. :D Anyway I don't wanna impose on the OPs thread anymore, I'll create my own thread, maybe later today, after I run out of ideas to try.
     
  7. nonathaj

    nonathaj

    Joined:
    Sep 4, 2013
    Posts:
    8
    ChrisSch, did you ever find a way to activate the NetworkIdentity/NetworkBehaviour on children of a spawned object?

    I am trying to achieve the same functionality, with the base object controlling movement, and child objects controlling weapons over the network. Having to send all messages to the base would REALLY break the OOP design of our game...
     
    kakhlaghi2002 likes this.
  8. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Nope I didn't, I had to rework it a bit. Clearly either we don't know how to do it, or we're doing it work and its not suppose to work like that. I didn't work on that project in a while, and have to go to work now, but if I remember correctly for the gun part I removed the network identity and I had it run across network just like all other particles and actions (by having the parent activate them), but I made it only take effect from the player firing/doing it.

    Did you watch the Unity 5 Tutorials - Includes UNET Co-op survival? I think it might give you some insights as there's actual shooting involved. I was using particles as bullets, so a bit different. :D
     
  9. DigitalCandy

    DigitalCandy

    Joined:
    Jul 16, 2014
    Posts:
    5
    Just change NetworkBehaviour in your script back to monobehaviour and comment out any network related code, and it'll let you remove the network identity component. You can then remove the comments and you should be good
     
  10. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    Wow, I just had an annoying run-in with this same problem. Unity forcing you to use a network identity script without having the option to remove it (unless you completely comment-out all traces of Networking code from your scripts) is extremely annoying.

    I could see people having to change from NetworkBehaviour back to MonoBehaviour and then commenting out huge amounts of code in multiple scripts, just so they can remove the network identity component lol.
     
  11. DustinBoyle

    DustinBoyle

    Joined:
    Jul 30, 2015
    Posts:
    8
    I encountered the same issue when adding a network animator to a child object of a network identified object. Unity gives a warning but it seems to work fine.