Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[UNET] Targeted RPCs or SyncVars?

Discussion in 'UNet' started by JonathanPearl, Dec 31, 2016.

  1. JonathanPearl

    JonathanPearl

    Joined:
    Jan 25, 2015
    Posts:
    15
    Hey there,

    I'm just starting out in UNET and it's pretty confusing trying to find the functions I need as I'm always unsure if functionality is the old Unity Networking stuff or a part of UNET. Anyway, I'm wondering if there is any way to only send RPCs to a single client as there are times where the data doesn't need to be broadcast to every other client and it is just wasteful. Example: If there are 16 people in the room and one person fires, the server should send the new ammo count to the new player as the other players don't NEED to know what ammo this client is currently at (in this usecase anyway). Sure you can have conditional logic in the receipt of the RPC but at that point you just sent off 15 buffers for nothing. SyncVars are the same issue, when it's changed it will always send to every client. How can I get these updates only to send to the client they're useful for?

    Also, is there any documentation for how to get client authoritative variables working? SyncVar works for server authoritative but there doesn't seem to be an equivalent for client authoritative without just sending a command every frame which also seems wasteful and isn't as clean as the [SyncVar] attribute magic. It looks like NetworkTransform is able to do this by using the LLAPI but I feel like there has to be a better way to learn this than ILSpy'ing Unity's code.
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    In the new Unet there is no way to RPC to a single client, only to all. "Client Authoritative Variables" also do not work - all of (high-level) UNET is predicated on messages going from a Client to the Server, and from the server to the clients. So, the authoritative client would send a CMD to the server, which would then update a SyncVar, which would then update all the other Clients, in HighLevel UNET architecture.
     
  3. JonathanPearl

    JonathanPearl

    Joined:
    Jan 25, 2015
    Posts:
    15
    Are you sure about that? I just found an attribute called [TargetRPC]. I haven't been able to test it yet but it looks like it could be what I'm looking for. I may be wrong.

    And yeah, looking through the code there doesn't seem to be any way to have client authority automagicked in HLAPI. The NetworkTransform doesn't use CMDs or SyncVars and relies on the LLAPI which seems like a cleaner option. Ideally, I would have loved a [SyncVarAuthority] attribute which would let the variable be in control of whichever client had authority rather than just the server. In fact, I would have also loved a flag on those attributes that would let them only be updated on the client that owns it. I can see how this may hurt host migration, but not all games need host migration (and I'm pretty sure that's not currently working anyway?)
     
  4. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    Interesting, i think TargetRPC was added recently, as i didn't know about that one. We just moved the project up to 5.4.x from 5.3 and i'm not sure when we will move it to 5.5.
     
  5. JonathanPearl

    JonathanPearl

    Joined:
    Jan 25, 2015
    Posts:
    15
    Yeah, it looks like it's new to 5.4, not sure which minor release.

    Looking for a way to intercept the UNetWeaver to implement a [SyncVarAuthority] attribute.

    I don't suppose you would know how to get ahold of the assemblies before the UNetWeaver has been able to touch them? If not, what would be a good venue to ask? (Which subforum, who to email, or unity answers)
     
    Last edited: Jan 1, 2017
  6. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    Looks like TargetRPC lets you send to 1 client. But SyncVars automatically send to all NetworkIdentity.observers... and I do not believe there is a way to specify who observers an object (basically all clients observer every object baring brief periods before spawning or during scene transition).

    I had this same problem and I ended up scripting CustomSyncVars, but used UNET messages to send data. This lets you only send CustomSyncVar data to 1 specific client. It's a LOT of work to do this... but if the wasted network bandwidth of sending SyncVar data to all clients is really that important to you then I believe this is the only solution. I mainly did this to avoid SyncVar bugs where they don't sync properly to all clients or SyncVar hooks fail to be called, but improved network bandwidth is a bonus.
     
  7. Yany

    Yany

    Joined:
    May 24, 2013
    Posts:
    93
  8. aaronfranke

    aaronfranke

    Joined:
    Jan 23, 2017
    Posts:
    20
    Can someone provide an example of how to use the [TargetRPC] attribute?
     
  9. Deleted User

    Deleted User

    Guest