Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using Google Cloud Platform for Real-Time Multiplayer

Discussion in 'Multiplayer' started by OldHarryMTX, Feb 26, 2020.

  1. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Hi Everybody!
    Recently I am experimenting with various networking solutions for the project I am developing. Although I found a possible way to go with Photon, I wanted to explore the features offered by the Google Cloud Platform service for a Real-Time Multiplayer application.

    After a search, however, I managed to find only articles and podcasts that mention a collaboration between Google and Unity, without finding actual documentation, tutorials or applications.

    Do you know if it exists and where can I find this material?

    Thank you in advance
     
  2. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
  3. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Actually, I'm thinking on something different, like firebase, or even better, a Google virtual machine, but I don't know if it can ensure a data transmission sufficiently fast for a, for example, 6v6 fps or moba.

    I would like to try it but I would appreciate some introductive docs first ^^
     
    Last edited: Feb 26, 2020
  4. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    Firebase is an app building framework, a Google virtual machine is just "hardware with an OS" that you can use to run applications. Apples and oranges. Also Photon is yet another kind of "fruit".
    I think you should clarify what exactly you're trying to do.
     
    Joe-Censored likes this.
  5. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    You are right, I have not been very clear. I try to explain what I'm going to try when I have some time (probably this weekend ...).

    I would like to create two different builds of my project, one that works as a server and one that works as a client, so that they follow this pipeline:

    • The client collects the player's inputs and sends them to the server.
    • The server processes them, moving prefab, spawning GameObject, checking for collisions, using NavMesh, even saving some permanent datas using PlayerPref.
    • These objects are then synchronized on the client which shows them on the screen.

    What I would like to do is create two different builds, removing everything related to interactions and events from the client code, and all the graphics part from the server.

    So put the server on a Google VM (which being linux based should allow me a headless build) and see what happens.

    I was thinking of using Unet as a networking system, although deprecated, but I don't know if it allows me to create two asymmetric server and client applications.
     
  6. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    Ok now it's clear and it seems perfectly doable. I'd expect the server part to be able to handle a 6v6 FPS on a single Google VM.
    As regards the two builds, I am not sure how much stuff can be stripped off of the two executables. In theory they need most of the assets in order to either apply the server logic or just render them on client. Maybe you could use conditional compilation to exclude code that is not needed on one side? I am not sure, though.
    Yeah, as you said it's deprecated, so it would probably be best to look into a possible replacement:
    https://forum.unity.com/threads/wha...of-available-network-solutions-assets.609088/
     
    Last edited: Feb 27, 2020
  7. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Yes, I was thinking of starting by separating the two situations with conditions, something like "if (isServer) {...} else {...}", and then maybe cleaning up the code if I get results. As for assets, I wanted to see if I can use different prefabs, for example a simple cube for the server and a more complex geometry for the client.

    Thanks a lot, I take a look!
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It sounds like what you want has nothing to do with Google specifically. You just need a server to run your server build.
     
  9. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Ok, let me give you some updates:
    I prepared the project, using Mirror as Networking system because it is similar to Unet and, in my opinion, suitable for my purposes.
    Using if (isServer) {}, if (isClient) {}, if (isLocalPlayer) {} and obviously [Server], [Client] and [Command] I have outlined the two cases, server and client.
    Finally, to actually create two different builds, I used the build symbols (#define #if #endif) to exclude all code delegated to the server from the client. (in this link it's explained how to set the symbols for the whole project).
    I will give you an example (this is not actual game code)::

    Code (CSharp):
    1.  
    2. [ClientCallback]
    3.     private void ManageInput()
    4.     {
    5.         if (isLocalPlayer)
    6.         {
    7.             if (Input.GetKeyDown(KeyCode.Alpha1))
    8.             {
    9.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.                 CmdShootRocket(ray);
    11.             }
    12.         }
    13.     }
    14.  
    15. [Command]
    16.     private void CmdShootRocket(Ray ray)
    17.     {
    18. #if SERVER
    19.         if (cooldown <= 0 & Physics.Raycast(ray, out hit, Mathf.Infinity))
    20.         {
    21.             Vector3 direction = hit.point - transform.position;
    22.             direction.y = 0;
    23.  
    24.             SpawnRocket(direction.normalized);
    25.         }
    26. #endif
    27.     }
    28.  
    29. #if SERVER
    30. [Server]
    31.     private void SpawnRocket(Vector3 direction)
    32.     {
    33.             GameObject rocketClone = Instantiate(rocket, transform.position, transform.rotation);
    34.  
    35.             RocketControls rocketControls = rocketClone.GetComponent<RocketControls>();
    36.             rocketControls.Direction = direction;
    37.  
    38.             NetworkServer.Spawn(rocketClone);
    39.  
    40.             cooldown = 3;
    41.     }
    42. #endif
    43.  
    As you can see in the client build you can exclude all the content from the [Command] functions, because is processed on the server.

    Now I fix two more things and then I try to put the server on a Google VM.
     
  10. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Ok, I put the server on the VM and I have to say that everything works perfectly.

    But I need your help again. Since on the server is a headless build, I need to show any log messages directly on the console.

    I was unable to find a direct solution, but only workarounds that involve creating a log file using the -logfile command and then printing changes to the file on the console.

    For some reason, however, my build does not generate the Player.log file. I have trie dto change the writing permission in the directory and even change the destination path, but nothing.

    Can you help me?
     
  11. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    I think it's probably best to use log files and if you want to see their content live you can "tail -f" the log file. At least on Linux and Mac. On Windows I believe there's something equivalent via the power shell.
    You could also try using Console.Writeln() but maybe Unity redirects that output away from the standard output, I don't think I've ever tried it.
     
  12. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Yes,
    Console.WriteLine()
    is also redirect.
    I have tried various solutions for this problem, and all of them involve the use of the
    -logfile
    command.

    After several attempts I found two solutions: use
    -logfile -
    , which, in theory, should send log messages directly to the console, or
    -logfile server.log
    , which generates the log file which, as you say, can be sent to the console via the
    tail -f
    command.

    The problem is that in the log file there is no trace of the
    Debug.Logs
    launched by the server, but only the normal report generated by unity and by the networking system (Mirror).

    Do you know why it happens?

    Thank you in advance
     
  13. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    Weird, indeed.
    Debug.Log should be sent to the log file. Do you also pass other command line params to the unity executable?
     
  14. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Not for now, since I don't need to pass
    -batchmode
    and
    -nographic
    (the build is already set headless when i compile it). Now I'm at work, as soon as I can I try to add more
    Debug.Log
    , maybe one every few seconds, and i watch what happens.

    Thankyou for your help
     
  15. MrsPiggy

    MrsPiggy

    Joined:
    Jun 13, 2018
    Posts:
    154
    What's weird is that I think I've already done something similar sometime ago, and it worked. It was something I did for a client, but it doesn't come to mind right now. I'll see if can refresh my (faulty) memory.
     
  16. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    You are great man!
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  18. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Nono, but if I run the build in the editor I see the log in the editor console as normal
     
  19. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Guys, i'm very sorry
    I've tried to cast a Debug.Log every 4 seconds inside a GO and it actually works. I don't know what is changed, but now it works.

    For the ones that want to know how to do that, you just need to launch the program with the command
    -logfile -
    , E.G.:

    ./Server_Linux.x86_64 -logfile -


    Thankyou again for the help!
     
    MrsPiggy and Joe-Censored like this.
  20. ScorphiusMultiplayer

    ScorphiusMultiplayer

    Joined:
    Nov 10, 2018
    Posts:
    66
    Hi OldHarryMTX,
    Could you please list down the steps taken to start the server on gcloud VM ?

    Thanks
     
    Brodus likes this.
  21. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Hi RatBytes
    I simply created a new instance, using almost all the default settings as I am still in testing and need the most generic configuration possible. So I opened a port that the server will listen to using this guide. Finally i started the vm remote console and loaded the .zip of my project.

    Once there you must install a linux app to manage zip files using the command
    sudo apt install unzip
    and lunch the server with this commands:

    Code (Boo):
    1. //unzip the archive
    2. unzip -q YourServer.zip
    3. //enter in the directory
    4. cd ./YourServer
    5. //set some privileges
    6. sudo chmod +x ./YourServer.x86_64
    7. sudo chmod 777 ./
    8. //go back to the upper directory
    9. cd
    10. //launch the application
    11. ~/YourServer/YourServer.x86_64 -logfile &
    If you need to clean an old version you can launch this two commands before loading the new one:

    Code (Boo):
    1. //delete the old directory
    2. rm -rf YourServer
    3. //delete the old archive
    4. rm -rf YourServer.zip
     
  22. ScorphiusMultiplayer

    ScorphiusMultiplayer

    Joined:
    Nov 10, 2018
    Posts:
    66
    Hi OldHarryMTX,,

    I followed some of the steps and was able to host on gcloud.

    One more question. How do I stop the game application that is launched ? Google Search did not help.
    I ended up shutting down the instance. :(

    Thank you for your replies.
     
  23. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    Take a look here.
     
  24. adlabs

    adlabs

    Joined:
    Dec 31, 2020
    Posts:
    2
    Hi @OldHarryMTX
    Are you using google cloud free tier VM named as f1-micro?
    If yes, how much max CCU it can handle for free, I am an indie dev and new to multiplayer networking, so exploring best options for multiplayer server which could be compatible with firebase, which I have already integrated for database purpose. Thanks
     
  25. OldHarryMTX

    OldHarryMTX

    Joined:
    Sep 18, 2018
    Posts:
    45
    I suppose by CCU you mean Currently Connected Users, right? I haven't run any tests on it yet. The thing I highly recommend is to limit the number of fps of the Server instances on the VM, using targetFrameRate, otherwise the application will tend to use all the available CPU.
     
  26. adlabs

    adlabs

    Joined:
    Dec 31, 2020
    Posts:
    2
    Thanks for responding, yes I meant by Concurrent Users. So if we take a hypothetical situation, lets say if my game has 1000 daily active users (each plays for 10 mins), since my game is a real time multiplayer game which needs physics sync also, and I am running headless unity instance(s) for multiple rooms, will f1-micro free instance able to handle it? if no will it auto scale ? and how much it ll cost appx ?

    Else If you can share your DAU, costing and avg user time per session, I can calculate costing for my scenerio.
    Thanks