TimeOfRespawn
How do I use Time Of Respawn
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PickupItem.cs
Copy
124 public void PunPickup(PhotonMessageInfo msgInfo)
125 {
126 // when this client's RPC gets executed, this client no longer waits for a sent pickup and can try again
127 if (msgInfo.sender.isLocal) this.SentPickup = false;
128
129
130 // In this solution, picked up items are disabled. They can't be picked up again this way, etc.
131 // You could check "active" first, if you're not interested in failed pickup-attempts.
132 if (!this.gameObject.GetActive())
133 {
134 // optional logging:
135 Debug.Log("Ignored PU RPC, cause item is inactive. " + this.gameObject + " SecondsBeforeRespawn: " + SecondsBeforeRespawn + " TimeOfRespawn: " + this.TimeOfRespawn + " respawn in future: " + (TimeOfRespawn > PhotonNetwork.time));
136 return; // makes this RPC being ignored
137 }
138
139
140 // if the RPC isn't ignored by now, this is a successful pickup. this might be "my" pickup and we should do a callback
141 this.PickupIsMine = msgInfo.sender.isLocal;
142
143 // call the method OnPickedUp(PickupItem item) if a GameObject was defined as callback target
144 if (this.OnPickedUpCall != null)
145 {
146 // you could also skip callbacks for items that are not picked up by this client by using: if (this.PickupIsMine)
147 this.OnPickedUpCall.SendMessage("OnPickedUp", this);
148 }
149
150
151 // setup a respawn (or none, if the item has to be dropped)
152 if (SecondsBeforeRespawn <= 0)
153 {
154 this.PickedUp(0.0f); // item doesn't auto-respawn. must be dropped
155 }
156 else
157 {
158 // how long it is until this item respanws, depends on the pickup time and the respawn time
159 double timeSinceRpcCall = (PhotonNetwork.time - msgInfo.timestamp);
160 double timeUntilRespawn = SecondsBeforeRespawn - timeSinceRpcCall;
161
162 //Debug.Log("msg timestamp: " + msgInfo.timestamp + " time until respawn: " + timeUntilRespawn);
163
164 if (timeUntilRespawn > 0)
165 {
166 this.PickedUp((float)timeUntilRespawn);
167 }
168 }
169 }
File name: PickupItem.cs
Copy
171 internal void PickedUp(float timeUntilRespawn)
172 {
173 // this script simply disables the GO for a while until it respawns.
174 this.gameObject.SetActive(false);
175 PickupItem.DisabledPickupItems.Add(this);
176 this.TimeOfRespawn = 0;
177
178 if (timeUntilRespawn > 0)
179 {
180 this.TimeOfRespawn = PhotonNetwork.time + timeUntilRespawn;
181 Invoke("PunRespawn", timeUntilRespawn);
182 }
183 }
File name: PickupItem.cs
Copy
195 internal void PunRespawn()
196 {
197 #if DEBUG
198 // debugging: in some cases, the respawn is "late". it's unclear why! just be aware of this.
199 double timeDiffToRespawnTime = PhotonNetwork.time - this.TimeOfRespawn;
200 if (timeDiffToRespawnTime > 0.1f) Debug.LogWarning("Spawn time is wrong by: " + timeDiffToRespawnTime + " (this is not an error. you just need to be aware of this.)");
201 #endif
202
203
204 // if this is called from another thread, we might want to do this in OnEnable() instead of here (depends on Invoke's implementation)
205 PickupItem.DisabledPickupItems.Remove(this);
206 this.TimeOfRespawn = 0;
207 this.PickupIsMine = false;
208
209 if (this.gameObject != null)
210 {
211 this.gameObject.SetActive(true);
212 }
213 }
File name: PickupItemSyncer.cs
Copy
87 private void SendPickedUpItems(PhotonPlayer targtePlayer)
88 {
89 if (targtePlayer == null)
90 {
91 Debug.LogWarning("Cant send PickupItem spawn times to unknown targetPlayer.");
92 return;
93 }
94
95 double now = PhotonNetwork.time;
96 double soon = now + TimeDeltaToIgnore;
97
98
99 PickupItem[] items = new PickupItem[PickupItem.DisabledPickupItems.Count];
100 PickupItem.DisabledPickupItems.CopyTo(items);
101
102 List
103 for (int i = 0; i < items.Length; i++)
104 {
105 PickupItem pi = items[i];
106 if (pi.SecondsBeforeRespawn <= 0)
107 {
108 valuesToSend.Add(pi.ViewID);
109 valuesToSend.Add((float)0.0f);
110 }
111 else
112 {
113 double timeUntilRespawn = pi.TimeOfRespawn - PhotonNetwork.time;
114 if (pi.TimeOfRespawn > soon)
115 {
116 // the respawn of this item is not "immediately", so we include it in the message "these items are not active" for the new player
117 Debug.Log(pi.ViewID + " respawn: " + pi.TimeOfRespawn + " timeUntilRespawn: " + timeUntilRespawn + " (now: " + PhotonNetwork.time + ")");
118 valuesToSend.Add(pi.ViewID);
119 valuesToSend.Add((float)timeUntilRespawn);
120 }
121 }
122 }
123
124 Debug.Log("Sent count: " + valuesToSend.Count + " now: " + now);
125 this.photonView.RPC("PickupItemInit", targtePlayer, PhotonNetwork.time, valuesToSend.ToArray());
126 }
File name: PickupItemSyncer.cs
Copy
130 public void PickupItemInit(double timeBase, float[] inactivePickupsAndTimes)
131 {
132 this.IsWaitingForPickupInit = false;
133
134 // if there are no inactive pickups, the sender will send a list of 0 items. this is not a problem...
135 for (int i = 0; i < inactivePickupsAndTimes.Length / 2; i++)
136 {
137 int arrayIndex = i*2;
138 int viewIdOfPickup = (int)inactivePickupsAndTimes[arrayIndex];
139 float timeUntilRespawnBasedOnTimeBase = inactivePickupsAndTimes[arrayIndex + 1];
140
141
142 PhotonView view = PhotonView.Find(viewIdOfPickup);
143 PickupItem pi = view.GetComponent
144
145 if (timeUntilRespawnBasedOnTimeBase <= 0)
146 {
147 pi.PickedUp(0.0f);
148 }
149 else
150 {
151 double timeOfRespawn = timeUntilRespawnBasedOnTimeBase + timeBase;
152
153 Debug.Log(view.viewID + " respawn: " + timeOfRespawn + " timeUntilRespawnBasedOnTimeBase:" + timeUntilRespawnBasedOnTimeBase + " SecondsBeforeRespawn: " + pi.SecondsBeforeRespawn);
154 double timeBeforeRespawn = timeOfRespawn - PhotonNetwork.time;
155 if (timeUntilRespawnBasedOnTimeBase <= 0)
156 {
157 timeBeforeRespawn = 0.0f;
158 }
159
160 pi.PickedUp((float) timeBeforeRespawn);
161 }
162 }
163 }
TimeOfRespawn 132 lượt xem
Gõ tìm kiếm nhanh...