Hello everybody!
I am trying to understand some of Unity networking basics and I’ve faced with some troubles.
I must put on Network View component only on prefabs and then Instantiate them into scene. I can’t do this with GameObjects without instantiating via Network.Instantiate?
If i have some RPC functions I must declare them both on server and on client? Ex. bot. When he is dead I must send RPC to show death animation. But he can be killed by server or by client so I myst have 2 functions ex. BotIsKilledServer, BotIsKilledClient. Am I right?
And the lat question: if parent has Network View component is it necessary to put on NetworkView component on some of its children if I want to do some actions with them via network?
Thanks a lot. These questions are very important for me.
Thank You very much, apples!
So all object in the scene which were put on it manually are owned by server?
But I have some troubles with RPC. I have bot. I put on it function
Hmm. No. It is true(((. My bot is a game object. I don’t create it through Network.Instantiate. I put it on scene by drag-and drop and put on it network view component. I have script which must move my bot, but it doesnt work too. May be I must create it by this func? Thanks.
I have some progress now. My bot is a gameobject that didn’t created by networking.instantiate, so as I have correctly understand its owner always will be server no client. So I must check on server if the bot is dead - send message to all that he is dead? Ex
if (networkView.isMine)
{
networkView.RPC("Dead", RPCMode.Others);
}
I am confused with networking:face_with_spiral_eyes: so thanks for Your’s attention:smile:
Basically good advice is to put all scene and logical objects in scene before runtime and set manual IDs and instantiate all player related objects by them to be their owners as well. NPCs should be instantiated by server if they are being controlled by server.
Thnaks to all. I’ve solved my problem. But I have some new questions.
About RPC.
If I have for example class BotAi with method MinusHealth(int ammount) declared as RPC and have some other class, for example BotTrigger where I send an RPC message networkView.RPC(“MinusHealth”, RPCMode.Others, ammount); and these two classes I put on one game object (my bot). In this case there is no necessity to re-declare method MinusHealth in BotTrigger class. But if BotTrigger class was on element which is inside my bot (child element) I must re-declare it or put BotAi class on this element also. Am I right? Sorry if my explanation is confusable.
And the second question. As I have correctly understand better way to develop networking games is to write authoritarian server. In these way all object with NetworkView component must send their messages to server. An on server I must check them all, make some decisions and then send RPC messages to objects. Am I right? For example Bot sends to server message MinusHealth. Server checks it and if everything is OK sends bot an RPC message MinusHealth.
Thanks for your attention.
About your second question, Yes authoritative servers are a more secure way of running the game with a heavier load on server, so if you need to prevent cheating in your game, based on game/business requirements then you should create an authoritative or semi authoritative server and to do that you should send RPCs from client to server which is a request to do something and server sends back to him and any other intersted party that if it’s accepted or not.
youre checking if bots health is at 0 in a OnEnterCollision function? dont you think you should do that in the update() function, so if the bots health is at 0 (function Update()), then do the RPC call
No, I am checking it on update. And I’ve solved this problem. Thanks for Your’s attention.
Thanks. I’ve read about authoritative servers but some things are confusable. I can create some server class which will recieve RPC from clients and make some decisions. And then put this class on every Game Object which will be in network. Or there will be easier way to include my class without putting it on every GO?
A good approach is for each class that is for a networked object, you have two different classes, one for client gameObject and one for server one or in the same code file have both codes with checks like isServer which is haphazard at best, but if you choose to do that, be careful to not compile server code for client using a #if SERVER directive or something like that.
Thanks. I’ll try this.
And one more question. If I have buildings and some other stuff, that are in the scene they are owned by server (as appels previously replied me
In this case there is no necessity to check networkView.isMine? Better way is to send RPC with RPCMode.all with object networking id?
And one more question: can I get access to gameObject via networkView.viewID?
I saw sode: viewId.gameObject.transform?
And If i correctly understood all object which are creating via Network.Instantiate will have (Clone) word in their tag?
Well after assigning the NetworkView component then you should assign a unique id to the object in scene. Any instantiated object with Object.Instantiate (normal one) or Network.instantiate wil have (clone) in its name. to find the gameObject with a specific id i think you can use NetworkView.Find but since it’s a hell lot of time that i did not use built in networking i’m not sure on this one.