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

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! :slight_smile:

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

This is how you do it:

//local variables
public Transform sender;
public Transform target;
...
photonView.RPC("SomeFunction", PhotonTargets.All, sender.gameObject.GetPhotonView().viewID, target.gameObject.GetPhotonView().viewID);
...

[PunRPC]
void SomeFunction(int senderView, int targetView){
sender = PhotonView.Find (senderView).transform;
target = PhotonView.Find(targetView).transform;
//do stuff with the transforms(works with GOs, rigidbodies or any other component of the GO)
}

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 :stuck_out_tongue:

-Cheers

11 Likes

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.

// call code:
photonView.RPC("ChatMessage", PhotonTargets.All, "jup", "and jup!");

// the RPCs implementation:
[PunRPC]
void ChatMessage(string a, string b, PhotonMessageInfo info)
{
    Debug.Log(String.Format("Info: {0} {1} {2}", info.sender, info.photonView, info.timestamp));
}
1 Like

Its not the same as what im doing :slight_smile:
Im not sending to another object with a photon view, but im sending the current object’s variable of which target it has gotten :wink:

But yes i see that this way would also work!

Hehe, you’re as picky as I am :slight_smile:
You’re right.

Great! Helped me alot :slight_smile:

2 Likes

I was trying to find this solution everywhere! Thanks!

1 Like

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

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.

1 Like

Awesome, thanks dude! You really helped us out a lot in our project. <3 xoxo and so on :smiley:

2 Likes

thanks you

THANK YOU SO MUCH YOU DONT KNOW HOW MUCH THIS HELPED

This is so cool man:hushed: