Search Unity

New to UNET - Question about syncing complex player data

Discussion in 'UNet' started by LootlabGames, Feb 8, 2018.

  1. LootlabGames

    LootlabGames

    Joined:
    Nov 21, 2014
    Posts:
    343
    I have been working on my game for about 6 years now(man time flies).

    I have decided I would like to try to add coop(Just 2 players).
    It is a ARPG type game with many different items(randomly generated).

    There are a few different data types that can be used on players.
    Two of them are effect's and condition's.
    Also condition's can contain 1 to many effects.

    One type of item that exists in the game is a potion.
    Potions can contain either an effect or a condition(or both).

    Scenario 1
    You(player A) throw a potion and it hits player B.
    The potion applies a condition that contains an effect that will decrease their strength for 5 minutes.
    The amount of strength decreased and the duration was created randomly when the potion was created.

    For this to happen player A would need to have the information about the potions(and the potions condition, and the condition's effect(s)) on player B's machine correct?

    Should the information about the potion and the potions condition be sent when player B's is hit, or should a inventory be replicated on all clients?

    If not what is the suggested method?

    P.S.
    I have searched the forums and read a lot about the subject.
    But I'm still not getting a good answer since most of them were not using complex objects.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Instead of Player A and Player B, think of things as game Host (server) and Client. The Host is generally where you would apply any effects, and you would update any changes relevant to the client using the various methods available for that (ClientRpc, TargetRpc, SyncVar, Messages, etc).

    So if the Host player throws a potion and hits the Client's player gameobject, the most straight forward way would be to apply that effect on the host computer, and then call a ClientRpc to inform the Client computer of this effect applied to the client's player gameobject. The Client doesn't actually need to know about the host player's inventory beforehand for this to work, but you certainly could sync the host player's inventory to the client anyway. Say you want the client player to be aware of the inventory to allow for item trading, or whatever.

    If the reverse happened, and the Client player hit the Host player with a potion, you would actually do it the exact same way. The effect is applied on the host, a ClientRpc is called, and everyone sees this effect applied. (The ClientRpc could for example kick off a visual effect of the potion that the client would want to see even though it affects the host player) Of course you'd want to sync the Client player's inventory from the Host to the Client so that they can see their own inventory in this case.
     
    Last edited: Feb 8, 2018