-1

I am trying to implement multi-player networking with Photon PUN 2 in a Unity game. I have only been looking at this for two days, so I am learning as I go. Sorry for the long question:

I have managed connection, the lobby, creation / joining rooms etc, and am moving on to transfer of real time information during the game play. Where I am stuck due to not understanding the concepts well enough.

It is a very simple test game: some cubes that tumble around a square field - just to check concepts.

Conceptually, there are two types of player - 'my player', and 'other player(s)'. I control the movement of 'my player', and I want some information as to what any 'other players' are up to - however I do not necessarily want 'other player' positions and state etc just to be replicated in my (local) room - I want some information as to what they are up to, then I'll decide what to do with the information.

At start up time in the game play scene, I read the list of players in the room and for all that are not me (ie Player.IsLocal is not set - so not 'my player'), instantiate an 'other player' from a prefab.

'My Player' and 'other player' are really quite different types of object - the first moves around, the other just stores some information about what the remote 'other player' is up to.

Firstly, because of the above, although part of what I want is the 'other player' position, I do not want to use PhotonTransformView - I just want to get the positional (and other) information and know which 'other player' it came from. However when I attached a PhotonView to my local 'other player' copy and then set 'other player' as the observable object, the editor immediately stuck in a PhotonTransformView as well saying I needed that to replicate position. Well, I don't, but as soon as I removed the PhotonTransformView, it removed the observable reference as well! It appears if the object has a transform, it is going to get a PhotonTransformView whether it likes it or not.

  • Question 1 - how to I stop that?

Next, bowing to the inevitable, I put a PhotonView and PhotonTransformView on both 'my player' and 'other player' to see what happened. Well, I found that what happened was that as I moved 'my player' around, the 'my player' in the remote scene moved around - the 'other player' didn't move at all. From that I conclude that information appears only to be transferred between two objects of the same type? Ie local 'other player' information goes only to remote 'other players', ditto 'my player'. So

  • Question 2 - how to I transfer information from object type A in the local scene to object type B in a remote scene?

I suppose what I really want to do is this: in the local scene I want to send out a package of information identified as from me (presumably by ActorNumber). I also want to receive packages of information from remote scene(s), in each case also identified by Actor Number, and having got them, I can decide what to do with them. I do not really want automatic transfer of any specific piece of information. I gather IPunObservable is the relevant interface to support, so really, what I want is a free standing script implementing IPunObservable in which I cam use OnPhotonSerializeView() to read and write the information. Not sure how - to have a freestanding script, I appear to need a GameObject to attach it to, GameObjects have Transform components that it appears you cannot remove, so I'll be back forced to use PhotonTransformView. So

  • Question 3 - can I do that, if so, how?

I am not really concerned whether one thinks what I am trying to do is sensible or not - this is really just a learning exercise to find out how the architecture works and what it allows. Once I have figured that out, then I can decide on real implementation. But - first comes the knowledge.

So, on that basis, any explanatory help gratefully received!

nmw01223
  • 1,611
  • 3
  • 23
  • 40
  • [Docs](https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/player-networking]): Scroll down to "Beams Fire Control". – Iggy Jun 24 '20 at 08:47
  • Thanks, but that link is broken actually, and I've been through all that. Didn't help me. – nmw01223 Jun 24 '20 at 10:03
  • Sorry, [fixed it](https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/player-networking). You should try the example projects. I use IPunObservable in my current project to serialize and deserialize custom data over the network and it works well. – Iggy Jun 24 '20 at 10:49
  • I have tried the basic sample project. It didn't help, in fact I cannot get the IPunObservable callback to work at all (never gets called). – nmw01223 Jun 24 '20 at 11:09
  • You need to add it to the PhotonView Observed Components list. You must also instantiate your PhotonView with PhotonNetwork.Instantiate. – Iggy Jun 24 '20 at 12:11
  • Thanks. I've taken a different route. For now I am going to try to ignore PhotonView entirely and do everything at a lower level using RaiseEvent - at least I should have full control. There's a telling statement on the Photon RPCs and RaiseEvent page: "Under the hood, PUN also uses RaiseEvent for pretty much all communication". So I'm assuming all the higher level stuff is probably built using RaiseEvent in the first place. – nmw01223 Jun 25 '20 at 10:58
  • That makes sense. I had a quick look at some low-level Photon code that seems to be the case. – Iggy Jun 25 '20 at 11:26
  • Btw, if you're not utilizing Photon completely, you might consider going lightweight with something like [LitNetLib](https://github.com/RevenantX/LiteNetLib). Good luck! :) – Iggy Jun 25 '20 at 11:33

1 Answers1

1

This may not be the 'normal' answer, but I have come to the conclusion that it is by far the simplest solution to avoid the higher level 'helpers' (PhotonView etc), and just send all message using RaiseEvent() / OnEvent().

That way you can send what you want, how you want, when you want and to where you want. Then just build your own logic on top. With that, possibilities are very wide.

There is a telling statement in the Photon docs to the effect that this is the underlying mechanism used by Photon.

nmw01223
  • 1,611
  • 3
  • 23
  • 40