- Home /
Other
RPC call problem : local variable unvaluated ?
Hello everyone !
In my FPS game, I try to make a score dialog. To manage it, I have some informations in a script existing in all clients (and the server).
In the Awake() method of my player script I have these lines :
if (!this.transform.parent.networkView.isMine)
{
enabled = false;
return;
}
To be sure that all the following code won't be called from another player.
So when one of the players has his life at 0, I call :
networkView.RPC("NewKill", RPCMode.All, Network.player, by);
The called method is the following one :
@RPC
function NewKill (dead:NetworkPlayer, killer:NetworkPlayer) {
var playersfound:int = 0;
for (var currPlayer:PlayerData in playersList.players)
{
if (currPlayer.player == dead)
{
++currPlayer.deaths;
++playersfound;
}
else if (currPlayer.player == killer)
{
++currPlayer.kills;
++playersfound;
}
if (playersfound == 2) break;
}
}
When player1 kills player2, player2 has its data in the playersList var updated. I developped a dialog which displays on the screen the playersList data. I can see that player2 has the right data, but not player1, nothing was updated in its playersList var.
With the attached debugger I saw that player1 goes into the "NewKill" method but playersList does not contain anything, but it does when I call the method to display the scores ...
So I tested something else : still with the attached debugger, I added
if (!this.transform.parent.networkView.isMine)
{
enabled = false;
return;
}
at the very beginning of the 'NewKill" method. And it goes into this "if" ... But my script is not disabled ! So I am in someone script but I don't know whose (player2 has its script enabled too), in which the playersList var containes nothing.
The comportement is symmetrical : if player2 kills player1, player1 has its data updated but player2 has no changes, so this should not be a client/server - server/client problem ...
Please help me :'( !
Answer by TheDarkVoid · Dec 03, 2012 at 10:19 PM
It would probably be more efficient to have the script stay enabled, and instead prevent parts of the script from running if the networkView.isMine is false. Also it would also be best for the server to receive that RPC and determine who gets the kill and death, then you can send the updated list to all the other players. This is the way I have it in my game and it work well and it keeps score keeping on the server-side which is better for security.
Why should the script stay enabled if it does not belong to the actual player ?
if it's not enabled it cant receive data if it's not enabled, that is the main problem i can see with your code. Only the person who sends the RPC will receive it since all other receivers are disabled.
I replaced the enable = false by a simple return, and I called this test on all interesting methods (awake, start, ongui), nothing changes on the behaviour, I still have no data in the playersList only for the New$$anonymous$$ill method :(
I don't really see any difference between sending only to the server the RPC and then it sends to all clients and doing what I am doing. I only increment ints and since RPC calls are queued, I'm not sure why I would not be safe. If you have any ideas I'm interested on them :)
It is called from a script handled by one player.
I tested one thing : I initialize my playersList in the awake method. I tested the initialization before the test to know if the networkView is $$anonymous$$e, and oh magic it is working ! So I tested it with my first code (to disable the script if it is not $$anonymous$$e) and it is still working. I think it is working because the script of player1 is disabled only in player2 world, so the RPC call is still sent threw the network to player1, and since the script is enabled in player1 world there is no problem ... If someone lese knows exactly why, he may explain it here as a comment :)
So the thing I still don't understand is why the RPC call sent from player2 to player1 is applied to the script of player1 in player2 world (as the debugger sees it ...). It is now working but I don't really know why ...
Follow this Question
Related Questions
RPC a lot of lags (Photon) 0 Answers
Unity3d Network - Cant See Connected Players 3 Answers
Really quick/easy question about RPCs 1 Answer
Getting rotation to work on client 1 Answer
Network RPC 1 Answer