Search Unity

Photon Networking Guide

Discussion in 'Connected Games' started by bertelmonster2k, Mar 21, 2012.

  1. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    Last edited: Mar 21, 2012
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995

    Introduction
    Based on my best selling asset store package for Unity networking, The ultimate unity networking project, I am now introducing the Photon Unity Networking Guide (PUN)! Learn how to add multiplayer to your Unity games, no previous multiplayer experience required.

    Server hosting: This package is includes 6 months of hosting for one application (100CCU professional, worth $114).
    Contents: Includes 27 pages documentation, 14 tutorials and examples and in-depth background information. All in C# code, however PUN is fully compatible with JS so all lessons learned are easily transferable to JS.
    Updates: The project is maintained for at least Unity 3.X. If new important PUN features are added I'll include new examples to this project which you'll get for free by updating via the asset store.
    Re-usable: This package comes with full documentation introducing you to PUN and teaching you by using various examples. Much of the example code is useful for re-se in your own games.

    How is Photon Unity Networking different from Unity Networking?
    Unity networking works very well for small games, but for serious networking games the connectivity problems and bad connections are troublesome.
    I have developed the PUN plugin with as goal to create a Photon based networking implementation that's even easier to use then Unity networking, but far more powerful. PUN is a free plugin available on the asset store. PUN has to be combined with a Photon server OR by using the Photon cloud service.

    How is this guide different from the M2H Unity networking tutorial?
    This guide is based on my asset store Unity networking project. The code and texts have been rewritten to work with PUN, the texts have been improved and the code has been made even cleaner&clearer. Furthermore new PUN specific parts have been added to introduce powerful PUN features.

    Table of contents:
    Introduction
    Basic PUN concepts
    Getting started
    Tutorials:
    Tutorial 1: Connecting
    Tutorial 2: Sending messages using PhotonViews
    Tutorial 3: Authoritative servers
    Tutorial 4: Manually instantiating PhotonViewIDs​
    Examples
    Example 1: Chatscript
    Example 2: Game list
    Example 3: Lobby system
    Example 4: FPS game
    Example 5: Multiplayer without any GUI​
    Extras:
    Network Culling code​
    Further network subjects explained
    1. Best practices
    2. Interest management (using PhotonView.group)
    3. Anti cheating
    5. Manually allocate PhotonView ID's
    6. Manual cleanup: PhotonNetwork.autoCleanUpPlayerObjects=false
    7. Connectivity
    8. Network and level loading
    9. Room properties
    10. Versioning: keeping different game versions separated
    11. Improve performance
    12. Statistics​
    Unity tips​
    Feedback
    Feedback is very welcome. If you feel like something is missing let me know.

    Download

    Download from the Unity asset store

     
    Last edited: Jul 16, 2014
  3. aleksbgs

    aleksbgs

    Joined:
    Dec 25, 2010
    Posts:
    31
    Hi today i buy Photon Networking Guide where is licence 6 months of hosting for one application (100CCU professional)
     
    Vontarga likes this.
  4. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    Heya, thank you for your purchase.

    I forgot to attach the instructions to the project, unfortunately the subscribtion cannot be activated automatically, you need to send an email:

    I have updated the asset store pacakage to include these instructions in the readme, and also updated the included PUN version to the latest (v1.12).
     
    Last edited: Apr 24, 2012
  5. aleksbgs

    aleksbgs

    Joined:
    Dec 25, 2010
    Posts:
    31
    Hi i have error today in code Photon Networking Guide .After one good connection ,second connection give me this error !!!


    InvalidCastException: Cannot cast from source type to destination type.
    Connect1C.OnReceivedRoomList () (at Assets/PUNGuide_M2H/_Tutorial 1/Scripts/Connect1C.cs:108)
    UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
    NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1336)
    NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1158)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:79)
     
  6. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    Please change line 108:

    "foreach(Room room in PhotonNetwork.GetRoomList()){"
    "foreach(RoomInfo room in PhotonNetwork.GetRoomList()){"

    I'll include this fix in the next update.
     
  7. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Hi

    from what I understand the Photon Cloud is not really suitable for staging a FPS game with, say, eight players. Is this right?
    It could only be achieved if Photon is installed on a VPS?

    R
     
  8. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    It's perfectly sutiable for an FPS game, 16 players should also still be fine.
    However, what you are missing in the cloud is server side logic, so you should be OK with a setup where one of your clients acts as the authorative client. Otherwise, you'll need to host the Photon server(s) yourself indeed.
     
  9. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Thanks Leepo

    I am developing something where there is only simple movement and chat - I am not worried about cheating - so I wont need to worry about auth servers.

    My avatars are simple too.

    So the Cloud should be ok?

    I am confused about rooms and messages per second tho.

    If an avatar moves then the send rate can be 10 messages a second to the Cloud? Then if there are 10 other players, then the Cloud will need to send these moves to them. So that is about 110 messages in one second to convey one players move?

    Isn't the Cloud limited to 500 messages a second?

    Also with rooms - are they instances of a game? Is this right?

    thanks for your help :)
     
  10. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    Some clarifications:
    1) We currently do not limit the message rate (msg/s). At a certain point we may have to put a limit on it.
    2) Msg/s is an average across all rooms.
    3) We will soon display the msg/s counter in the Dashboard - so you see what you generate.
    4) We will allow to outnumber the limit. Currently this is free - and if we charge for it it will be super fair (as our current pricing).

    Photon Unity Networking (PUN) is configured for 10 updates/s. So with 10 players in a room you have about 10 msg/s * 10 * 10 = 1000 msg/s. This is indeed more than the limit - but do not worry too much about it now.

    Chris
     
  11. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    From my perspective absolutely.

    From a implementation perspective..in most cases a room stands for a game that is currently playing, yes.But you can of course choose to use a Photon room for a lobby-chat, or whatever.
     
  12. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Thanks Bertelmonster2k for your reply.

    So I would buy the $9 a month Indie license initially. I would like at least 20 people to be able to move and chat around my game level.

    So this would generate a minimum of 2000 messages per second on the Cloud.

    Your pricing states that there is a limit of 300 msg/per second for the 100 indie license.

    But you are saying that I do not have to worry about this for now? When would this become an issue?

    thanks
     
  13. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    Just do not worry and get your app running. We do no have plans to cut it for now.
     
  14. Tim Cottenham

    Tim Cottenham

    Joined:
    Nov 10, 2011
    Posts:
    2
    Just bought the Photon Networking Guide, very interested in learning Multiplayer via Photon.

    Only just started but I already have an error while playing with "Tutorial 2: Sending messages using PhotonViews".
    When I try to connect the second client to the master client I get the following error:

    createGame failed, client stays on masterserver: OperationResponse 227: ReturnCode: 32766 (A game with the specified id already exist.). Parameters: {}.
    UnityEngine.Debug:LogError(Object)
    PhotonHandler:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:125)
    NetworkingPeer:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:700)
    NetworkingPeer:OnOperationResponse(OperationResponse) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:831)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:79)

    What did I mess up to cause this already? ;)

    Edit: Sorry, just assumed the fix above (posted by aleksbgs) was already updated in the project. It's working now.


    Another quick note about "Tutorial 2: Sending messages using PhotonViews".
    In the third piece of text it states "the master client is automatically the of all Photonviews".
    I guess something is missing between "the" and "of"?
     
    Last edited: May 10, 2012
  15. logo32design

    logo32design

    Joined:
    May 13, 2012
    Posts:
    1
    these photo networking guides are awesome.i really like.
     
  16. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    @Tim:
    Sorry for the confusion. I have fixed the document but that too will be live only with a new update.
    (Hilversum he?, zie je misschien bij een volgende GGJ daar!)
     
  17. portgalaxy

    portgalaxy

    Joined:
    Dec 9, 2011
    Posts:
    55
    Considering switching to Photon from SmartFoxServer. The one thing I see that concerns me, is the 300 Msg/s. As stated above, 10 players would already be 1000 Msg/s? Yet the first indie license is good for 100 CCU. I do not understand how that matches up, if we have 100 CCU but can only have 300 Msg/s.

    We are developing an indie MMOG where players will explore environments, chat, customize characters, buy and collect items in the game and build their own spaces. I am fine with having 100 CCU to start out but is it not even possible due to the Msg/s restrictions?
     
  18. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    We are not restrict on the msg/s (this is per room!) restriction and do not plan to do so. We may have to cut off at a very high limit.
     
  19. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    A Few Questions

    Were starting a new game soon which will be multiplayer. After looking at the viking demo this looks to be what we need. My question is will someone on an iPad or iPhone be able to connect to the same game as someone on a PC or Webplayer? The only thing that needs to happen is all the players can walk around and talk to each other. Also can you check the room capacity? Example check how many people are in the room and limit that, and not allow new players to join that room?
     
  20. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,305
    > will someone on an iPad or iPhone be able to connect to the same game as someone on a PC or Webplayer?

    Yes, sure. You can communicate between all the platforms we support client side.
    If you did cross platform to Flash (AS3 library) or JavaScript (for HTML5 games) you could do it but have to stick with the data types these languages support too. Aside from that: Not a problem.

    > check how many people are in the room and limit that, and not allow new players to join that room?
    Sure. You can set MaxPlayers per room creation. This is available in the room listing, together with the current player count. Even if a room is not at max capacity, you can close and or hide it. We don't have passwords but some are using properties for that.

    Have a look at the demos from the Asset Store. They are free and setup within minutes.
     
  21. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    Perfect thank you
     
  22. reset

    reset

    Joined:
    May 22, 2009
    Posts:
    393
    Hi

    My friend and I were walking along a corridor, chatting, and then he suddenly stopped. So I waited with him. I asked him if he was ok, and has said that he was fine but that he had come to a huge canyon with a river running below him and was a little worried about jumping across! I knew that the canyon was much further on so I raced up the corridor and arrived at the canyon. I couldn't see him but he could see me!! Why had he left his avatar skin behind?

    R
     
  23. CaptainExtra300

    CaptainExtra300

    Joined:
    Jul 14, 2012
    Posts:
    130
    Leepo:
    was wondering if this photon guide includes a matchmaking tutorial and example as your other unity networking guide?

    I see that there is a lobby tutorial here but is it with matchmaking?
     
  24. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    I'm thinking about using photon for my game because I heard its more powerful than Unities networking. Will I be able to have 64 player FPS matches?
     
  25. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    hey I bought the photon tutorial. Although the tutorial_2A scenes are not working as expected. The 2nd client cannot connect to a game and see the master client move the box around.
     
  26. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,043
    You do realize building an authoritative FPS with Photon is an immense job? since you need to implement all the physics yourself on the server.
     
  27. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    unities physics doesnt work with photon? Is there a prebuilt physics package out there that works with it? What about ulink?
     
    Last edited: Jul 15, 2012
  28. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,043
    you can not use the Unity physics on the server when running photon, as photon runs outside of Unity. You can obviously still use the physics on your clients, but the server will be "dumb" in that way that it can't check if you hit someone when you shot, etc.

    No there is no physics package for Photon that I know of.
     
  29. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    Ok so what if I use rigid bodies for tanks and vehicles. Will that work over a PUN network? Will collision even work?
     
  30. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,305
    PUN works very much like Unity's networking does: All the work is done by clients. Forget the server in this case. It's there to allow hassle free connections between players but it doesn't interact with them.
    So, yes, collisions will work but one of the clients is going to be authoritative. There is a "MasterClient" for that (the player who is in the game the longest time).
     
  31. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,167
    But will rigid bodies work for projectiles and vehicles over the network?
     
    Last edited: Jul 16, 2012
  32. AnXgotta

    AnXgotta

    Joined:
    Nov 25, 2011
    Posts:
    8
    I know there are other threads for Network interpolation/extrapolation but this is about the NetworkRigidbody.cs scripts in this package.

    I used the NetworkingRigidbody script with unity networking and it smoothed my players out perfectly. Now when I use your NetworkRigidbody script from this package for use with my Photon Cloud it does not work. It gives me the same jittery crap as if nothing was happening. I noticed that the script is always extrapolating which is to be expected. Is there something I'm missing here?


    Any help would be greatly appreciated.
     
    Last edited: Aug 4, 2012
  33. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,305
    We didn't specifically work on support of the NetworkRigidbody script.
    Where is it from? Maybe it's not too difficult to make it run.
     
  34. ND

    ND

    Joined:
    Aug 3, 2012
    Posts:
    60
    Hey every body,

    At last, after working hard on my own master server solution, I realized it's good if check this asset out for my game project.
    On the Photon cloud's web site I read there are cloud server's in the US, Europe as well as Asia (Default is Europe which is acceptable for me).
    Hum I was so glad until I ran the project (worker sample of this tutorial) and I faced with Ping of 300 !!!! I live in Iran (Middle East). Terrible !!!! What does it mean by this ping rate for me?
    I played lots of online games (Most of them on the servers located in the USA) average ping I had was about 140 and at worst situations around 200.
    Ping of 200 was awful because I always had a big delay. I was killed when I realized someone is there just in front on me. Now with ping of 300 on Photon cloud I imagine it will be a dead solution for my home country project. Most of my players are from Iran so after facing with this delay they wont come back ever.

    Since in this thread there are some experts from Photon I'd like to ask them if there is a way to get this ping lower? Or I have to have my own server located in Asia?

    Thank you so much.
     
    Last edited: Aug 9, 2012
  35. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    Hi ND,

    While the cloud is located in 3 locations, none of them are very close by. Most likely you are already connected to the EU (Netherlands). If you want ExitGames can help you try the other 2 locations but I doubt you get a -much- better ping there.

    Your best option is to run a Photon server yourself in Iran, considering you have the most players there. It is quite easy to install such a server yourself. You'll have the same features as the cloud hosting but also the ability to expand the server side logic if required.
     
  36. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    Updated on the asset store
    v1.15: Updated the PUN guide to PUN 1.16.2
     
  37. ND

    ND

    Joined:
    Aug 3, 2012
    Posts:
    60
    Hey,
    What does it mean by Pricing : Per App/ Server ? If I want to run 2 different games on one VPS, I have to buy 2 licenses?
     
  38. roryo

    roryo

    Joined:
    May 21, 2009
    Posts:
    1,395
    Can we connect to Photon Cloud from HTML5? It seems to only be for Photon Server (drat!)
     
  39. Oksana-Iashchuk

    Oksana-Iashchuk

    Joined:
    Sep 10, 2012
    Posts:
    111
    Hello,
    I'm wondering is your solution send one message per View or per Client (I mean combine all views messages in one stream)?
     
    Last edited: Jan 15, 2013
  40. bertelmonster2k

    bertelmonster2k

    Joined:
    Apr 3, 2009
    Posts:
    78
    We just launched the javascript SDK for the cloud.
     
  41. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,305
    It's in between those. RPCs are sent as single message, while synchronization messages (multiple times per second) are accumulated per group.
     
  42. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    I just verified with ExitGames, and the answer is yes.
     
  43. MMO-Developer

    MMO-Developer

    Joined:
    Jul 2, 2012
    Posts:
    25
    Hi Mike i have this errors in latest guide :( i try different photon id but same errors.

    createGame failed, client stays on masterserver: OperationResponse 227: ReturnCode: 32766 (A game with the specified id already exist.). Parameters: {}.

    When i try automatching i have this error !!!


    Illegal view ID:0 method: SpawnOnNetwork GO:code
     
  44. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    "createGame failed, client stays on masterserver: OperationResponse 227: ReturnCode: 32766 (A game with the specified id already exist.). Parameters: {}."

    You cannot create multiple rooms with the same name. If you dont care about room name you can always pass null in PhotonNetwork.CreateRoom and PUN will generate a unqiue ID for the room.

    "When i try automatching i have this error !!!
    Illegal view ID:0 method: SpawnOnNetwork GO:code"
    Is this in the original guide? I'll have to check it.
     
  45. MMO-Developer

    MMO-Developer

    Joined:
    Jul 2, 2012
    Posts:
    25
    Yes i download 10 min ago from asset store. Just click in unity 3d ver 4 on automatch.Also RPC example have problem.

    full error log

    Illegal view ID:0 method: ApplyGlobalChatText GO:code
    UnityEngine.Debug:LogError(Object)
    NetworkingPeer:RPC(PhotonView, String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2311)
    PhotonNetwork:RPC(PhotonView, String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:1645)
    PhotonView:RPC(String, PhotonTargets, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonView.cs:195)
    Chat:AddGameChatMessage(String, Boolean) (at Assets/PUNGuide_M2H/Example1/C#/Chat.cs:217)
    Chat:OnJoinedRoom() (at Assets/PUNGuide_M2H/Example1/C#/Chat.cs:52)
    UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
    NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1409)
    NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1288)
    ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
    ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
    ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
    PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:65)
     
    Last edited: Feb 19, 2013
  46. MMO-Developer

    MMO-Developer

    Joined:
    Jul 2, 2012
    Posts:
    25
    I check previous guide plugin 1.17 works good.

    in chat example under guide plugin 1.17 on chat example we have this error

    NullReferenceException: Object reference not set to an instance of an object
    Chat.AddGameChatMessage (System.String str, Boolean systemMessage) (at Assets/PUNGuide_M2H/Example1/C#/Chat.cs:217)
    Chat.OnJoinedRoom () (at Assets/PUNGuide_M2H/Example1/C#/Chat.cs:52)
    UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
     
    Last edited: Feb 19, 2013
  47. Imillionaire

    Imillionaire

    Joined:
    Dec 14, 2012
    Posts:
    44
    Are there any tutorials or guides on how to get an FPS, or MMO game running off a server AND setting up server side logic?
     
  48. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    995
    Imillionaire: Server side docs are only here AFAIK: http://doc.exitgames.com/photon-server/

    The guide has been updated to 1.20 and using the new and easier PhotonNetwork.LoadLevel (instead of disabling messagequeue and then Application.LoadLevel)
     
  49. darmie

    darmie

    Joined:
    Sep 13, 2012
    Posts:
    3
    Hello M2H, I have a Digital Ocean account with 20GB, 512MB RAM.... Can Photon Server run on it?
     
  50. BeetlePlay

    BeetlePlay

    Joined:
    Jan 31, 2013
    Posts:
    18
    Hi,
    I opened Photon Unity Networking Free and in Readme I see this:

    Requirements
    Unity iOS PRO and Unity Android PRO for exports to these platforms


    So, I can't use Photon on iOS and Android with free license ?
     
unityunity