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?
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).
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!
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?
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
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.
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;
}
}
}
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.
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!)
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?
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