Search Unity

Help me with basic understanding of Unet!

Discussion in 'UNet' started by BouncedPhysical, Jul 24, 2017.

  1. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    Hi guys, and thanks to be there.

    So i just want to retry to start by learning UNET and port my game on Network.
    (I say retry cause few months ago i've spent almost 2weeks on Unet without really understanding anything but being able to do some Networked things.)

    So i want to start with some really beginner concepts that aren't so clear for me

    1 * From what i know, the Server & Client must be developed togheter, am i right? There are callbacks that must run only on the server but can be called by the clients.
    2 * The server MUST be a Player of the game (and at the same time a client), am i right?

    3 * So by assuming that i'm right on the 1&2 affermation: can i use a VPS service like "Hamachi" to host the server and don't have it also as a Player?
    Because right now i think i can not!

    4* And by assuming that in the 2* i'm wrong, how can i manage this?

    Many thankses in advance!
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    1. It doesn't need to be developed together. You can make it different Scripts. Or different projects all together.
    2. No, you can run normal servers, normal clients or Hosts (Client and server in one).
    3. Don't know what Hamachi is. But any VPS will work. I use amazon AWS for hosting dedicated servers.
    4. Please elaborate, manage what?
     
    Deleted User likes this.
  3. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    How can i actually run a normal server? Do i have to make it a Unity instance or something else?
    I just saw this video, and this guy got a folder called "Server_Data" where he start a CMD and actually (i think) start the server


    So how i can mange connections from Unity to the server, and mostly important the callbacks (that if i've understood right must be runned only on server).
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    If you use the HLAPI you can simply use NetworkManager.singleton.StartServer().
    And yes, it's a unity instance. To run it from terminal with the least amount of overhead. Run it with the args -headless and -nographics
     
  5. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    Okay thanks so much!
    This video shows exacly what i want to archive, so for having a terminal i just have to run the builded project with those arguments? But how i will say "this instance is the server" - "this other is a client" i just saw a lot of project and one guy had a pre-menu where he selected the purpose of an instance, but it was so ugly! And i haven't understand yet how to manage the servers callbacks!
     
  6. Krause-Biagosch

    Krause-Biagosch

    Joined:
    Mar 8, 2017
    Posts:
    19
    hi
    i create a client/server solution 2month ago with this nice tutorial
    with 0 expirence in Unity/Multiplayer

    and i have good understand whit this video how it works and extended function und events for me how i need it
     
    BouncedPhysical likes this.
  7. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    So basically i have to learn LLAPI to work with dedicates?
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    No, you can do it with HLAPI aswell
     
    BouncedPhysical likes this.
  9. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    Again my question is, how? Sorry, i don't want to seem like a new guy on unity that want to make a game without knowing everything and just asking "how how how".

    i thought to have a #define to distinguish between client and server when i run the build
    For example:

    #if SERVER
    // server logic
    #endif

    #if CLIENT
    //server logic
    #endif
    Then i use a NetworkManager to the the calls, but basically the server calls what will be?

    Is a good idea or there are betters?
     
  10. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    To actually know what kind of build it is at runtime?
    Easiest way is to have a boolean that you set before you build it. but it is possible to detect if the game is running headless mode during runtime
     
    BouncedPhysical likes this.
  11. Deleted User

    Deleted User

    Guest

    The defines would just cut the code from scripts after the actual build.
    If you want to split the logic, you should check for (NetworkServer.active) - for server, NetworkClient.active for Client. Or if you are on HLAPI you could just mark the function as [ServerCallback] or [ClientCallback], it would add the (NetworkServer.active) automatically.

    Check my topic about defines in order to understand why you should or could use it:
    https://wobesdev.wordpress.com/2016/10/10/preprocessor-directives/

    Edit: you could add the static variables to your GameManager and get these values via getter:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public static class GameManager
    5. {
    6.     public static bool IsServer
    7.     {
    8.         get
    9.         {
    10.             return NetworkServer.active;
    11.         }
    12.     }
    13.  
    14.     public static bool IsClient
    15.     {
    16.         get
    17.         {
    18.             return NetworkClient.active;
    19.         }
    20.     }
    21. }
     
    Last edited by a moderator: Jul 24, 2017
    BouncedPhysical likes this.
  12. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It's actually simpler and tidier to have different scripts for Client and Server logic. You can use NetworkIdentity - Server Only to separate what objects are enabled on the client and what's only on the server. Mark methods with [Client]/[Server] to see what's executes where.

    Whole idea that making "host" with both client and server at the same time is simpler... It's "kinda" nice for newbies, but at the same time it's just a deathtrap for a project. Don't do it. Stick with Server-Client approach, that way you'll get in the result a very good and maintainable code.

    Using multiple projects will be tricky, but doable. Make sure you've got powerfull enough machine to run two instances of Unity Editor, 2xIDE's and prepare for some crashes. Because...Unity!

    Also, about scenes and prefabs - make sure they're perfectly identical in terms of NetworkIdentities AND .meta files! This is important. Set Edit->Project Settings->Editor->Version Control->Visible meta files. Because when you're transfering those from one project to another id's might get lost, resulting in a netId missmatch.

    Also, you can always use both, HLAPI and LLAPI. HLAPI is quite usefull for common things syncronization, and LLAPI is quite good at passing single object data and doing sync manually.

    Basicly it all comes to the achitecture point of view. Just plan something and stick to it. Time will show whether it's a good design or not.

    Just don't forget to check internet first. Otherwise you'll be reinventing a wheel once again.
     
    Last edited: Jul 25, 2017
    BouncedPhysical likes this.
  13. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    That's what i'm doing now, thank you!
    I was able to do this separation to client/server, i just have to see how it will work in the future.

    So now i've started a local host on the server headless and i was able to connect from a client to it, so that's amazing!
    Now i have one more question, how can i add an IP server address? I tried to change the NetworkAddress inside the NetworkManager but i think that this not works!
    That is what i got before pressing "LocalHost"
    http://imgur.com/a/VJTd5
    And this is after i basically press the HostLocal (by a script)
    http://imgur.com/a/EfWRg

    If i click on Stop and LocalHost again i got in the NetworkAddress: "localhost" again.
    (Basically i've inserted the hamachi IP in the networkaddress, but doesn't work!)

    Still didn't understood!
     
    Last edited: Jul 25, 2017
  14. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You should not really edit the host address. That's just the interface IP. But just get the VPS public IPv4 and connect to it with the client
     
    BouncedPhysical likes this.
  15. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    Don't i have to host the server from Unity with the NetworkManager from the server instance and by using the hamachi IP?
    And then i connect from the client by using the same ip?

    Thanks for you answer!
     
  16. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    The Server address field should be untouched unless you know what you are doing. It's the interface to listen on I believe. As for client, just use the server computers public IP
     
    Last edited: Jul 25, 2017
    BouncedPhysical likes this.
  17. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    I think that i didn't set the idea as well.

    So i don't have to touch the server address component, that's ok.
    I'm using a vpn service that allow me to have a server, this service gave me a IP.

    Now i want to host the Unity server on this IP, and i want that the client connect to this server.
    It's more clear? Thanks!
     
  18. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Yes, Upload the server to the VPS. And then simply run it on that VPS. Then find the VPS's public IP. And use that on the client to connect
     
  19. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    How to? D:
     
  20. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    ...
    It depends what VPS you are using. Usually they have a dashboard.
     
  21. BouncedPhysical

    BouncedPhysical

    Joined:
    Jul 16, 2016
    Posts:
    58
    Y, the dashboard contains just of slots, members and it give me the IP.
    Do i have to insert this IP to make the NetworkManager connect with this?