- Home /
RPC called by OnPhotonPlayerConnected doesn't work
this is my sript I use for opening doors. when new player connects, I need to tell him whether the doors are closed or open. I wanted to send rpc to close the door when anyone connects if the doors are closed, but nothing happens
using UnityEngine;
using System.Collections;
public class door_interact : Photon.MonoBehaviour {
public int speed = 4;
public string axis = "z";
public float coord1 = -10.12562f;
public float coord2 = -15.18888f;
public float coord3 = -4.210109f;
public float coord4 = 0.9528589f;
public void OnLookEnter()
{
if(Input.GetKeyUp("e"))
{
if (gameObject.transform.root.GetComponent<ship_stats>().door_locked == false)
{
gameObject.GetComponent<PhotonView>().RPC("open_close",PhotonTargets.All);
}
}
}
public void OnPhotonPlayerConnected ()
{
gameObject.GetComponent<PhotonView>().RPC("open_close_update",PhotonTargets.All);
}
[RPC]
public void open_close()
{
if(gameObject.transform.root.gameObject.GetComponent<ship_stats>().door_bridge_open == true)
{
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_left").gameObject,iTween.Hash("islocal",true,axis,coord1,"time",speed));
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_right").gameObject,iTween.Hash("islocal",true,axis,coord3,"time",speed));
gameObject.transform.root.gameObject.GetComponent<ship_stats>().door_bridge_open = false;
}
else
{
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_left").gameObject,iTween.Hash("islocal",true,axis,coord2,"time",speed));
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_right").gameObject,iTween.Hash("islocal",true,axis,coord4,"time",speed));
gameObject.transform.root.gameObject.GetComponent<ship_stats>().door_bridge_open = true;
}
}
[RPC]
public void open_close_update()
{
if(gameObject.transform.root.gameObject.GetComponent<ship_stats>().door_bridge_open == false)
{
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_left").gameObject,iTween.Hash("islocal",true,axis,coord1,"time",speed));
iTween.MoveTo(gameObject.transform.parent.gameObject.transform.parent.gameObject.transform.FindChild("door_right").gameObject,iTween.Hash("islocal",true,axis,coord3,"time",speed));
gameObject.transform.root.gameObject.GetComponent<ship_stats>().door_bridge_open = false;
}
}
}
anyone? please I cant find out whats wrong. or any alternative way?
Hey mate - have you tried a bit debugging to see if the open_close() method is being called? Chuck a Debug.Log("RPC called") into open_close before the if statements. Additionally check if you have attached a PhotonView component to the GameObject you're referring to.
when both players are connected synchronization via open_close works wonderfully. when new player connects the open_close_update function does actually happen (confirmed via Debug.Log), but it doesn't change anything on the new player script. maybe the player scripts has not been loaded yet by the time the function is called? new player is instantiated as soon as the game connects to room
Answer by Jellezilla · Jan 28, 2014 at 09:58 AM
When the second player joins, I'm assuming that: door_bridge_open == true for that very client, seeing since the only way you set it to false is with the first player by pressing e, right?
However, the second player has not received this information, if it has been set to false, while the second player still hasn't joined the game.
Whenever a new player connects it checks if it is false, which it cannot be unless it is stated so at the start of the application or he receives data about what has happened in the game before he joined.
Hope this is somewhat a help to you. If not, you should try and post it on the Exit-Games forum (Photon's own). They are usually very helpful.
Good luck.
yeah well that's what I am ai$$anonymous$$g for. the open_close function happens when player pressess e, but the open_close_update function should happen everytime someone connects. and it actually does, it just doesn't do anything on the new player part which I frankly don't understand since it's the same code in both cases... thanks for the tip
But the open_close method only checks if the "door_bridge_open" is false. Which it isn't when a player connects.
So are you trying to make sure that all changes made in the scene will be visible to the newly connected player or that the method above will open/close doors whenever a new player connects?
The latter is really easy to implement, however the one about visible changes for new players is something I was too afraid to touch upon when playing around with the Photon plugin myself.
well door_bridge_open is boolean so for the other option there is else in the if statement. it is the first one, I am trying to update all changes. well I just need to close the doors for the new player. aaah I see the error now. in the function open_close_update it actually checks it on their side I see. I'll try to rewrite it as I know what's wrong now and get back here with a result
so here is the final (working) result (the doors are closed by default)
when new player connects, I'll check which doors are different from default (open) and make an RPC call to close em.
Thanks, you helped me a lot.
void OnPhotonPlayerConnected()
{
if(gameObject.transform.root.gameObject.GetComponent<ship_stats>().airlock_in_open == true)
gameObject.GetComponent<PhotonView>().RPC("open_airlock_in",PhotonTargets.All);
}
[RPC]
public void open_airlock_in()
{
iTween.$$anonymous$$oveTo(gameObject.transform.FindChild("airlock_in").gameObject.transform.FindChild("door_left").gameObject,iTween.Hash("islocal",true,"z",door_airlock_in_left_open,"time",speed_small));
iTween.$$anonymous$$oveTo(gameObject.transform.FindChild("airlock_in").gameObject.transform.FindChild("door_right").gameObject,iTween.Hash("islocal",true,"z",door_airlock_in_right_open,"time",speed_small));
gameObject.transform.root.gameObject.GetComponent<ship_stats>().airlock_in_open = true;
}
Glad I could help :)
Remember to mark answer as correct if it suits. Take care.
Answer by toffino · Nov 02, 2014 at 09:14 PM
Don't forget to drag and drop your script (NetworkDoor) to your Photonview script in the inspector..
1- drag and drop your door object 2- drag and drop your network door script
It should send your variable correcly