- Home /
Couldn't Invoke RPC Function !
Hi every one I assume that my question is very simple but actually i spent hours or even days to solve my problem withuot any result, now i have a simple non-authoritative server game in which whenever a player connects to the server i.e. when OnConnectedToServer(), a player character is network instantiated with a set of preallocated networkViews
void OnConnectedToServer()
{
// spawn the player on connected to server
Network.Instantiate(playerPrefab, new Vector3(startPosX, startPosY, startPosZ), Quaternion.identity, 0);
}
each player prefab has multiple network views for different tasks (two for transform and animation sync and another two for rpc calls), i implemented a private chat app that each client can chat directly to another client connected to the server. seems no problem until now but the annoying message that keeps popup is when i send an rpc call to another client
View ID AllocatedID: 0 Level prefix: 0 not found during lookup. Strange behaviour may occur
and
Couldn't invoke RPC function 'FunctionName' because the networkView 'SceneID: 0 Level Prefix: 0' doesn't exist
to clear things out each player has his own 4 networkViews i mentioned earlier so in my opinion i can't just send rpc function from my networkView to another player's networkView even if they share the same script am i right ? (each player prefab has these networkViews), so i tried to forward the rpc function to the server and then the server forward it to the target client and actually it worked some times but now not. i tried to call the rpc function on the target player insted from whithin my player like this:
targetTransform.networkView.RPC("RPCFunctionName", targetNetworkPlayer, arg1, arg2);
but with no result, i tried also to activate the gameObject that the networkView is attached to before starting the game becuz the player starts with the cild that this networkView is attached to deactivated, the same error
any help will be appreciated and if i misunderstood the functionality of allocating view ids any coorection will be appreciated
Answer by Jix · May 20, 2014 at 10:37 PM
Ok, maybe having multiple networkViews on your playerPrefab is causing problems, I usually use just one for everything i want (transform, RPCs)
Also (just in case you're not doing it) did you write [RPC] before your function like this?
[RPC]
void RPCFunctionName()
{
}
unfortunately i can't use only one networkView, i can use at least two networkViews to observe transform and animation components and also this wont work becuz the scripts that is attached to children of playerPrefab that uses RPC functions will net be able to access these two networkViews (i cant put all rpc dependant scripts on the plaeyrPrefab parent game object) .
but if u have a better idea in dealing with sending data on remote devices than just attach a networkView whenever the script in the child playerPrefab want to send data remotly ?
Well, if I need to sync transformation and send RPC at the same time I set the networkView to RPC mode and send all the info I want in one RPC function, I can't send transform directly due to RPC argument type limitation but I send the position and rotation and make the receiver object translate to this position and rotate.
If the receiver will collide with object then I send velocity too because if an object(A) is just translating and touch another object(B) while (A) has velocity = 0 then it will just move (B) a little ins$$anonymous$$d of pushing it. Objects need velocity to actually push another object.
RPC calls have limited arguments (int, float, string, NetworkPlayer, NetworkViewID, Vector3, Quaternion) so if you want to send objects you have to be creative, for example if an object has 2 int and 1 float you can send your object in pieces or even better in a single Vector3 then deal with its component in the receiving function.
As for animations I haven't tried it before in RPC but I think you can send the name of the animation and the current time of the animation.
I know it's a lot of work to do but believe me it's not slower than the regular networkView that sync transform since you can send what you need exactly, for example if you don't care about rotation but just want the position. And it can save performance since you can call these RPC calls only when needed. It's a possible scenario that you want to sync the position of an object at a certain time.
Also bear in $$anonymous$$d that when there is an object not controlled directly by players (like the ball in a football game) let the server is the one to update its info to all players.
I too tried before to put several networkViews like you and it failed terribly, the solution I offer is hard and may cause conflicts if not planned carefully but it works and has good performance, I tested it on low speed internet connection (download/upload rates 100/20 kbps)
I forgot to add important note:Unity's physics engine doesn't give the same results for the same inputs, for example when a player kicks a ball in direction x with force y on one PC and you send x and y the other PC's physics engine might give different result, so you can ins$$anonymous$$d send the result.
from ur well-written explanation what i understand is the following points:
A lot of networkViews is bad thing for a single prefab.
state sync decreases performance and increases network bandwidth.
ins$$anonymous$$d of state sync it's preferable to serialize data when needed
Network.Instantiate is a nightmare so use RPC with viewID as parameter if i want to instantiate a player on all other machines
u didn't mention any thing about OnSerializeNetworkView so ithink its not recommended for sync position, rotation and animation right ?
this all very good and i actually started to apply that but here is the problem if u could clarify more i will be very thankful
first is it preferable to AddComponent() to each player prefab on instantiation or just attach the networkView before runtime and then assign a ViewID to him and reflect that to all players, second the player parent and all the position, rotation, and animation will be sync using rpcs but what if a child gameObject want to send rpc to another player that has also this child gameObject do i refer to that player locally and then call the rpc on his player prefab or i can't send an rpc directly to a specific client (this is by the way the error message that i described in the question when sending data to a specific client) ?
Your answer
Follow this Question
Related Questions
Get NetworkPlayer from ViewID 1 Answer
networkView not called on Child objects? 0 Answers
Networking Error! 0 Answers
OnSerializeNetworkView issus in my 2D game 3 Answers
Overloading RPCs 1 Answer