[Solved] Adding Photon View via code on Unity
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on Realtime.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
[Solved] Adding Photon View via code on Unity
Egonaraujo
2021-09-07 17:21:15
Hello!
I am trying to develop a multiplayer funcionality in a Unity-based engine.
The way I went with it is to allow the user to add a "Network Behavior" on its object.
The behavior will add a
PhotonView
and a
PhotonViewTransform
Component on the gameObject
Running in single player works normaly as expected, considering that the views will have no effect
The problem is that, when running in a multiplayer setting, it doesnt update the rotation and position of the objects..
The object with the view is already instantiated when the connection with the server is created, the game loads asynchronously before letting the player either "Play" or "Play Multiplayer", and connecting with photon before the play multiplayer option is tapped makes no sense
In a bulletpoint view:
- instantiate object A
- Add PhotonView and PhotonTransformView to object A
- Connect with PhotonNetwork
- Join Photon Room
- "StartGame" (start the runtime mode of the engine)
My question is how to make the PhotonView send updates to the game server? (I still have to manage Master/Client access to the objects, but for now they dont even update positions)
Comments
You should not add PhotonViews to objects while not in a room. They define a network id for the object in question and that is per room. If you are outside of rooms, this doesn't do much and is not supported.
Turn it around and it would work. Join a room, get your actorNumber and assign ViewIds.
Make sure the PhotonView has some scripts in the Observed list. You can use photonView.FindObservables().
Egonaraujo
2021-09-08 16:15:08
Thank you for your input!
For some reason my transform view wasnt really getting observed so I added some redundant code and it worked.
The correct path for it that you described also guided me through the correct documentations where I could fix my problem
For next users (This is a prototype, do not consider this a good architecture decision):
Glad to know you could make it work.
Are you sure that AllocateViewID(0) is doing what you want to do? Read it's API reference, please.
If you allocate the viewID for the player who is creating the PhotonView, the control of the object is already assigned to that player and you don't have to assign it.
If OnPointerDown can be called frequently, it's not a good idea to call TransferOwnership each time. It queues a network messages, which goes to everyone with some lag. It seems any client can do this, so you will run into situations where several TransferOwnership messages fly around and it's unclear whos' duty it is to send updates of the PhotonView.
It might be my limited overview of your project but I don't think this is a good approach.
Back to top