How to change variables for all players?
Options
GiViZed
✭
Script, which destroys the object after certain period of time:
I tried this, but it does`n work:
public class TimedObjectDestructor : MonoBehaviour { public float timeOut = 1.0f; public bool detachChildren = false; // Use this for initialization void Update() { if(timeOut >= 0) { timeOut -= Time.deltaTime; } else DestroyNow(); } void DestroyNow () { if (detachChildren) { // detach the children before destroying if specified transform.DetachChildren (); } // destory the game Object Destroy(gameObject); } }I need to increase the value of timeOut variable for all players, so that the object stays in the scene for longer
I tried this, but it does`n work:
[PunRPC] void pickup() { if(Input.GetKeyDown (KeyCode.E)) { int x = Screen.width / 2; int y = Screen.height / 2; Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y)); RaycastHit hit; if(Physics.Raycast(ray, out hit)) { Pickupable p = hit.collider.GetComponent<Pickupable>(); if(p != null && hit.distance < 5) { carrying = true; carriedObject = p.gameObject; p.gameObject.GetComponent<Rigidbody>().useGravity = false; gameObject.GetComponent<PhotonView>().RPC("addTime", PhotonTargets.All, p.gameObject.GetComponent<TimedObjectDestructor>().timeOut); } } } } [PunRPC] void addTime(float ob) { ob += 20f; }
0
Comments
-
All you're doing is changing a local variable inside of addtime... maybe you mean this.ob += ob?0
-
Can you tell me where exactly should i write this line please?TreeFortress said:All you're doing is changing a local variable inside of addtime... maybe you mean this.ob += ob?
0 -
Hi GiViZed,
I guess TimedObjectDestructor is attached to a networked object. How do you instantiate this object? Best way is to use PhotonNetwork.Instantiate(...) for this and only allow MasterClient to reduce its lifetime and finally destroy it by using PhotonNetwork.Destroy(...). Therefore add an PhotonView component to it and add condition isMasterClient at the beginning of the Update() function. If you want the lifetime to be accessible by non-MasterClients think about implementing OnPhotonSerializeView(...) function additionally.
When it comes to increase the time, clients can call the RPC and choose PhotonTargets.MasterClient as target, so that he can increase the time. Therefore your addTime(...) function needs to set the correct value, which is notob
because this is your parameter and the value you want to add to the remaining time.0 -
Thanks a lot for your help Christian_Simon, i changed my code to:
public class TimedObjectDestructorNetwork : Photon.MonoBehaviour { public float timeOut = 1.0f; public bool detachChildren = false; // Use this for initialization void Update() { if(PhotonNetwork.isMasterClient) { if(timeOut >= 0) { timeOut -= Time.deltaTime; } else DestroyNow(); } } void DestroyNow () { if (detachChildren) { transform.DetachChildren (); } PhotonNetwork.Destroy(gameObject); } }
But i don`t know what code i need in OnPhotonSerializeView(...) function, and also, how to send RPC to master client to increase the lifetime on a specific object other player is holding.0 -
Hi,
I would suggest you glance through the basic tutorial available in our doc and package:
https://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-networking#health
https://doc.photonengine.com/en-us/pun/current/tutorials/pun-basics-tutorial/player-prefab#health
Basically, in that tutorial, you'll learn how to fire a laser beam at the opponent and it will take health graudally as the laser is crossing the player, so in essence this is the same logic as your lifetime.
You'll see how to use OnPhotonSerializeView as well, which resumes to sending data when you are the owner, and receiving this data when you are not.
Bye,
Jean-1 -
jean
xuclame el ñosco0