- Home /
Multiplayer in Action RPG, best approach?
Hi,
In the last couple of days I've been reading a lot about multiplayer in Unity and in general. I have an action RPG game that is nearly ready and I'm thinking about adding multiplayer at a later stage post release.
From what I understand there are multiple ways to handle synchronisation of data between the different connected clients. For example, it's pretty obvious that the other human players can be synchronised with NetworkView that automatically syncs position and rotation of an object across clients. But what about the 50 enemies that are using pathfinding and local avoidance? How would you sync the AI across all clients? Does the entire AI logic have to be on the server?
For example, does the server have to determine "spawn enemy there, now go there, now attack this player, now cast a spell on the other player", etc etc.
What's the best approach of an action RPG game?
too much to write in depth, but what you are looking for is authoritative or semi-authoritative server. Bad news is, networking is not something you simply "add" to existing game without facing multiple problems like the one you have described above. It's much better to think about network architecture as early as possible.
Answer by mediumal · Aug 13, 2014 at 06:13 PM
I am also working on an action RPG with (cooperative) multiplayer support.
The AI logic doesn't necessarily have to all be on the server. RTS games like Starcraft avoid syncing large amounts of enemies by distributing the simulation on each client. However, having each client handle AI individually and end up with the same result requires a deterministic game engine, and that is going to be very hard to achieve in Unity.
Instead I went with having the server control all the AI. The clients only see the end result of the AI via synced rigidbodies and animation states of all enemies. I then use RPCs when enemies want to interact with remote players and vica-versa, for things like damage. It is useful when taking this approach to separate the decision-making code that should only be run by one client with the rendering code that needs to happen on all clients.
Before worrying about the network overhead of 50 enemies just try it. If it ends up being a problem then you can try to optimize it, and if it isn't a problem then you've saved yourself a lot of time over the distributed solution.
"separate the decision-making code that should only be run by one client with the rendering code that needs to happen on all clients."
Thank you!! :) I had my server ("first") handling everything, for all enemies, and it was lagging like crazy for all other clients. I read this and change all "rendering" (movement, animations) to run through RPCs; keeping the server still making the decisions. Runs perfectly smooth! =)
Answer by KiraSensei · Aug 13, 2014 at 12:01 PM
For the position of mobs, I don't understand why a network view attached to them would not work as well.
Then for spawning, the server has to know where and when it happens, so if the mob handles itself, it must talk to the server, and then the server will tell all the players where it is, or the server handles the spawning directly, and instantiate the mob threw the network to all players. The pathfinding in my opinion does not have to be handled on the server, only directly by the mob, and then the network view does the job. For the spell casting, the mob handles itself, but has to tell the server what it does, and then the server tells to every player the action.
There should be other possibilities of course, but this one should work !
Thanks for the answer. I think a network view attached to each enemy won't work because of the huge network overhead that it would cause. I'm thinking about what would happen if a mob decides two different things on the different clients. For example if the pathfinding and LA is handled locally, on one client the mob might end up close enough to a player for a melee attack, while on the other client it might decided that it should use a ranged attack. That's my main concern of "syncing the AI".
I can't really see how exactly would you like players to have the same game experience without synchronising mobs, which will require either attaching NetworkView to each mob or writing some custom solution with single object synchronising all mobs, which will be a nigthmare to write, synchronise and manage. If you are so concerned about network overhead, you can flatten Vector3 to 2 ints and send them over network, but I seriously doubt it will make any difference.
Why do you think the mobs would decide two different thigs ? Since one decision is sent for all players, everything would work correctly !
Because its network and character positions are slightly different, "ping behind". And how much they are behind is unpredictable, so the whole thing is non-deter$$anonymous$$istic. If decision is "attack player A", chances are that player A is out of sync, in fact, it will never be in sync unless it stands still.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Client Disconnect with message “Peer Created” - PUN 0 Answers
Multiplayer Problems 1 Answer
Instantiated objects does not appear on clients when connecting 1 Answer
Multiplayer Networking in Unity? 1 Answer