New to Photon: tips for asynchronous gameplay

gnoblin
gnoblin
edited July 2011 in Photon Server
Hello!

I've finally decided to try using Photon (client is made with Unity3d).

The gameplay I'd like to test is something like this:
1. each player lives in a copy of a game world, players don't interact directly.
2. there's a chat, where text communication between players takes place.
3. player moves in the world and acts in realtime (by this, I mean that the game is not turn based).
4. all events in the world are controlled by server, all player actions are checked on server to prevent cheating (this is important for highscores).

Are there any special tips I should take into account while learning how to use Photon for the purpose of creating such gameplay?

What features of Photon should I use, and what features are not important for me?

I suppose, the fact that players don't live in the same world must have a pretty solid impact on how the server code is structured :D. What 'architecture' in terms of Photon would you propose?

big thanks,
Slav
«1

Comments

  • Ok, my mistake:
    this is probably not an asynchronous gameplay (like in farmville),
    but a gameplay where players are completely isolated from one another in terms of interaction between each other (but can communicate in a chat), and also a separate world needs to be simulated for each player.
  • I've started reading the docs :mrgreen: .

    I've grasped the concept of Events, Operations, Properties, Channels and Rooms.

    I suppose, I need 1 room for each player.
    The room states will be stored in Properties, and when player leaves the room - the property will be saved to MySQL database, and the room (not sure here) will be destroyed to free up resources.

    I suppose, I will need 3 channels:
    channel0 will be for sending something like player movement (the data which can be lost, sent as unreliable),
    channel1 will be for important data (sent as reliable),
    channel2 will be for chat messages (important, but can be sent with some delay).

    ---
    One thing which I haven't yet understood is:
    - in which part of LitePeer code is "the main Update loop" (so I can send events to users periodically)...
    Also the part that isn't clear to me: what to do if my every room needs some world logic to happen, for example NPCs need to move around or weather needs to change (do I need a separate update loop for each Room?).

    Any tips are appreciated! :?

    thanks,
    Slav
  • Hey, good to know that finally the documentation helps a bit :)

    About the open questions:
    Main Update Loop: Means the game loop or in Unity the Update method of some object, that has control over the LitePeer instance. The point is: there is no looping or threading in the client library. You keep it in the loop yourself and thus get full control over it.

    Rooms are not especially well suited for open worlds, where everyone could interact with an NPC in it. Rooms are good if there is no direct connection between different groups of players. Like instances of the world where only your party exists aside from NPCs.

    In your world style game, you would be better off with the MMO Application. It does give you a bit more in the direction of MMOs. Most of all, it does interest management for you. It divides the world in areas and the server will subscribe to events of an area if you're close enough to notice what's happening in it.

    Don't worry: The knowledge about Operations and Events still helps.

    By the way: If your important actions (channel 1) have to take place in a certain position, you have to put those into channel 0, too. That way, they are in sequence with the position updates in channel 0.
  • Thanks for the info!
    Main Update Loop: Means the game loop or in Unity the Update method of some object, that has control over the LitePeer instance. The point is: there is no looping or threading in the client library. You keep it in the loop yourself and thus get full control over it.
    Maybe I misunderstood something, my question was about a loop in server logic.
    Rooms are not especially well suited for open worlds, where everyone could interact with an NPC in it. Rooms are good if there is no direct connection between different groups of players. Like instances of the world where only your party exists aside from NPCs.
    I have exactly the situation where there's no direct connection between players (only through chat).
    Every players runs around in his own copy of a world.
    In your world style game, you would be better off with the MMO Application. It does give you a bit more in the direction of MMOs. Most of all, it does interest management for you. It divides the world in areas and the server will subscribe to events of an area if you're close enough to notice what's happening in it.
    Area events look interesting, thanks.
    A few moments about MMO Application:
    1. how should I use it if I have N worlds for N players instead of 1 world for N players (as in normal MMO)?
    2. where can I find client-side part of the examples for Unity3d for MMO Application? I think I've seen such folder only in serverside SDK.

    thanks,
    Slav
  • The server side does not have a game loop. Everything is triggered by the actions (Operation requests) of the clients, their connections, etc.
    If you want something like a Update method, you should use a scheduled message to trigger it and re-schedule the message. LiteLobby does this, so the blueprint for it would be LiteLobbyRoom.cs. Check out the method SchedulePublishChanges.

    I am not sure what you try to achieve exactly. Your users only chat and won't see each other? They don't interact and they have their own copy of the world, which would mean they don't operate on the same instance of an NPC??
    Then I'd say: implement a chat room or several and all game specific Operations you need, could be implemented by the Peer class. You can do work there, too and add you per-person-instance-data, too. If they interact with the same NPC (one hits it, the other sees the reduced health), then you could use rooms or the MMO style world.

    The client side demos of the MMO Demo are in the Server SDK as well, right. Here:
    sdk\src-server\Mmo\Photon.MmoDemo.Client.UnityGrid
    sdk\src-server\Mmo\Photon.MmoDemo.Client.UnityIsland

    But you should also run the DotNet Demo:
    sdk\src-server\Mmo\Photon.MmoDemo.Client.WinGrid

    We have a tutorial video up about it:
    http://blog.exitgames.com/2010/07/video ... -mmo-demo/
  • thanks again :D.

    Ok, just to make it 100% clear:
    Every player lives in his own 'sandbox' with his own versions of NPCs (player2 can't affect NPCs in the 'sandbox' of player1), but can chat with other players.
    It's like a singleplayer game, _but_ in which all players can chat with each other and the events in each player's sandbox are controlled by server (if NPCs run away, attack player, go to sleep, etc) and all the player's actions are double checked on server (to prevent cheating and getting a high place in top100 highscores).

    I think a setup where client triggers 'update events' on server is OK, and I'll also have a look at SchedulePublishChanges (and then decide what suits me most).
  • Ah, ok.
    I think you could really implement your per-player game logic inside the LitePeer class in that case and add each player to a chat-room to send chat events across.
    You should think about limiting the chat room size and get rid of the join/leave messages. These are unimportant in a chat, Id say. If you want to have online status of friends, the join/leave events are not enough anyways. Check LiteLobby on how it extends Lite and how LiteLobbyRoom gets rid of the join/leave events, compared to a regular Room.
  • I have enabled logging in log4net config (<log4net debug="true">), built the Lite app for the server, put the contents of bin folder to the sdk\deploy\Lite\ folder, launcher Photon - but the problem is that I see no new log info in the log folder (neither in deploy\log\, nor in deploy\bin_x64\log\ folder).

    Any hint how to enable\see logs in a better way? :)
  • I also tried to edit logic in Lite application (client side) in order to add sending and receiving chat messages.
    (I used Move\SetPosition as an example)

    I didn't find what to edit on server side, so I tried running the app - but messages don't come through.
    Is there anything I need to add in serverside logic or is it an error in my clientside logic?

    big thanks! :?
  • One more thing I'd like to know: what's the best approach for the situation when I want to send AskForDataOp from client to server, so server queries the DB and sends the response back to client as an AskForDataEv (the thing I don't understand right now - what is the most correct way to implement this on server... I am working with Lite application at the moment).

    thanks again :)
  • chat does not require any server modification, you can send it through as reliable event if its meant to be sent to the whole room for example



    Generally I would recommend to not alter Lite
    Instead create a new application that extends Lite
  • About the Log4Net:
    The setting <log4net debug="true"> is for internal log4net debugging. Means: If the log4net library hits some issue, it will log this as well.

    If you want to make more of the logic's debug output, you will have to set the debug root level like this:
    &lt;root&gt;
        &lt;level value="DEBUG" /&gt;
    

    Each individual logger type can have it's own level as well, overriding the root's level if set.

    You don't have to re-build the application to just change this config. I think it even accepts changed while it's running :)
  • If you just want to send your info from one client to others in a room, you can do so by raising events with the OpRaiseEvent() method. The demo function Move\SetPosition is the right example. Keep in mind you will have to change the Event Code or else your new content will be sent with the same event (Move).
    You don't have to change the server, if you don't want it to do something with the data (like keeping it in memory or so).

    See "RaiseEvent / Custom Events" in the DevNet:
    http://developer.exitgames.com/liteandl ... teconcepts
  • Dreamora is absolutely right: If you just send events across, you don't have to modify Lite. To make DB requests possible, setup a new project and make it extend Lite. The idea is to keep the source "as is", so it can easily be replaced with updates we provide.
  • Ok, I successfully managed to send a text message from one client to another (with a separate EventCode) :D.

    1) I'd like the player to receive his own message from server - how should I approach the problem? (right now I don't receive it)

    2) I send a text message like this:
    evInfo.Add((Object)STATUS_CHAT_MSG, this.text);
    peer.OpRaiseEvent(EV_SCHAT, evInfo, isSendReliable);
    

    and then receive it like this:
    string str = (string)evData&#91;STATUS_CHAT_MSG&#93;;
    

    I tried sending text in Russian - but to my happiness it is sending OK :).

    The question is: is there anything I can optimize here? (to reduce the amount of transmitted data or smth like that)

    thanks,
    Slav
  • the only thing to optimize would be not allow freeform texts but predefine phrases / symbols like PhantasyStar Online of Final Fantasy XI do it, in that case you could sent ints ;)

    but otherwise, not really as compressing them won't get any win
  • I've heard something about possibility of using PhysX with Photon - is it true that I can simulate some collision on server? :)
  • OK, what I actually wanted to say:
    Can I use physic simulation in Unity and somehow check the results in Photon? I suppose this is possible to some extent, it's interesting to know how good this can work.

    So, for example, I could have players running around and shooting with server-checks to prevent cheating.

    I understand that networked physics is not exactly a simple stuff, but that's what popped in my mind :?
  • one more question:
    As far as I know, non-HTTP traffic can be blocked on some users' machines - is it a problem for Photon?

    thanks again!
  • Noob attack on client-side prediction :D

    Do I understand correctly that I should do several things in order to properly synchronize realtime movement of objects in my room:

    1) use the knowledge of each object's position and movement direction (+speed & acceleration) - to predict the movement of an object between server updates, and upon receiving a fresh update data - smooth between the current predicted state to the state received from server

    2) calculate the latency (ping\2) and use this to know "how far in past" is the last update received from server (where the good_latency value would be calculated like this: check latency 10 times, get a median value, remove all samples that are "> 1.5 median" and then take the average of remaining samples)

    ?

    The thing I'd like to know if my flow of thoughts is ~correct and whether there are any useful things provided by Photon \ Photon examples that can help me with this task.
  • normally not cause they will request the connection to your server where you setup all portforwardings etc.

    the blocking is commonly only / primarily a problem when users host servers themself and don't setup forwardings etc
  • About PhysX / Unity:
    You could use PhysX with Photon but it's not identical to Unity out of the box. For instance, you need the world data and body setup on the server.
    We know some of our customers run Unity on the server side to do simulation for a number of players (e.g.: a room). But so far we did not have the time to check out how well this works. As you would run one Unity headless process per room, it implicitly scales across cpu cores. But we don't know any details. Sorry.

    Non-Http-Traffic:
    Yes, this could be an issue. Depending on the firewall setup, it could block either UDP or TCP or even HTTP for certain pages. Basically, a network guy (in a company) could probably lock any specific traffic if he wants to.
    In most cases UDP traffic gets through, cause it's the client which initiates the conversation with some server. Answers are allowed most of the time.

    Synchronization:
    We don't have good examples for synchronization currently. It's a complex topic in itself and depends a lot on the style of control you have in your game. Arcade-like steering without slowdown synchronization is different from what you do if movements have acceleration and deceleration.

    In general, your ideas are good and the way to go. Depending on your game, the synchronized timestamp might be helpful for you. We tried to minimize its offset, so in general it will be +/- 10ms between peers on the same server (but this is not guaranteed).
  • Do I need several Photon licenses if I'd like to run two parts of my game world on two separates machines?
    I didn't get this part clearly :)
  • I'd like to try using Photon with Amazon EC2 micro instance (I've seen Boris mentioning that it runs OK with a small amount of clients).
    The problem is that I have no experience with Amazon - do they provide Win 2008 image or should I somehow prepare\install it myself?

    Will I be able to test Photon on EC2 free year "trial" for new amazon subscribers? (maybe they don't allow running windows if you're in a free mode or smth like that) :roll:

    thanks,
    Slav
  • gnoblin wrote:
    Do I need several Photon licenses if I'd like to run two parts of my game world on two separates machines?
    I didn't get this part clearly :)
    yes, one license per server. 1 license for up to 100 concurrent users is free (per game).
  • gnoblin wrote:
    I'd like to try using Photon with Amazon EC2 micro instance (I've seen Boris mentioning that it runs OK with a small amount of clients).
    The problem is that I have no experience with Amazon - do they provide Win 2008 image or should I somehow prepare\install it myself?

    Will I be able to test Photon on EC2 free year "trial" for new amazon subscribers? (maybe they don't allow running windows if you're in a free mode or smth like that) :roll:

    thanks,
    Slav
    I don't recall seeing any free ec2 trials. AirVM is a descent alternative - cheaper and better performance.
    They both provide win 2008 images, the amazon image is very outdated and you need forever to install the updates. Don't try installing updates on the micro image, tried that once but it takes forever.
    We do also offer a pre-configured image that you can load in the us-west region, search for photon or for ami-d4095991 in the community AMIs. It's a bit outdated (photon 2.2) but you can just update it to the newest version. And don't forget to run windows update.
  • To be clear about this:
    Only 1 license with 100 concurrent users is free per game. If you exceed 100ccu in your game, you need another license. We don't mind splitting a 500ccu license into 5x100 so you can run several machines at the same time for the same game.
  • I don't recall seeing any free ec2 trials
    http://aws.amazon.com/free/
  • Will Photon work with Unity iPhone Basic?

    Can I run several Photon instances with less than 100 ccu for several different games on one machine? :)

    thanks,
    Slav
  • System.Net (sockets) is iOS Pro only