RPC not triggered on all clients
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
RPC not triggered on all clients
DuaneDog
2014-05-23 18:55:47
I've got a very simple RPC call that is working fine with one of my players but fails to call for one of the others.
The basic syntax is like this...
public void CastSpell(SpellDefinition spell)
{
PhotonView.RPC( "CastRPCSpell", PhotonTargets.All, spell.Name );
}
[RPC]
public void CastRPCSpell(string SpellName)
{
SpellDefinition spell = SpellDefinition.FindByName (SpellName);
}
It works fine when one of the players casts the spell and you can see the animation on both systems. Meaning the RPC is most certainly called. However with the other player, the spell casts locally just fine, but the RPC is never called on the other client so you see nothing there. It has nothing to do with the build. Meaning that I get the same behavior in the editor as I do with the runtime. It is always the player that first connects that gets the RPC and you can see the spell of the opponent being cast. The 2nd player that joins appears not to get the call from the RPC.
I'm hoping I'm just missing something simple.
Many thanks for the help.
Comments
Still struggling... Amazing how something so simply can be so frustrating and just not much to go on.
This is the primary error...
PhotonView with ID 2001 has no method "CastRPCSpell" marked with the RPC or @RPC(JS) property! Args: String
The thing is, I ID 2001 PhotonView DOES have CastRPCSpell on my main object in my scene that I have already configured with the photonview. The problem appears related to another PhotonView that does not have the CastRPCSpell stepping over or conflicting with the photonview in my scene.
Maybe a bug in the Photon system in which is can't see the Photonviews that are already in the scene so it starts setting view #'s that could conflict?
I must reiterate that I have the two characters already in the scene, wired extensively with property bindings and interface elements that make it very undesirable to initialize them at runtime. Therefore I have a Photonview on each of these characters. I end up down a rabbit hole that it seems unless I initialize one photonview dynamically, I can't get the ownerid of the player, therefore I can't set the ownerid of the PhotonView I need to set on my character. But when I inialize a prefab, it appears it can then generate a viewid that conflicts with the one in the scene.
So I expect I need to initialize a Photonview component on a gameobject in my scene... but I can't figure out how to just simply do that. Looked through sample code... Even bought the little space multiplayer $10 asset on the store. But all of these seem to initialize a complete new object from scratch and never do I see a simple example of how to add a Photonview to an existing gameobject you have in the scene. I'm sure I'm missing something but i'm a bit frazzled from about 36 hours of trying to figure this out.
Whew... I've been able to solve the immediate problem.
This was the primary code I used:
PhotonView oPhotonView = GameObject.Find ("Mage").AddComponent<PhotonView>();
oPhotonView.observed = GameObject.Find ("Mage").GetComponent<SpellCaster> ();
oPhotonView.ownerId = 1;
PhotonView oPhotonView2 = GameObject.Find ("Necromancer").AddComponent<PhotonView> ();
oPhotonView2.observed = GameObject.Find ("Necromancer").GetComponent<SpellCaster> ();
oPhotonView2.ownerId = 2;
The above run code runs on both systems. It adds the view on the fly, sets up the owner and what I need to call the RPC. Nice to see that I can actually do the basics.
One issue... I can't find a way to set the observe option for the Photoview using AddComponent or setting a property after. It appears for the RPC it doesn't really matter. But now that I'm turning my attention to getting all the health, mana, and other items all synced up having observe option defaulted to 'off' is going to be a problem. So what's the trick to make that happen?
Finally got it... Last line of code I needed to get the dynamic PhotonView built was...
oPhotonView.synchronization = ViewSynchronization.UnreliableOnChange;
Great to read you got it working.
Many thanks... I may need to start a new topic but I'm first posting within this topic because it maybe the way I'm initializing my photonviews using addcomponent that is causing my problem.
Using the exact code above, the OnPhotonSeralizeView never seems to fire in my spellcaster script. Could it be because of the way I'm initializing the PhotonView?
I put a debug statement in the OnPhotonSerializeView and it never shows in the console.
Below is the code I'm using:
void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
{
Debug.Log ("*********************OnPhotonSerializeView");
if( stream.isWriting == true )
{
stream.SendNext( _health );
}
else
{
_health = (float)stream.ReceiveNext();
}
}
Basically I just want to update the _health property that is sitting on my spellcaster script... the same one that has the RPC that is now working correctly.
Many thanks in advance for the support.
DuaneDog
Hi,
Attaching PhotonView dynamically does not look like good idea. As well as setting ownerId manually. Can you explain why standard approach with adding PhotonView to prefabs does not work for you?
Back to top