- Home /
Trouble syncing health in network
Hi I am trying to create a network game. I have some trouble syncing the health of structures etc - newly logged on users don't see the same as the server and other currently online players. Below is an example. The server player logged in and did some damage to the cubes. Then the Player 1 logg in and the cubes health is set to the default value, and NOT to the current health. BUT if either of the players do more damage, both see the same (and correct number).
I attaches this script to the cubes, and configure the amount of default health in the inspector.
#pragma strict
var HP:float;
private var localHP:float;
var explosionPrefab:GameObject;
private var prefabPosition:Transform;
function Start () {
//Debug.Log("HP "+ HP);
//Debug.Log("LHP " + localHP);
}
function takeDamage(damageVal:float){
Debug.Log(gameObject.name + " is taking "+ damageVal +" damage!");
HP -= damageVal;
if(HP <= 0){
Network.Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
if(networkView.isMine) {
networkView.RPC("sendHPToServer", RPCMode.AllBuffered, HP, transform.position);
}
}
private var namePlatePos : Vector3;
var namePlate:GUIStyle;
var showHP:boolean;
function OnGUI(){
// Place the name plate where the gameObject is
if(showHP){
namePlatePos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
GUI.BeginGroup(Rect((namePlatePos.x-50), (Screen.height - namePlatePos.y), 100, 50));
GUI.Label(Rect(0, 0, 100, 50), HP + "%",namePlate);
GUI.EndGroup();
}
}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
if (stream.isWriting) {
var healthC : float = HP;
stream.Serialize(healthC);
} else {
var healthZ : float = 0;
stream.Serialize(healthZ);
HP = healthZ;
}
}
@RPC
function sendHPToServer(newHP:float, location:Vector3) {
HP = newHP;
//transform.position = location;
}
How can I make shure newly logged on players see the current damage done to the cubes. I use on Network View to track the transforms of the cube. Can I use one to track variables?
I use this code on the Grenade object:
function explode() {
// Applies an explosion force to all nearby rigidbodies
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (hit && hit.rigidbody)
hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
hit.SendMessage("takeDamage",damage);
}
Destroy(gameObject);
Instantiate(explosionPrefab, transform.position, Quaternion.identity);
}
Please help me with this :)
Just an opinion, I am not sure, but I think you don't need to use both RPC and serialization for HP. I think, calling sendHPToServer() on all buffered, as you did, it's enough to update and show the new HP value on GUI for all already connected and new connected players.
Don't know much about Unitys networking, but I only see a RPC call when the Cube takes damage, so it makes sense why it's updated for players when the Cube get's hit.
Thing is, where is the code that would actually send the HP info the player that just joined the server ? Not sure how your game is set up, but usually you'd send all that info to the Client / Player when he joins and when the info is of any value to the client.
(I'm just guessing since I have no idea how those Unity servers work and if they should send this kind of info automatically to the client or if you're supposed to program that part..)
Answer by terjesorheim · Dec 30, 2013 at 07:35 PM
I removed the OnSerializeNetworkView(), but still the correct values don't show for the new players.
I was thinking the same thing, ShadoX. But I can't figure out what code to run at startup that sets the HP to the buffered value. I tought Unity it self buffered and remembered network objects.
I updated the players name plates, that works just fine.
Need more tips? Has anyone tried this before?
Next up is to track each players HP, so I have to make this work
UPDATED: It appears that the code I use is correct. The trouble was that my network lobby lost some variables (and probably other stuff) when I loaded the level. The lobby and the level is now in the same scene, and everything work as intended!**
Your answer
Follow this Question
Related Questions
Load variable from server 0 Answers
How to do health thru network 1 Answer
Strings through a network? 1 Answer
Specific enemy variable affects all enemies 2 Answers
Instantiate health bar with networking 2 Answers