Search Unity

Third Party How To: Photon Networking - Send GameObjects,Transforms and other through the network!

Discussion in 'Multiplayer' started by nevaran, Jul 27, 2015.

  1. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Hello everyone!
    For quite a while i have been struggling to find a good solution on how to sync some types of variables through the network(example: target of an enemy player to be another player object).
    For a long time i have searched of a solution in the forums, but havent found anything. This is why im making this thread! :)

    This has been tested in Photon Networking only, but it may also work in uNet!

    This is how you do it:
    Code (csharp):
    1.  
    2. //local variables
    3. public Transform sender;
    4. public Transform target;
    5. ...
    6. photonView.RPC("SomeFunction", PhotonTargets.All, sender.gameObject.GetPhotonView().viewID, target.gameObject.GetPhotonView().viewID);
    7. ...
    8.  
    9. [PunRPC]
    10. void SomeFunction(int senderView, int targetView){
    11. sender = PhotonView.Find (senderView).transform;
    12. target = PhotonView.Find(targetView).transform;
    13. //do stuff with the transforms(works with GOs, rigidbodies or any other component of the GO)
    14. }
    15.  
    The trick in this is to send the photon view's VIEW ID, and then using PhotonView.Find to find that component, and fetching the object that it is attached to.

    I hope this have helped some of you, who try to sync objects and such like i need to in most of my projects!

    If anyone has questions, dont be shy and ask away :p

    -Cheers
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I'm sorry to read you have been looking for this so long. It's cool you're sharing your findings, which are one way to do it.

    Alternatively:
    If you call an RPC, the other clients will execute it on the same object / PhotonView. In the rpc method, you can use the photonView of that GameObject as target.
    You can add a final parameter to RPC methods, even though you never add this to the call's arguments. Add a PhotonMessageInfo. The receiving clients will identify the sending player and pass it in the PhotonMessageInfo object.

    Code (CSharp):
    1. // call code:
    2. photonView.RPC("ChatMessage", PhotonTargets.All, "jup", "and jup!");
    3.  
    4. // the RPCs implementation:
    5. [PunRPC]
    6. void ChatMessage(string a, string b, PhotonMessageInfo info)
    7. {
    8.     Debug.Log(String.Format("Info: {0} {1} {2}", info.sender, info.photonView, info.timestamp));
    9. }
    10.  
     
    marcospgp likes this.
  3. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Its not the same as what im doing :)
    Im not sending to another object with a photon view, but im sending the current object's variable of which target it has gotten ;)

    But yes i see that this way would also work!
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Hehe, you're as picky as I am :)
    You're right.
     
  5. JannickL

    JannickL

    Joined:
    May 18, 2016
    Posts:
    78
    Great! Helped me alot :)
     
    SpiffingGoldfish and tobiass like this.
  6. UDN_b7e02a1f-c47d-43ec-877f-367ff4696609

    UDN_b7e02a1f-c47d-43ec-877f-367ff4696609

    Joined:
    Feb 13, 2017
    Posts:
    1
    I was trying to find this solution everywhere! Thanks!
     
    SpiffingGoldfish likes this.
  7. DigitalCandy

    DigitalCandy

    Joined:
    Jul 16, 2014
    Posts:
    5
    In order to avoid hundreds of photonviews, I'm maintaining a list of items in the scene that are (mostly) static, in a list on each seperate players computer. When they pickup or drop an item, its spawned or destroyed locally, and added/removed from their list, such that items are synced for everyone at all times, and RPC's are only sent when an item is picked up or dropped.

    The only way I've been able to identify an item a players picked up across all clients is to send its position via RPC, scan through that players scene item list, and perform the appropriate action. This works fine, except I'm trying to find a better way to locate the object than by passing in a Vector3 of its position and comparing it. As long as my objects are static its fine. However my dropped items have rigidbodies which get destroyed when they hit the ground. I'm aware objects will be oriented slightly differently across clients, and this is fine, also noone will be joining the game once its commenced. Rigidbodies obviously break my position comparing method. I was wondering what other way I could compare these scene items in my list to locate an object across clients
     
    Last edited: Aug 15, 2017
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    You could make up IDs instead of using locations. As long as you sync those, they will be easier to use and leaner to send.
     
    SpiffingGoldfish likes this.
  9. LeGoatie

    LeGoatie

    Joined:
    Nov 7, 2018
    Posts:
    1
    Awesome, thanks dude! You really helped us out a lot in our project. <3 xoxo and so on :-D
     
    SpiffingGoldfish and tobiass like this.
  10. ngochoang1

    ngochoang1

    Joined:
    Jun 19, 2021
    Posts:
    2
    thanks you
     
  11. NotCullis

    NotCullis

    Joined:
    Apr 19, 2022
    Posts:
    5
    THANK YOU SO MUCH YOU DONT KNOW HOW MUCH THIS HELPED
     
  12. sommeralexander47

    sommeralexander47

    Joined:
    Apr 24, 2022
    Posts:
    1
    This is so cool man:eek: