- Home /
Lets talk multiplayer: General Questions?
So as it stands I've spent a lot of time creating a primarily single player experience.
However the experience would suit itself to being multiplayer quite well, before now I haven't had the skills to implement somethings so ambitious however for now I would like to try. I have a few questions on networking.
So far I have been able to have multiple players playing in a server generated random gen map with independent movement, and player control.
I have run into some stumbling blocks.
my biggest question is:
tl;dr HOW does one run an update script of an AI on a network so that the AI behaves the same on all computers??
Answer by Em3rgency · Jun 24, 2013 at 12:29 PM
Have the server do the AI calculations and send the players the results. All results will be the same.
For a slightly more advanced implementation, to reduce lag issues, have the scripts also run on the players side, but all the player-side objects or actions or whatever should be temporary, that get destroyed/revoked after either receiving the confirmation from the server or if a certain amount of time has elapsed.
In general it is accepted that the server handles(or at least confirms) all logic calculations. You can't have the client do calculations and then just send them out to everyone, because a cheater might rig his client to his advantage, and if there is no server verification on the calculations, he'll get away with it.
Answer by Jamora · Jun 24, 2013 at 12:33 PM
You only need to run the AI update on one computer, then transfer the effects to the other computers. The following is how I made a very basic multiplayer top-down shooter AI:
NetworkInstantiate AI mobs from the server. Have clients remove the AI scripts. The NetworkView on AI mobs observes the transform ( so only the position of mobs is updated on clients ). Whenever an AI mob shot, the server calculated if anyone got hit and how much hp they lost. Then the server called an RPC on all machines to update their values accordinly.
Thank you, this was very helpful. I too am building a top down shooter, so its the same thing :P I will try what you said.
You can use the OnNetworkInstantiate callback.
The code could look something like:
void OnNetworkInstantiate((Network$$anonymous$$essageInfo info){
if(!networkView.is$$anonymous$$ine){
Destroy(gameObject.GetComponent<AIScript>());
}
}
Perfect, thank you! I wish I had enough rep to up vote your answer!