Help me with basic understanding of Unet!

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!

  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?
1 Like

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

https://www.youtube.com/watch?v=HlDwGmskCbo

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).

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

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!

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

1 Like

So basically i have to learn LLAPI to work with dedicates?

No, you can do it with HLAPI aswell

1 Like

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?

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

1 Like

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:

using UnityEngine;
using UnityEngine.Networking;

public static class GameManager
{
    public static bool IsServer
    {
        get
        {
            return NetworkServer.active;
        }
    }

    public static bool IsClient
    {
        get
        {
            return NetworkClient.active;
        }
    }
}
1 Like

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.

1 Like

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!

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

1 Like

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!

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

1 Like

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!

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

How to? D:


It depends what VPS you are using. Usually they have a dashboard.