- Home /
Send RPCs on application quit
Hi,
I have a problem with RPC, when i try to send an RPC while i quit my application RPC are don't called, how can i fix it?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestScene : Photon.$$anonymous$$onoBehaviour {
List<string> chat$$anonymous$$essages;
int maxChat$$anonymous$$essages = 5;
void OnApplicationQuit() {
AddChat$$anonymous$$essage (PhotonNetwork.player.name + " è uscito");
}
public void AddChat$$anonymous$$essage(string m) {
photonView.RPC ("AddChat$$anonymous$$essage_RPC", PhotonTargets.AllBuffered, m);
}
[RPC]
void AddChat$$anonymous$$essage_RPC(string m) {
while(chat$$anonymous$$essages.Count >= maxChat$$anonymous$$essages) {
chat$$anonymous$$essages.RemoveAt(0);
}
chat$$anonymous$$essages.Add(m);
}
}
this is my code and in void "OnApplicationQuit" i try to call RPC but it doesn't work
Would it be smarter to do this in OnPlayerDisconnected? I am not sure if phonton has that feature though.
I tried but doesn't work.. How can i view a message to the user, like: "Are you sure to exit?"
The application might not be given enough time to send out the RPC before it gets closed, they might not be sent the instant RPC() is called. Or perhaps Photon catches OnApplicationQuit first and disconnects you before. I'm not sure what you tried but Photon does have the OnPhotonPlayerDisconnected(PhotonPlayer) message.
As for that quit message, you would have to set it up in your in-game GUI menus.
Answer by tobiass · Feb 02, 2015 at 12:30 PM
Sending of the RPC fails, because it does not get sent immediately when you call it. RPC's are sent in intervals (like other messages) by default. This allows us to aggregate some messages.
Your code creates a message that would be send in the next interval but that doesn't happen anymore (due to app quit).
PUN should call PhotonNetwork.Disconnect() in it's own OnApplicationQuit() implementation. This a) will attempt to send immediately and b) tells the server that you are closing the app, too. The connection will be closed and the others get an update, too!
You can implement:
void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
{
// AddChatMessage (otherPlayer.name + " è uscito");
}
Your answer
Follow this Question
Related Questions
Time problem with Joining to Multilayer game 1 Answer
How to get the Host's player position ? 0 Answers
How to test Unity's new networking layer in separate game instances? 0 Answers
LLAPI Multiplayer - Client FPS very low when builded. 0 Answers
Can I create a separate server and use the Unity game objects in it? 0 Answers