- Home /
Server/Client problem : rpc call
Hello everyone !
First of all, merry christmas :)
I have a problem in my FPS game when I try to handle damages with a specific weapon.
This script is attached to a player's knife :
var isShootenBy:NetworkPlayer;
private var firstShootDone:boolean = false;
function OnTriggerEnter (other:Collider)
{
if (firstShootDone) return;
if (other.networkView && other.networkView.owner == isShootenBy) return;
if (other.gameObject.tag == "joueur")
{
firstShootDone = true;
Debug.Log("joueur touché au couteau");
Hit(other.transform);
}
yield WaitForSeconds(0.24f);
firstShootDone = false;
}
function Hit(obj:Transform) {
var currParent:Transform = null;
if (!obj.parent) currParent = obj;
else
{
var nextParent:Transform = currParent;
while (nextParent)
{
currParent = nextParent;
nextParent = nextParent.parent;
}
}
currParent.GetComponentInChildren(PlayerScript).networkView.RPC("IsHitBy", currParent.networkView.owner, 0, isShootenBy);
}
It sends to the shot player some intel like what weapon hit him, and who shot him.
Then, on my player game object I have a PlayerScript attached to its son that contains the "isHitBy" method :
@RPC
function IsHitBy (from:int, by:NetworkPlayer) {
if (isDead) return;
var damages = 0;
switch (from) {
case Weapon.Couteau:
damages = couteauDamage;
break;
case Weapon.Arbalete:
damages = arbaleteDamage;
break;
case Weapon.LanceFlammes:
damages = lanceFlammesDamage;
break;
case Weapon.Minigun:
damages = minigunDamage;
break;
}
}
My problem is the following : when the client player hits the server player, the server player looses life (correct behavior), when the server player hits the client player, I have a core dump, big crash and the windo closes...
So here is what I already tried to find the problem : if I comment the rpc call on IsHitBy from my knife script, everything is fine (but damages are not applied anymore ... logic), if I let this line uncomment, but I comment everything IN IsHitBy, then I still have a core dump. Both my players are instantiated in the same way, there are both instances of the same prefab.
Thanks by advance to anyone who can help me !
Answer by asafsitner · Dec 25, 2012 at 03:40 PM
One problem I can identify immediately is that the player cannot send RPC to another player directly by themselves, unless that other player is the server.
Because the server is connected to everyone, the server knows the other players by 'name' (i.e. by their NetworkPlayer struct) so the server can refer to each player individually.
The clients, on the other hand, are only directly connected to the server, so they can only ever send an RPC to the server. If they send an RPC with a RPCMode
different than 'RPCMode.Server` what happens is that the player sends the RPC to the server, which then relays the RPC to the relevant players, according to the flag (All or Others).
This is again, because the player can't send an RPC to others by themselves.
So if I understand correctly, my code should only work when the server player hits a client player ? Because it is the opposite that happens ...
EDIT : the thing is I use the same code with an other weapon and everything is fine ...
I guess I got confused by 'when the server player hits the client player, I have a core dump, big crash and the windo closes...'
I tested something else :
if (Network.isServer) currParent.GetComponentInChildren(PlayerScript).networkView.RPC("IsHitBy", currParent.networkView.owner, 0, isShootenBy);
else currParent.GetComponentInChildren(PlayerScript).networkView.RPC("BiduleIsHitBy", RPC$$anonymous$$ode.Server, currParent.networkView.owner, 0, isShootenBy);
So with that I test if the shooter player is the server, if yes I directly send (as before) the intel to the shot player, if not, I send the intel to the server who redirect them to the concerned player. This is not working at all :)
I always go to the "else" part...
Damn ! I don't understand why, but if I put the test :
@RPC
function BiduleIsHitBy (target:NetworkPlayer, from:int, by:NetworkPlayer) {
if (Network.isServer) IsHitBy(from, by);
else networkView.RPC("IsHitBy", target, from, by);
}
in this method, everything works !!
So my problem looks solved, it looked like it was something really close to what you said, but I can't explain why in my weapon script I go everytime in the "else" part, like no one was the server ...
Edit : if my function is :
@RPC
function BiduleIsHitBy (target:NetworkPlayer, from:int, by:NetworkPlayer) {
IsHitBy(from, by);
}
It works very well too, I really don't understand what's happening ...
Your answer
Follow this Question
Related Questions
Does the position change need state synchronization? 2 Answers
RPC and inheritance 1 Answer
Send GameObject active in network to all clients 2 Answers
Disconnect a player from a network 1 Answer
Network.Destroy(), Couldn't perform remo 0 Answers