Hey guys,
I’m almost crazy with unity networking. I have a problem and after trying hard several days I reached at nothing.
If I want to describe fast my scenario, I would say:
In my game there’s one UnDestroyable GameManager object with a (scene type) networkView attached and nothing to observed. It does some GUI stuff to start the server (on server side) and connects the clients (on client side). So far so good. I want the connected players select a room Id and enter the game first scene to play. With this selected room ID I want to separate them into some lists in the server. Actually with this lists sever can decide how to deliver the RPCs just to the players in one specific ID and implement players interaction in one group apart from the others. In other words there are separated player groups in the game scene.
Now what’s the problem?
The problem is the GameManager on the client side after calling LoadLevelenters into the game scene correctly and then he spawns his player by allocating new viewID to his prefab’s networkView. His GameManeger also enters to the scene because it has DontDestroy. The player moves happily. The second player connects to server and enters the same room ID (And of course I set this room id as LevelPrefix before calling LoadLevel) and then he enters to the same room. This new player also can move happily but he sees the first player just frozen in his initialize place. And the same for first player he sees the second frozen. They can’t observed the movement transform.
I didn’t use Network.Instantiate at all 'cause I wanted to route RPCs manually so after instantiation on client, he will send an RPC to server to tell his new ViewID and the server pass this exact viewId to the proper room mates (with the same roomId) to Instantiate. And also I buffer up the needed RPCs in a List for future new commers. And that’s why 2 players see each other.
I’m affraid to miss something to tell but this one that when player connects to server he first calls LoadLevel and then in the new level spawn his prefab but in the server side the GameManger remains in the menu(lobby) and there’s no need to come to level with every single player LoadLevel calls (against Unity networking Auth example).
Taking a sidestep here: if all you need is persistent rooms…have you considered using PUN + cloud hosting? It could make things much easier.
Having dedicated unity networking servers has quite an overhead and is cumbersome to manage in my opinion.
But if your game requires server side physics there are very few options, and honestly running one client as the “master client” that does the physics stuff for the entire game is a seriously crummy/slow/unreliable approach.
I’ve tried the Photon could but the ping for my country was around 300, and the price of PUN having more than 100 CCU is not what I can pay now. So I decided to use pure unity networking. A little help or experience here can move me forward, I think I’ve just implemented 80% of the job. Now I have some misunderstanding of changing levels routine.
fholm you’re right but I’ll run my server side on a very good VPS located in my country and I’ve worked hard on physic implementations to reduce traffic as much as possible. I didn’t test the network speed yet but I can handle it. If I know what I have to do, I’ll do. The main problem related to scene changed is that I don’t know what to do
Wow, I started a new brand project just for networking testing purposes. I set one level as lobby and another as main game.
Then each player connects, Loads game level and spawn their own avatar with their viewID. Everything’s just OK. It works, but the problem is the same avatars are also created in the server ( which is in the lobby not game level) and the’re also moving in the lobby which is wrong !!!
in the SpawnPlayer function I checked if the network is the server then disable the player. This time I reached the same problem as I mentioned in this thread. It means the players must be active in the both server and client to match up transform.
Here’s the Spawn RPC:
[RPC]
void SpawnPlayer(NetworkPlayer playerIdentifier, NetworkViewID transformViewID)
{
Transform instantiatedPlayer = Instantiate(playerPrefab, transform.position, transform.rotation) as Transform;
var networkViews = instantiatedPlayer.GetComponents<NetworkView>();
instantiatedPlayer.networkView.viewID = transformViewID;
// Initialize local player
if (playerIdentifier == localPlayer)
{
// the new player
instantiatedPlayer.name = "Me";
instantiatedPlayer.GetComponent<DudeController>().enabled = true;
instantiatedPlayer.GetComponent<SmoothMouseLookCamera>().enabled = true;
//switch to player camera
Camera.main.gameObject.active = false;
instantiatedPlayer.FindChild("Main Camera").gameObject.active = true;
}
else if (Network.isServer)
{
// on the server
instantiatedPlayer.gameObject.active = true; // => here's the problem
instantiatedPlayer.name = "PlayerOnServer";
instantiatedPlayer.FindChild("Main Camera").gameObject.active = false;
instantiatedPlayer.GetComponent<DudeController>().enabled = true;
// Record player info so he can be destroyed properly
PlayerInfo playerInstance = new PlayerInfo();
playerInstance.transformViewID = transformViewID;
playerInstance.player = playerIdentifier;
playerInfo.Add(playerInstance);
Debug.Log("There are now " + playerInfo.Count + " players active");
}
else
{
// on the other clients
instantiatedPlayer.name = "Other";
instantiatedPlayer.FindChild("Main Camera").gameObject.active = false;
}
}
Then You think how It’s possible to have reliable observed on player without instantiating on the server? If I have 10 rooms and in each room 10 players then 100 players are moving in the lobby in front of the camera!!!
Do you have any idea?
simply use the network group feature of unity. At instantiation you can use Network.Instantiate and then pass the group number as last argument (not sure). All messages including RPCs will go throw that group and users of each group don’t receive RPC and state synchs of other groups.
Did not use unity’s networking for a while but it was like this if i remember correctly after more than 1.5 years. I’m not advertising any middleware but we have an article linked below on choosing networking middleware for unity games which you can check out to see if what others are saying or other options or maybe unity’s built-in stuff is good for your project.
Thanks a lot, but the group separation on unity Networking (Network.Instantiate as you mentioned) is not what it sounds to be. It just gives some priority to messages I think. I tested it in some cases, the result was negative. I think Smart Fox and specially Photon are really good solutions. But actually I have the same problem in Photon now. The guy who starts a room must join the room! that’s the thing I have problem with now. But in Smart fox that’s OK. I’m checking these both at the same time, I took a look at ulink in the past but I’ll check it in deep sooner or later. Thank you.
Well! If you are looking for other solutions, check uLink well. UnityPark suite (which contains uLink) is really great at that. You have two different things there (not counting pikko server).
1- uLink 1.3 is comming out (the beta is available to public) which allows these groups in a much more advanced way compared to unity networking but with it’s ease. It allows the group owner and even object group to be changed. You can filter data based on group and scope and they are creating a shiny nice example (as they say) for it as well. The build is fairly stable.
2- you have uStream to filter user data based on Area of Intereste AOI ( a circle around each user’s player) to filter their data for each other.
I think what you want is the first case but the second might be interesting as well.
You have the benifit of running unity code on server here like unity’s built-in version so you’ll be faster in porting. If you want to use a service like photon cloud or … that’s a different story.
I did not talk about it because to me it seemed that others are somehow advertising stuff instead of answering your question. I still think that it’s possible to do with the built in networking but unfortunately most games with commercial requirements aren’t that possible to be made with buit-in networking of Unity.
uLink 1.3, as ashkan says, introduces some epic Network Group functionality that is expressly designed to do what your asking. The beta is available, it works super great.
Unfortunately the most of advanced developers in networking manner work for Photon company and as you cleared out they would not answer purely the questions. As I sent the messages to them the first answer was clear: Why don’t you use Photon instead?
But not all of them, some friends here are so kind. they share their information and experiences to other developers. Actually that’s why this forum works well.
@primederektive Yep , i agree. I did not mean they are evil. Even those who sometimes act like living advertisements are knowledgable and help others in many cases but this act is not a good thing. When you post a question, You are not looking for ads, you are looking for an answer.
Well with a question like this you’re bound to get lots of suggestions to use other networking middleware not because we have any vested interest in you using them, but because the built-in networking isn’t really good at interest management/network scoping.
It’s so obvious the next step is gonna be uLink, now I’m on Photon and the way it handles the operations is very good. Specially the support of reliable UDP messages is one thing I couldn’t do in my own code easily. The routine is very clear and has no conflicts with my previous codes that I implemented with unity built-in networking because in that code I used the operation codes as well (but in my own manner). But as I describe I have the same problem and it didn’t help me.
@Ashkan_gc: I have a burning desire to verify the uStream which you said it is coming with uLink. @PrimeDerektive : I started this thread after sending a couple of messages to some friends. And the answers were interesting
Also it’s a good idea to check if you really need multiple rooms in the same server/process or not. By doing some profiling you can see if it’s more useful and resource friendly to run single games per app or multiple rooms per app process. Of course the amount of calculations that you have on the server and the average number of RPCs per game per minute are good values to examine for this.
Keep in mind that in case of unity built-in networking and uLink you are using unity on the server which is a single threaded process and no load balancing inside it happens so having more processes means more opportunity for load balancing. If the process becomes so lightweight, you’ll waste resources due to it’s overhead of running a process and it’s amount of RAM required and … If it becomes too heavy then chances are high that framerate drops, however you can have multiple processes for multiple games and allow the CPU to put them on different cores and they’ll run fine and you can do load balancing between them (uStream and uPikko in unitypark for uLink). uPikko is pricy and is not required for your game however. It’s the uLink integration of pikko server.