Search Unity

Clans

Discussion in 'Multiplayer' started by FlamingGenius, Mar 5, 2017.

  1. FlamingGenius

    FlamingGenius

    Joined:
    Dec 30, 2015
    Posts:
    60
    So I'm quite familiar with unity and C# and combining my knowledge with php I was able to make my game "feel" online the game loads,saves,registers everything from my server but the issue with the way I did things is my server grabs information from my database through php and then prints it out as a string and then my game parses the string into a data array which I then convert into the correct valued

    Now everything works perfect it's just a lot of work

    What I would like to know is recently I added PUN to my game and I used photon chat and set it up so I could have a global chat as well as a clan chat

    The issue I'm having with my current setup is its just so much brutal coding to parse strings of data from my server is there like a framework or something I can use to lighten the load?

    Maybe theres like a clan system framework I'm missing?
     
  2. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    I'm not sure that I exactly understand your issue, however if the issue is in the complexity of parsing your strings coming back from your web server, it would be good to see what format these strings are (to understand your parsing mechanics).

    The best, and most efficient, string storage mechanism is JSON. Since you use php, i'll assume that you know what JSON is, however, if you don't: https://www.w3schools.com/js/js_json_intro.asp

    Some reasons why it's the best option for sending strings of data:
    • it is widely understood by many development environments
    • otherwise, there are many robust libraries that allow you to convert runtime objects to json strings, and visa versa (i use LitJson for c#, which I absolutely recommend)
    • when concerned about bandwidth, json is designed around adding as little overhead as possible. When you compare it to XML for example, XML ends up adding a fairly large number of characters to each piece of information within the data with the way that the syntax works. Json is much leaner.
    Apologies if I have misunderstood your issue, however if the issue is in the complexity of parsing your strings of data coming from your web-server, switching to json strings will really help.

    Hopefully this helps. Let me know if you're still stuck.
     
  3. FlamingGenius

    FlamingGenius

    Joined:
    Dec 30, 2015
    Posts:
    60
    I love the detail you went into but my strings are actually simpler

    So all user data (money,level,xp) when retrieved are printed out in php like this

    `echo "$money|$level|$xp";

    And when I grab that data in the game I split it up

    `string[] data = download.text.Split(('|'));`

    Using the "|" I can differentiate the data and then set my data like this

    `GameController.Instance.money = IntConversion (data[0]);`

    Due to the server returning a string I had to make functions to convert every type of data for example the above IntConversion looks like this

    `Private void IntConversion(string data){
    int h;
    int.TryParse (data, out h);
    return h;
    }`

    As you can see over time all this becomes quite a bit of work but I can manage that because this is just user data is loaded once from the server and the player can play the game and upon leaving the game all the data is converted back into a string and sent to the server

    The issue came along when I added clans because my current method will update for one user but not for all

    Let's say that one person changes the clan description not everyone would see it because it hadn't been sent to the server yet

    Just need a better method so that everyone is constantly up to date without a bunch of brutal coding
     
  4. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    Hmm. Okay so you're not concerned about performance, but more about the awkwardness of having to extract/cast the data. That is understandable.

    Realistically, unless you wanted to implement some sort of "smart cast", where c# looks at the string and converts it to what it thinks it is, I would again recommend JSON. Json always deals with objects, so sending a single value (like just a single number/string) isn't possible because at minimum you must send either an object or an array, but in theory, you could do something like this using LitJson (the aforementioned Json parsing library for c#):

    First of all, set up a basic class that resembles the object structure that you are sending from the server. Using your example, we'll do a "PlayerInfo" class with properties for the money, level and xp:

    Code (CSharp):
    1. public class PlayerInfo {
    2.     public float money;
    3.     public int level;
    4.     public long xp;
    5. }
    Now, converting a json string to this object during runtime is as simple as:

    Code (CSharp):
    1. PlayerInfo info = LitJson.JsonMapper.ToObject<PlayerInfo>("{money: 100.32, level: 25, xp: 123456789}");
    And if you need to go back to json:

    Code (CSharp):
    1. string json = LitJson.JsonMapper.ToJson(info);
    In terms of performance, json is very well designed to be highly easy to parse, so you'll be very surprised at how well it performs even with huge json strings. You are right though, your method is clearly more lean and probably would actually perform a little better than using json, however noting the above, this would make your life a lot easier.


    On a side note, your initial post had me assuming your were using your web server just to handle persistent clan chat in a forum-like manner (versus your global chat via PUN for a more real-time non-persistent chat). This made sense so I didn't say anything. However, it looks like you're using the web server for a little more than that. Just to be sure: using the web server for data persistence is an absolutely excellent idea. I do the same, hosting player related data and setting up apis etc to manipulate the data rather than relying on third party tools (like google play etc) and it works great. However, be sure that you are not using the web server for real-time data communications. For example, if you are using the web server to regularly receive updates from the client and/or regularly send updates from the server to clients, you may run into blocking issues. A basic http web server is designed to handle one request at a time so in terms of scale, this will only take you so far.

    You also mention issues where when a player updates their clan name, it doesn't update the clan members. This is the reason things like UDP, and WebSockets were invented: for bi-directional communication. UDP and WebSockets allow you to create a persistent connection with the server, and send/receive over the connection in either direction on-demand. So it might be worth looking into that as a better way to communicate with your players.

    Hopefully this helps. Let me know if you're still stuck.
     
  5. FlamingGenius

    FlamingGenius

    Joined:
    Dec 30, 2015
    Posts:
    60
    Excellent reply I've used sockets before in my earlier days of programming but that was for html/java scrip game's not sure how I would use websockets for a unity game

    Once concept I never quite grasped is communication with my database using websockets

    What I'm more so looking for is a way I can send/receive data constantly withoUT a lot of coding

    Basically I would like to have 2 functions

    Public void RecieveData (){

    }

    Public void SendData (){

    }

    I would like to be able to make a general functions so I would only need the 2 functions for all my calls

    The one issue I came up with this is how will you know if there's new data to recieve? There would have to be a way for one client to tell another client that he changed somehing?
     
  6. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    Okay so there's potentially a lot to cover here, maybe more than is suitable within a forum post (we may need to move this to a PM if it starts losing it starts becoming less helpful as a forum post).

    Unet (unity's multiplayer networking framework) does support websockets within the HLAPI (high level api). However, it would be virtually impossible (at least enough to make it not worth doing) to have a setup where the client-side is a unity-built application using unet, and the server-side is a webserver running a websocket in php. So, you have potentially two options here. Either you stick to your current environment in which case you will have to implement a custom client and server side allowing you to (possibly) re-use alot of what you already have; or you would have to refactor your code so that both your client and server are unity-built applications using unet. Below i'll talk briefly about both routes:

    The webserver + websockets approach
    In php, Ratchet is your go-to framework for anything related to sockets (including websockets). So that would be the first thing to read up on. The basic idea with websockets is that the connection is initiated on the client-side, where it all starts off as a basic http connection (over TCP) that is "upgraded" to a persistent websocket connection. At this point, communication can be sent in either direction on-demand between client and server. The various examples of how to use ratchet cover the client-side and server-side for in a web environment. The server-side will cover the basics of what you need to know for your setup, the client side however will not.

    For your client-side, have a look at the websocket unity asset on the asset store. It implements the commonly known websocket-sharp library (a basic websocket library for c#) and makes it more unity-friendly. It's a little rough around the edges but ultimate it will allow you to handle your connection and send/receive data as you need.

    The unet approach
    So ultimately, you're not going to be able to use your web-server back end here (you could but the work-load to get it all working would be far too much to be worth it). So the idea is, you'll have a unity-built application for the client-side, and another one for the server-side. The client application will be what you release to your players and the server application you will run on a dedicated server.

    As mentioned earlier, unet allows the use of websockets within the HLAPI, and unfortunately not the LLAPI. The LLAPI would have been more ideal for you as you only really need the basics, however you need websockets. With that being said, all you need to use to get websockets up and running in unet is the NetworkServerSimple class to handle your sever-side, and the NetworkClient class to handle the client-side. Have a read up on those to get a basic idea of how they work.


    There's potentially alot for you to get started on here, so i'll leave it there for now and feel free to come back with any questions you might have. You may at this point be thinking "yeah... that seems a bit too much" and be considering just improving your current setup by for example "polling" the server for updates each second or something like that. Just remember the points raised in previous responses here: your current setup will only scale so far.

    Good luck!
     
  7. FlamingGenius

    FlamingGenius

    Joined:
    Dec 30, 2015
    Posts:
    60
    Definitely something I might need to look into

    Since my game server happens to be a home server I set up I put some jobs on my desktop to run "server" code for the game and got some really good results

    I didnt get my simple 2 function dream but I got a really decent result

    Now what one player does is updated across all players within a 3sec timeframe which I think is acceptable lag for the current build of the game

    I also got my clan system fully functional I've even found photon chat to be quite a handy tool

    And I really appreciate your remarks on JSON I found a way to format my text in php to JSON and return them to be parsed by the client and it happens to run MUCH faster so I highly appreciate that!
     
  8. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    Sounds like you're on the right track. I think now you're definitely armed with the tools you need to make improvements if you need them.

    I assume you're doing this by polling your web server every 3 seconds to request any updates? This is ultimately what we all did prior to websockets being available, so it is still in some cases a valid option, just make sure that you benchmark your current set up so that you know just how many players you can support before you have to start spending thousands per month for crazily powerful servers.

    No problem at all, glad I could help. Let me know if you get stuck on anything else.