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

How to differentiate NetworkViews between client and server network views observing the same copies?

Discussion in 'Multiplayer' started by asperatology, Jun 2, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Server and client are two different players in a network game. Both players can spawn multiple numbers of the same type of unit in the game, and can control any number of spawned units owned by the player spawning them. Spawned unit is a prefab that contains a network view. These spawned units are all instantiated using Network.Instantiate(), therefore, they are copies or duplicates of each other.

    I am having a bit of trouble telling the difference apart from multiple copies of the same spawned unit that a player owns in the game. And since there are two players in the game, I need to put twice the effort made to differentiate the spawned units from each other.

    I know that NetworkView can use NetworkView.isMine to allow the player to access its own state, which in this case, all of the player's units.

    What is the recommended way of separating the player's spawned units apart, so that I can do actions per how many units that were selected by the player? Thank in advance.
     
  2. luquitadenuevo

    luquitadenuevo

    Joined:
    Apr 1, 2015
    Posts:
    8
    Hi Asperatology,

    I'm not sure I understand the issue. As you mentioned, NetworkView.isMine returns whether the receiver belongs to the network owner or not.
    If you want to implement some kind of fast access to a player's owned units maybe you can keep a list with them (adding or removing when creating or destroying), or depending on how you are doing the selection give different layers to units owned by different players and filter by layer.

    Does this help?

    Cheers,
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I will try using an example to make the issue more understandable. At least someone is telling me that my post is not understandable, and I was worried it is off everyone's radar. Your feedback is very appreciated.

    Anyway, the example I am going to use is a multiplayer network RTS game that can be played from 3 players to 100 players.

    As the game starts, an arbitrary number of players are on the map foraging for resources and battling against each other. Each player uses the same unit type from the start, and uses the same tech upgrade tree in the game (for simplicity). The main issue is, how do you tell the difference between each player's owned units apart? If I use Network.isMine, I can only tell the difference between my units and the rest apart. What about units not owned by me?

    Say that in my main camera, I have Player 4 and Player 88 duking it out near my base, and I have a line of sight of both of them. Therefore, my client Unity game needs to show that Player 4's units and Player 88's units are battling and are playing their attack animations. However, Player 4's units and Player 88's units are not owned by me. I would need to find a solution, a solution that doesn't seem to involve Network.isMine, to have those players' units doing something in my client.

    Since Player 4's and Player 88's units (the two players in my first post) are instantiated by Network.Instantiate() (their units are copies of each other) and Network.Destroy() if killed, how do I know which unit is which, such as telling the difference from each other (separating them)?
     
  4. luquitadenuevo

    luquitadenuevo

    Joined:
    Apr 1, 2015
    Posts:
    8
    Awe, I think I understand better now.
    This sounds more like a design problem that a network problem.

    In this simple scenario I would have a Team object, a Player object and a Unit object. Then you could ask each unit which team they are which it would infer by it's player.
    (aUnit has aPlayer, aPlayer has aTeam).
     
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    It's getting clearer, I think I know how to tell the differences. If possible, could I ask 1 more question? What if it's the other way around?

    There are two factions. (aTeam objects). Each faction has players (aPlayer objects). Each player has units (aUnit objects) The whole order is just flipped around. Would that cause a problem on a network game?
     
  6. luquitadenuevo

    luquitadenuevo

    Joined:
    Apr 1, 2015
    Posts:
    8
    Hey Asperatology,

    Not at all. Your problematic is absolutely independent from your network implementation.
    Just make sure to keep everything synched properly.

    Cheers,
     
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Alright, thanks.