Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon Unity Networking

Discussion in 'Multiplayer' started by tobiass, Aug 23, 2011.

  1. killer_bigpoint

    killer_bigpoint

    Joined:
    Jul 11, 2015
    Posts:
    20
    I have a problem, i want the disconnecting player to send a last rpc before quitting because the server lobby needs to know he has disconnected and is no longer in game, but i can't what shall i use?

    EDIT: when calling OnApplicationQuit i send an rpc but it doesn't seem to work
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    You could send the RPC with the target option "AllViaServer". Wait for your own RPC to be executed and disconnect then.
    This is basically the only way to make sure the RPC actually was executed. It might take just a roundtrip but if your connection is current bad, the client might do several resends to compensate loss.

    When your logic leaves OnApplicationQuit, the client is basically closed and can't do re-sends, should they be necessary, no matter what you do. Then you won't be able to wait for the roundtrip and RPC execution. Even a Disconnect() won't always reach the server.

    An RPC is not sent immediately when you call it. It's queued and sent when the client (internally) calls SendOutgoingCommands() again (in PhotonHandler). You can do this in your code, too, but don't do it for every RPC (which will ruin your performance and connection). That way, your RPC will at least leave the client and has a chance to reach the others.
     
  3. killer_bigpoint

    killer_bigpoint

    Joined:
    Jul 11, 2015
    Posts:
    20
    still doesn't work, i will try and explain a little better: the problem is when someone i pressing the exit button in the window, it will call onapplicationquit and i try to send it, but doesn't work, there is my problem
     
  4. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    Hey Tobiass, how do I get the name of all the players currently in room. I have UI.Text fields and I use .text to insert the String I want them to display. However I can't get the playerName String from the other players using playerList. I can get my playerName String just fine with

    1. inRoomPlayer1.text =PhotonNetwork.playerName;
    Now I want to display the names of another player in the room. I've tried a bunch of lines, such as
    1. inRoomPlayer2.text =PhotonNetwork.playerList[1].name;
    I've added things to the above code, I've created new variables to store the PhotonPlayer, no matter what I've tried so far though I haven't been able to get the playerName.
     
  5. LovattoStudio

    LovattoStudio

    Joined:
    Feb 11, 2015
    Posts:
    15
    You can do like this:
    Code (CSharp):
    1.  foreach (PhotonPlayer player in PhotonNetwork.playerList)
    2.                 {
    3.                       Debug.Log(player.playerName);
    4.                 }
     
  6. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    Thank you LovattoStudio. I'm going to add an i++ to that and then see if i can get that player name string into the correct text field by using something like playerList[ i ].
     
  7. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    Hello, I'm having some trouble, where it seems other players are creating games (rooms), but then upon exiting the game, the game (room) is never closed properly. So if another player tries to join that room which should be closed, nothing in the game works. In my case I have Zombie AI that is does not function, players are unable to pickup drop items, nor does it sync player movement. But this only happens in again certain rooms that were created by another player who leaves the room, but the room remains live in the Lobby even if the last player (Master Client) exited the game.

    I can only assume that rooms are being created then they are stuck in some kind of offline mode in my lobby where all parts of the game become inactive, because the game should have really ended as there is no one in the room when I check.

    Although oddly enough I can create a room myself, join another client into that room and it's fine. I'm not sure how to properly troubleshoot the issue because If I create a room I don't get any errors, and any of my test clients connected to the room I create works fine.

    I can see this issue when I find another room started by another player, usually I find the issue in US or Europe Region. I noticed there was a power-outage in US region was down on Monday I think. So I don't know if this is related or coincidentally. Please advice.

    Note: I did send an email to developer@photonengine.com but still I have not recieved a reply after 3 hours...
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @killer_bigpoint: As said: You can't do much in OnApplicationQuit() because the next frame is not going to happen at all. You can Disconnect() and hope it's arriving at the server. You could potentially also do an RPC and also call networkingPeer.SendOutgoingCommands() in the same method. However, this does not guarantee delivery.

    @Kuroato: There is a slight delay until a room gets removed from the lobby. We need to update servers, etc. Also, maybe some clients don't disconnect actively but instead just stop responding, which causes a timeout after several seconds. It is possible to join a room that just removed the last players.
    You can call Disconnect() when the app closes and if you can make up a rule when to drop a room, then you could also set Room.open = false, to close the room for matchmaking.

    We are all engineers working on Photon. There is no helpdesk team, so we have to answer mails when we can.
    In cases like the outage on Monday, we do our best to fix things first, then reply. We try to tweet a status and updates but that's basically all we can do while under stress.
     
  9. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @Kuroato: One solution to avoid the issue from repeating could be that you set Room.open to false, when you join a room that is on no good condition and has no master anymore. Or in other words: When you join a room but are the first and only player at the same time. You can leave the room then, too.

    Alternatively, it makes sense to reuse the room and clean up the state when you join an otherwise empty room.

    Can you please let me know which version of PUN you use now?
    Because v1.54 and newer get the Master Client assigned via the server. At least the clients which join an empty room should also be the Master Client.
     
    Last edited: Jul 23, 2015
  10. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    I replied to Email, I guess we can post the solution here at the end just in case others run into it also.
     
  11. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    I have a quick question. How can I know if the player running the script is the room creator/host? I would like to create a room but keep it hidden and then when my friends join I would like to open it to everyone else, but I don't know how to give first player that kind of control over the room.
     
  12. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @superluigi: Check PhotonNetwork.isMasterClient.
    You can create the room as invisible, so no one will accidentally join. Set the RoomOptions .isVisible to false for CreateRoom. You can make it visible when your friends are in.
     
  13. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    @tobiass Yes, but how? I've tried , if I press some button "PhotonNetwork.room.RoomOptions.isVisible = true;" but it returns an error. I've also tried PhotonNetwork.Room.Roomoptions.isVisible = true; , room.isVisible = true; , etc... I've tried just about everything, but can't seem to get it to work. Thanks in advance for any reply
     
  14. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
  15. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    @tobiass Yes I create the room as invisible, but after it has been created invisible and my friends join the invisible room because they know the room name, how do I then change the room from invisible to visible so that other people can join the room randomly? I don't know how to write that in code. I keep getting errors.
     
  16. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Use
    PhotonNetwork.room.visible = true;

    Does that work for you?
     
  17. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    Yes that's it! Thank you. My mistake was that I was using isVisible = true instead of just visible = true;. I got that from using Room Options to create the rooms and room Options uses "isVisible"
     
  18. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Ah, sorry. This is confusing, I agree.
    I will check if I could adjust things.
     
  19. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    I updated PUN (Free and Plus) in the Asset Store. Get the new packages.
    This update fixes a few export issues on mobile (Android and iOS). Let us know if your exports should still fail with this version!
    I updated the change log in the initial post of this thread.
     
  20. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Important Note
    In v1.60, the setting autoJoinLobby became editable in the PhotonServerSettings file for the project. As side effect, the autoJoinLobby setting now defaults to false. If you want to show room lists to your users, you need to turn it on.
    The "Demo Worker" lists rooms only, if you set autoJoinLobby to true.
     
  21. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    Can someone explain the difference between Photon PUN and Photon Free on the store? They look the same since both include the trial Photon Cloud plan ...
     
  22. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Pretty much the same but the + allows up to 100ccu while the free "just" 25ccu.

    I recently upgraded and was as easy as deleting pun and importin pun+. Further, my original version was a bit old and the new one updated the way the RPC are called automatically in all scripts. Good job!
     
    tobiass likes this.
  23. Trusty

    Trusty

    Joined:
    Sep 7, 2013
    Posts:
    16
    Hello there ! I'm probably coming with a weird, long and bored-to-read question - sorry in advance - but I'm a little bit confused and don't really know what I should use.

    Last year with some friends we used the free version of PUN to make a multiplayer tower defense ( lobby, rooms with a name / join them and synchronize gameobjects was what we needed, 20 players was enough for us ).

    Now I'm starting a new personal project [ RTS ] ( will certainly never make money of it and probably took some years to make because I just work on it during the week end - BUT - I'm willing to invest in some good assets ( hey, that's where you enter in my story :D ).

    Correct me if I say something wrong :

    I was thinking of taking PUN+, which I'm assuming meets more than my needs ( more than 20 players, lobby, rooms, matchmaking, and of course the synchronization during the game, it's a rts ) BUT - and this is the question - I was already thinking BOLT was a concurrent of Photon - and my final choice was photon because I already use it a bit.
    I think BOLT could do the job too, at least for the synchronization ( didn't find / search for long at lobby, rooms, matchmaking feature in bolt ).

    Now that you are in the same family, I want to think again my choice. PUN+ and BOLT have almost the same price and I really don't know which plugin will be the best for me and will do the job I don't want to loose too much time on ( networking stuff ).

    I'll probably need to store a lot of data player in the future, I was thinking of using PHP and mysql solution but - photon can do the job too, right ?


    In short ... What should I buy ?

    Thanks a lot in advance.
    If I could do something for you, like cookies or pancakes, don't hesitate :D
     
  24. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Cookies? Mmmmm. Probably my choice.

    I don't know a lot about the project, aside from the genre, so it's hard to give advice. It would be more a guess.
    I would probably spend a little time with both solutions and attempt to implement 1..2 of the networking pieces in each. Without doing the game or proper UI or anything. Just with debug logging, etc. Anything really easy.
    That will tell you how you get along with either solution and you can pick one yourself.

    Photon Server can be used to store values for Users. Our partner PlayFab has accounts, inventory and much more (even simple server-side scripting!) out of the box for PUN, so this might be a nice combination of services.

    Bolt doesn't have a CCU limit but you will need to run a server for matchmaking (unless you want it to be a LAN game). We will hopefully provide matchmaking and other services for Bolt but right now, it's not there yet.
    On the other hand, Bolt runs in Unity and your server is a Unity instance. It loaded the same level, knows all units, the terrain, etc.. It's better for authoritative Unity servers.

    Not sure if that helps.
     
  25. Trusty

    Trusty

    Joined:
    Sep 7, 2013
    Posts:
    16
    *Preheat the oven*

    Yeah it helps ! I want to manage both LAN and online.

    Didn't put the authoritative / non authoritative question in my list yet, I definitively prefer an authoritative one, but Playfab looks awesome. I think the great final will be PlayFab & Already provided services VS Better for Authoritative & no CCU limit.

    Anyway I'll start by doing a little prototype with both, didn't even think about it but seems obvious now :)

    Thanks for the rapidity :) Awesome support :D
     
  26. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    66
    Hi, i am having a trouble with the synchronization of player positions. I use Photon Rigidbody 2D View and Photon Transform View together. I move the player object with the mouse.(Drag and Drop Style). But the movement is really jerky and inaccurate. Player objects even shake when i just grab the object without moving it.
     
  27. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    61
    Hi, i have a problem with Network instanciate ... in my game i want to spawn 2 characters per client connected, i have a loading screen which waits untill every client is ready and then i do this:

    Code (CSharp):
    1. PhotonNetwork.Instantiate("Prefabs/" + cn , new Vector3((float)Random.Range(0, 8), 0f, (float)Random.Range(0, 8)), Quaternion.identity, 0);
    most of the times this works but sometimes the MasterClient displays 6 characters and the client only 2 (instead of 4 each) why is that? If the problem occurs it occurs every time again when i start the same build again. But if i wait some time and try it again it works .. so this isn't a coding bug i think .. maybe it occurs with low bandwidth !?

    Any ideas how to fix this ?

    Thanks for your help!
     
  28. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Try smoothing position with a lerp. I think Photon already brings the scripts to do so without typing a line. Otherwise
    in On photon serializer view , the incoming stuff needs to be lerped.

    Checke, photon transform view, it has the lerp option, try it and see if it solves
     
  29. frodoe7

    frodoe7

    Joined:
    Jul 25, 2014
    Posts:
    37
    not working with me
    missing classes and namespaces :(
     
  30. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    66
    Thank you for your answer, i tried it too but it only fixes the jerky movement a bit when i move the ball fast. I tried UNET using Network Transform Component and it worked really well that i didnt have to do anything. Just synched Rigidbody2d in the inspector. But i cant use it because i need the matchmaking system. Photon is great but the position syncing tools are not that good in my opinion. I was working on another project before this and bought Pun Plus to export mobile. I couldnt finished the project because of the inaccurate positions of the players. I tried many things to fix it but in the end i just stopped working on it.
     
  31. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    II'm not an expert, but from my experience player pos in PUN do work, at least do work great in my game. Wait for Tobiass to give a mor in depth answer to your issue.
     
  32. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    If you run 2 clients on the same machine to test, you don't get the same results as you would get in real-life. UNet does the hosting on your machine and there is 0 lag in the testing case. Your players won't use the same setup and your game will begin to stutter at release (unless I am missing something).
    How you would solve the movement issues, depends on your game style. You can probably do some more updates, delay the execution a bit, etc.
    I am sorry if we couldn't present you with a solution for your case. We wish we could but the best solution depends a lot on your game's steering and movement. Each game is different.
     
  33. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Without more info, this can't be answered.

    Please update to the latest PUN and check if there is anything suspicious logged by the clients (console/log). Make sure the cn variable is fine (you can find the prefab in Resources) and check if you load a scene. If you do after someone instantiated anything, that instantiated game object gets destroyed (by Unity).
     
  34. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Please import into a new, empty project. It must be an import issue. Use PUN v1.60 from the Asset Store.
     
  35. afavar

    afavar

    Joined:
    Jul 17, 2013
    Posts:
    66
    I will try few things more, i have tested UNet on both local and online matchmaking service which is currently in Beta.
     
  36. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Not sure if you did that before: You could explain your game, type and movement-style in a post in our forum. When things are calm, I might take a look or give specific feedback. It's off topic for this thread...
     
  37. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I have a doubt. In previous versions to 1.58 I'm runing now, I could setup a server as Preferred or something like that. This way it would choose the closes to client. Now I'm forced to choose euro, us etc.. Any chance to set this manually to make it choose the most addecuate one?
     
  38. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
  39. xenonmiii

    xenonmiii

    Joined:
    Aug 2, 2010
    Posts:
    147
    @tobiass Is there a way to get PhotonNetwork.otherPlayers filled when in the Lobby? It is remaining 0 even though there are others connected in the lobby. (In PhotonNetwork.playerList they is only the local player)
     
  40. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
  41. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Sorry, no. Photon does not list players in the lobby. In a popular game, this would soon bee to much to send and you can't interact with them either. The lobby is there to list rooms, if you really must do that. In best case, use random matchmaking to get players into the game asap.
     
  42. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    How do I create an RPC in unityscript? I have a very long script that would require far too much work to convert to c#. I put [PunRPC] on the line above the function call but Unity tells me it's expecting a ; after [PunRPC].


    function Update()
    {
    all my code
    }

    [PunRPC]
    function ChangeTeamLeft()
    {
    teamInt -= 1;
    }

    I also posted the question on Unity answers and the PhotonEngine forums. I will copy the answer from one to the others http://answers.unity3d.com/questions/1023456/how-to-create-an-rpc-call-for-photon-using-unitysc.html

    http://forum.photonengine.com/discu...an-at-the-end-but-it-still-doesnt-work#latest
     
    Last edited: Aug 10, 2015
  43. frodoe7

    frodoe7

    Joined:
    Jul 25, 2014
    Posts:
    37
    I can`t find any cloud in your service , so I can`t have gamers accounts in my game
    unfortunately!
     
  44. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Suggestion. Add more info to the stats such as total unique players, min play time, average play time, server used and so on. The graphs are ok but could be much better.
     
  45. StarGamess

    StarGamess

    Joined:
    Jul 21, 2014
    Posts:
    179
    Hey i dont know if this is the right place to ask but im having trouble loading level dont know if im doing something wrong with the photon code or just something with my own code ;p. I put a syncscenes in the start void and then i use Photonnetwork.Loadlevel(); to load my level also i disable message queue then and in OnLevelLoaded i enable it again. Is this the right way to do it?
     
  46. superluigi

    superluigi

    Joined:
    Feb 6, 2013
    Posts:
    38
    So to add to my first question, I made a c# script and tried using [PunRPC] and it gave me the error: The type or namespace name `PunRPCAttribute' could not be found. Are you missing a using directive or an assembly reference? I then tried [RPC] and I got no errors. In the docs it says that PunRPC replaced RPC, so I'm confused.
     
  47. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    The doc should say:
    "Unity 5.1 made the RPC attribute obsolete and it will be removed later on. Due to that, PUN uses another attribute: PunRPC, beginning in v1.56. ..."
    It sounds like you didn't update to the latest PUN yet?

    In UnityScript, I think you just have to use @PunRPC instead of the brackets. I actually rarely use UnityScript but it should work.
     
  48. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    When you use PhotonNetwork.LoadLevel(), you don't need to handle the message queue yourself. It will do that for you.
    However, you need to set PhotonNetwork.automaticallySyncScene to true. Then PUN can handle the scene changes and whenever your Master Client changes the scene, the others will follow.
    It's described here.
     
  49. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Thanks for the input! This is welcome through any channel :)
    We would also really like to have those values but it's not been the focus of our service. We would need some better tools to display and analyze those values, too.
    We take note of the interest but I can't promise anything soon.

    It might be to combine Photon with Unity Analytics. What do you think?
     
  50. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062