Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Photon Unity Networking

Discussion in 'Connected Games' started by tobiass, Aug 23, 2011.

  1. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    1) OnPhotonSerializeView() this is called on any object with a PhotonView script, correct?

    Almost. It's called on all scripts that are "observed" by a PhotonView. So, if it's not in some view's "observed" list, it's not called.

    2) It is called each frame over the network? So not unity time, but PhotonNetwork.time?

    It's called every time an update for a PhotonView comes in or, when the client controls the PhotonView, it's called as often as defined by PhotonNetwork.sendRateOnSerialize.
    The reference comes with the source in the package. Also as PDF and there's the Api reference online.

    3) My tests to non local players, are not retuning any of my data for console read. So OnPhotonSerializeView() if photonView.isMine, I get the data I expect. If !photonView.isMine, I do not get any data..... So I am puzzled as to why this might be.

    This is hard to tell. If you managed to get calls of your OnPhotonSerializeView, you should be able to put data into the stream. On another client, you should get calls on the matching networked object, that passes the data from PUN back to your logic.

    Make sure you use more than one client, that the scripts are observed and that your GameObject is "network instantiated" via PhotonNetwork.Instantiate().

    In doubt, this tutorial hopefully helps.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,419

    Ok thanks.
    I think I got it. In fact I do, it was more so double checking.....
    A quick question.

    If I run a photonView.RPC(), from Player1, photonView.isMine (which was instantiated over the network), this RPC is fired on all Player1's on the network. However, Player2 holds the same [PunRPC] method, just called by Player1. Is the RPC smart in the sense it only contacts Player1?




    edit:
    I have another important question.

    In order to try an again a constant frame rate over the network, I have asked each local Game Mng (an object not instantiated over the network, but simply exists in scene already. each one (1 per client), has its own photonView), to OnSerializeView(), to run this method...
    Code (csharp):
    1.  
    2.     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    3.     {
    4.         if (PhotonNetwork.connected)
    5.         {
    6.             //reference for all players on this client.
    7.             pun_timeCrnt = PhotonNetwork.time;
    8.             pun_timeDelta = (float)pun_timeCrnt - (float)pun_timeLast;
    9.             pun_timeLast = pun_timeCrnt;
    10.             print("pun_timeDelta " + pun_timeDelta);
    11.         }
    12.     }
    13.  


    however, going on your last answer, and revealed in my tests, the client is not called here. So, my question is, if I want to rely on a delta Photon Time to insure a consistent update on clients (for animation speed etc), how can I better achieve this?

    Any ideas?
     
    Last edited: Jun 11, 2018
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    > Is the RPC smart in the sense it only contacts Player1?
    It targets the PhotonView on which you called it initially, yes.
    https://doc.photonengine.com/en-us/pun/current/gameplay/rpcsandraiseevent

    As you say, you need to include the "network traveling time" for the messages to calculate (more) accurate positions.
    You can add your script to the "observed" list of your PhotonView and thus include it in the regular updates. Alternatively you can send the PhotonNetwork.ServerTimestamp in messages which you send via RaiseEvent().
     
    renman3000 likes this.
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,419
    Thanks.


    So @tobiass (or anyone knowledgable),
    My main concern now is consistency of frame rate across network. Obviously, each client may have different local frame rates. One computer might be really fast, and another really slow. For example, if I run one build from my editor, and another as a standalone, the standalone is about double to speed/frame rate.

    Since PhotonNetwork.time is always the same, how can I best use this fact to dictate frame rate, if possible? For example, I am debating using FixedUpdate() instead of Update() on most functions. Do you have any insight to this concern?
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,419
    Hi,
    For some reason, an RPC called is causing an error that...


    ... thing is, this game object holds both the photonView, and the script with the [PunRPC] "thisMethod".


    Any ideas?
    ---------------
    edit:
    SOLVED.

    I had placed a variable between the [PunRPC] and the method.
    so this (which works)...
    Code (csharp):
    1.  
    2. int x;
    3. [PunRPC]
    4. void method(){}
    5.  
    Was this....
    Code (csharp):
    1.  
    2. [PunRPC]
    3. int x;
    4. void method(){}
    5.  
     
    Last edited: Jun 14, 2018
    tobiass likes this.
  6. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
  7. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    this is what i have so far, movement is tiny bit better but it's still jittery when object speeds up,

    I have 2 Bools sent one for Gas and one For Brakes, if any returns true the object increase or decrease speed, if none of those is true, the object keeps moving forward at the same speed...

    Smooth Function is called at the top with the if view is mine else function
     
  8. Kufufami

    Kufufami

    Joined:
    Feb 24, 2017
    Posts:
    5
    Hi. I'm having problems with the PhotonNetwork not being able to recognise the "WebSocket" reference, do you know what the reason to this could be?
     

    Attached Files:

  9. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    577
    I don't know if there's any example project available to learn, but I have no idea of how to control NPCs. I can spawn them, but it seems that two AI bots do the same in game that is when one shoots another does the same and when one is killed the other is killed too. I have the following code:

    Code (CSharp):
    1. if (this.photonView != null && !this.photonView.isMine){
    2.             this.enabled = false;
    3.             return;
    4.         }
    in all of the NPC scripts (Awake or Start funcion), but it doesn't work somehow.
     
  10. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,419
    I am running some tests to see if the player is online or not. I get an error from the NetworkingPeer.cs line 1578...

    Code (csharp):
    1.  
    2.     #region Implementation of IPhotonPeerListener
    3.  
    4.     public void DebugReturn(DebugLevel level, string message)
    5.     {
    6.         if (level == DebugLevel.ERROR)
    7.         {
    8.             Debug.LogError(message);
    9.         }
    10.     }
    11.  
    My issue is, that I can not call my own scripts from here, (as they are under a different NameSpace (perhaps a different reason)), that said, I need to alert a custom manager that this (can not get online) is the case.


    How can I best check if client is online?

    I had used this, but it did not work.
    Code (csharp):
    1.  
    2.         if (Application.internetReachability == NetworkReachability.NotReachable)
    3.             {
    4.                 //Change the Text
    5.                 List<string> s = new List<string> { "You Appear to Not Be Online. Please Check Connection." };
    6.                 Home_Mng.ins.init_animateAlert(s, 1);
    7.             }
    8.  
    9.  
    10.  
     
  11. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    The WebSocket class is wrapped in "ifdef" checks and only compiles when the export is set to WebGL or if your project defines WEBSOCKET. This should help adding it, even to other exports. We don't actively support WSS on platforms other than WebGL.

    Your code seems to deactivate the NPC behaviour. That should be OK but you should check it does what you want it do. In the inspector, you can see which scripts are active. Check this.
    Aside from that, it's hard to guess what else you do, to make the NPCs act. You might have to do a small repro case and put this in a new forum thread to ask for help. I am not sure if we (Photon team) can help you soon.

    This method is not intended to be used for your game/UI logic. This is for debug messages only, which can change anytime.
    What you're looking for is likely PhotonNetwork.connectedAndReady or simply connected.
    Also, you should have a good look at the callbacks PUN offers, as those will simply inform you when the client disconnects.
    Application.internetReachability is a value provided by the engine and could be used to give more specific info to the player. It tells you if a device can reach the internet (or (my guess) more specifically a Unity server) at all. So if this is false, PUN (and anything connected) won't work either on the device.

    Hope that helps.
     
    renman3000 likes this.
  12. Tomza

    Tomza

    Joined:
    Aug 27, 2013
    Posts:
    577
    All, OK. I just copied the script and some delegates with events were copied too. All instances of my enemies were subscribed to them. Now, all works great. Thank you!
     
    tobiass likes this.
  13. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,521
    Does this work without errors in Unity 2018.1.3f1?
     
  14. Baraff

    Baraff

    Joined:
    Aug 16, 2014
    Posts:
    240
    Currently running Unity 2018.1.6f1 here without problems.
     
    tobiass likes this.
  15. Athalos

    Athalos

    Joined:
    Jul 21, 2014
    Posts:
    23
    Can anyone confirm which On-Premise Server version we should use with what I assume is the latest PUN package for Unity 2018.1.6f1 (1.90)? We are having issues porting from 2017.xx to 2018. Demo project will not connect to cloud using TCP... not sure if that is supported.. any help would be much appreciated!
     
  16. castana1962

    castana1962

    Joined:
    Apr 10, 2013
    Posts:
    265
    Hi I am trying the Unity 2018.2 beta with Photon Unity Networking unitypackage and download from Unity Assets Store and I got the following error
    Assets/Photon Unity Networking/Editor/PhotonNetwork/PhotonEditor.cs(675,46): error CS0619: `UnityEngine.RPC' is obsolete: `The legacy networking system has been removed in Unity 2018.2. Use Unity Multiplayer and NetworkIdentity instead.'
    I have read about this topic here
    https://github.com/google-ar/arcore-unity-sdk/issues/197
    and it seems that it was fixed, for it Is it like that? If it is correct where I can get this last Photon Unity Networking unitypackage? or How I could fix it?
    Thanks and sorry my little English
    Alejandro
     
  17. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    @Athalos: Anything in the console logs? Please try UDP, too. In general, TCP should work as before, so any logs you could provide would be welcome. Mail to: developer@exitgames.com

    @castana1962: Unity 2018.2 will also complain about PUN v1.90 but this can be fixed really (!) simple:
    Double-click the error in console and in the code editor, comment out the block you are taken to. Done.

    snip.png
     
    castana1962 likes this.
  18. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    Just submitted:

    v1.91 (6. July 2018)
    Fixed: Support for Unity 2018.2. removed [RPC] attribute support no longer available.
    Fixed: PhotonNetwork.OnEvenCall is now an event to prevent wrong usage and registrations. PunTurnManager and InRoomRaiseEvent EventCall Registration now add themselves to the event properly.
    Fixed: Bug which prevents from enabling the Offline Mode for a second time after a room has been joined in Offline Mode and the Offline Mode has been disabled afterwards.
    Added: NetworkingPeer.cs: Clean up Level Async Loading process watcher if we leave the room, close connection. Also better handle loading process when already joined.
     
    castana1962 likes this.
  19. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    @tobiass

    In this thread the poster made the following statement regarding the 500 msg/s room limit <The 500msg per room with Photon are not a hard limit, but a recommendation.>

    Can you confirm if this is indeed right, and that the limit is a quality of life recommendation rather than a hard limit?

    I have always been under the impression that exceeding the limit would result in messages being dropped by the relay server.

    Thanks.
     
  20. Athalos

    Athalos

    Joined:
    Jul 21, 2014
    Posts:
    23
    @tobiass we ended up getting it working with UDP but TCP fails when connecting to our On-Premise Server. We used TCP to support live-streaming - it seemed a bit faster and more stable than UDP, however with the update and some testing UDP seems more stable now.

    The error we are getting with TCP is: Receiving failed. SocketException: ConnectionReset. This is on attempting to connect to Master Client.

    Our On-Premise Relay server (latest) is sitting on an Azure VM running Windows Server 2016. We followed this for setting up ports on the firewall config.

    Last note, we are also using Photon Voice.

    Any tips would be greatly appreciated!
     
  21. Athalos

    Athalos

    Joined:
    Jul 21, 2014
    Posts:
    23
    Also interested in any update you may have regarding the release date/news for Pun2!
     
  22. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,068
    @Munchy2007: I don't know yet, who was writing this. Let's say it this way: It is a limit, but we also found solutions for titles that went over this limit. In the end, if you're above that limit, you are very likely also above the traffic limits, which we have to charge. In doubt, get in touch with us. We will try to make things work!

    Hmmm, the previous post sounded like your client won't connect to the Photon Cloud via TCP but you're using your own server. As next steps, we could check the server's logs and see if there is a TCP connection which gets shut down, or if there never was any (then some configuration is incomplete or missing in the guide).

    We are more or less done with the refactoring for PUN2!
    The plan is to update internal test projects, release one more update via the forum(s) and then polish documentation (the manual doc page needs a redo). It's making a very stable impression to us but of course, there has been a limited amount of projects that use it so far.
    But yes, overall PUN2 is very close to release.
     
  23. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    I'm sorry for the stupid question, but i'm very new to learning Photon (And multiplayer in general)

    EDIT: Changing the question: when we call PhotonNetwork.Instantiate on object with photonview, Is the object's local authority Always given to the local user?
     
    Last edited: Jul 6, 2018
  24. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    @tobiass
    Thanks for the clarification. Presently the limit isn't a problem for us, however it's nice to know that should it become restrictive in future there are options to be explored.
     
  25. castana1962

    castana1962

    Joined:
    Apr 10, 2013
    Posts:
    265
    Hi @tobiass Thanks for your help !!!
     
  26. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
    Hi,

    yes, the .isMine property will be true for your local instance.

    Bye,

    Jean
     
  27. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    Thanks,

    Are there any Map / Mode Rotation tutorial for me to follow? can't really figure it out
     
  28. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
    Hi,

    What do you mean by "Map / Mode Rotation" ?

    Bye,

    Jean
     
  29. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203

    Hmmm, it means when user create a game, the user specifies a list of maps for the room, then after each game is ended it will move on to the next map.

    I probably will need to use a Queue however i have no idea how to use a queue in this situation with Photon.
     
  30. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    Assuming you want everyone to change map at the same time you can use PhotonNetwork.LoadLevel(string levelName) or one of its variants https://doc-api.photonengine.com/en/pun/current/class_photon_network.html, assuming you store your map rotation in a queue of strings.

    This has the added benefit that any new clients that enter the room will automatically load the current map.
     
  31. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    The part i don't understand is how to store the map rotation queue With Photon CustomRoomProperties ?
     
  32. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
    Hi,

    you don't need to maintain a queue inside the CustomRoomProperties, you should actually not maintain anything, as @Munchy2007 said, you should use PhotonNetwork.LoadLevel() coupled with PhotonNetwork.automaticallySyncScene

    http://doc-api.photonengine.com/en/...etwork.html#af721675dc5cf3c5ac4d4b67339246cdf

    you do that from the MasterClient, and so the masterClient has your queue of maps, hardcoded in your logic or using some database based on your game or room setup, but it's outside Photon, your logic decides what scene to load and PhotonNetwork.LoadLevel() takes care of putting all players in that room in that scene.

    Think of room custom properties as a way for all players to have access to the same data, it should be kept to its very minimum as it impact bandwidth when user join ( all custom room properties needs to be sent to that new joining player).

    Maybe in your case it will be a combination of using LoadLevel() and some room custom properties to define how that level will behave depending on your current game context.

    You should check the pun basic tutorial, it will show you how work with loadLevel():

    https://doc.photonengine.com/en-us/pun/current/demos-and-tutorials/pun-basics-tutorial/intro

    Bye,

    Jean
     
  33. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    Would this work if the masterClient changed?
     
  34. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    There's no reason why it shouldn't. You could listen for
    void OnMasterClientSwitched(PhotonPlayer newMasterClient)
    and on the new master client you could query the currently loaded map to determine the current position in the queue of maps. This way you wouldn't need to worry about synchronising the current state of the map queue across the network.
     
  35. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
    Hi,

    yep, of course, you listen to masterClient being changed and the new masterClient takes over, the new masterClient knows about the current context since he's in the room as well, so he simply takes over the job. Each client is potentially the masterClient, so all the codes and logic is already available for any client to become the masterClient

    Bye,

    Jean
     
  36. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    When making a room, User specifies a queue of maps to include in the room.. a Queue is created. The Current Map gets stored in the room properties as a string for users in the lobby to see and load when they join. Now lets say the master (who has the randomly made Queue) leaves, how do I transfer the whole Queue to the new master's Map Queue Script and not only the current map??
     
  37. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
    Hi,

    if the user is responsible for creating the queue for the room, then yes, it can go in the room properties, I assumed the queue of maps where defined at edit time. However, if the queue of map is a long lists, then it's better to store this information in a database that you host yourself and pair with the room Id, players when entering the room check against the database for the infos on that room. But if the queue of room is not 100 hundred items or more then it's ok. this has to do with bandwidth, you could also have hashes for your maps so that they are only referenced as bytes, then you can have a fairly long ist of bytes, which is always better than a long list of strings.

    Bye,

    Jean
     
  38. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    311
  39. BlueBolt_

    BlueBolt_

    Joined:
    Feb 11, 2018
    Posts:
    20
    What are examples of ideal uses of RPCs and RaiseEvents?

    I just read about RaiseEvents, and so far it seems to be just like RPC except object won't require PhotonView?

    So maybe things like if you shoot 100 bullets in fast succession it would be better to use RaiseEvents? Just trying to get examples so i could understand better.

    Thanks
     
  40. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    Speaking for myself, I use RaiseEvent as it gives me better control of which clients get sent data.

    I find it much better in particular for handling situations where not all clients are in the same scene and there are networked scene objects (such as mobs and other interactable items) which need to be synchronised but won't exist on all clients due to the difference in scenes. RPCs complain if there isn't a corresponding target on every client.

    In our latest game (a multiplayer RPG) we haven't used RPCs at all, preferring RaiseEvent for all communication.

    Regarding your example of firing bullets, using RaiseEvent over RPCs could help if you used it to implement 'interest management' to restrict sending updates to clients that were out of view for example.
     
    Last edited: Jul 27, 2018
    BlueBolt_ likes this.
  41. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    Can I use the same AppId for photon Chat as I do for Photon PUN. IF not is it possible to buy a $100 100ccu for photon chat so I can have a 100 ccu for both.
     
  42. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    203
    What type of methods would be used to sync position of a constantly moving objects? for example a Plane game, the plane might slow down or speed up (with min and max speeds) but it will always be moving and somewhat predictable but in a fast velocity, what are some of the good methods to sync this kind of behavior without using too much traffic?

    thanks
     
  43. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    I'm trying out Photon chat for the first time.
    i see now you can buy a 100 ccu for 5 years. I guess thats a pretty long time.
    Id like to buy 100 life time so it matches with my other 100 life time.

    Mainly so i can use those two things for alpha releases.
     
    Last edited: Jul 28, 2018
  44. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    How many users can Photon Chat handle per channel?
    Does each channel use like a separate cloud instance?
    So like if i limit it to 1,000 per channel can I have a unlimited amount of channels with 1,000 people in each channel?
    Can it ever get maxed out with that example?
     
  45. BlueBolt_

    BlueBolt_

    Joined:
    Feb 11, 2018
    Posts:
    20
    Thanks!

    For anyone: quick question about PhotonStreams, I'm only using the X and Z positions, so in this case is it better to send the Vector3 Position (as 1 stream entry) or two X and Z floats (as 2 stream entries) i don't know exactly how it works here and which is lighter on the network...
     
  46. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    I can't figure out how you send a Hashtable as a chat message.
    Can someone type me up an example please

    Code (csharp):
    1.  
    2. Hashtable CharacterData = new Hashtable();
    3. CharacterData["id"] = 1;
    4.  
    5. chatClient.PublishMessage(Channel, CharacterData);
    6.  
     
  47. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
  48. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    Ya i was reading about that but there is no Photon.Hashtable
    must be called something else?
    My editor says that doesn't exist.

    I actually came up with a better solution using JSONNode.
    well idk if its better. Not sure which one is faster.

    I have to do JSONNode.ToString() and then I have to convert the returning object into a string for some reason and then convert the string back to JSONNode.
     
  49. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    959
    Here is an example of what Im doing

    Code (csharp):
    1.  
    2. JSONNode Data = new JSONObject();
    3. Data["id"] = 1;
    4.  
    5. chatClient.PublishMessage(channel, Data.ToString());
    6.  

    Then on the return request
    Code (csharp):
    1.  
    2.     public void OnGetMessages(string channelName, string[] senders, object[] messages){
    3.  
    4.      JSONNode characterData = JSON.Parse(messages[0].ToString());
    5. }
    6.  

    So using this way seems im doing a lot more work having to convert it to a string twice.
     
  50. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    950
    Code (CSharp):
    1.    using PhotonHashtable = ExitGames.Client.Photon.Hashtable;
    2.  
    3.     void Example()
    4.     {
    5.         var table = new PhotonHashtable();
    6.     }
    However if your solution works then it's all good :) I'm not sure if either one is better than the other.