Hello.
I’m on my day 3 (about 27th hour) with figuring out hashtables, and how to pull something out of them, from an individual player.
I’m making a game with PhotonNetwork, and I need to add kills and deaths to a scoreboard.
This is the 3 files where I use the hashtable. What am I doing wrong? I get the error that my PhotonView ID doesn’t exist, but I doubt that’s the only problem with my code. I will be grateful if someone would take the time and patience to read this through. I’m sincerely stuck.
Networkmanager.cs
void OnJoinedRoom() {
connecting = false;
Debug.Log ("OnJoinedRoom");
SpawnMyPlayer ();
this.props = new Hashtable();
this.props.Add("Kills", 0);
this.props.Add("Deaths", 0);
if ((int)this.props["Kills"] == 0 || (int)this.props["Deaths"] == 0) {
this.props["KD"] = 0;
}else {
this.props.Add("KD", ((int)this.props["Kills"] / (int)this.props["Deaths"]));
}
PhotonNetwork.player.SetCustomProperties(this.props);
//ResetScores(); Is it needed?
}
Health.cs
[RPC]
public void TakeDamage(float amt, int shooterID, string shooterName) {
currentHitPoints -= amt;
if (currentHitPoints <= 0) {
if(photonView.isMine) {
//photonView.RPC("AddChatMessage_RPC", PhotonTargets.All, shooterName + " killed " + gameObject.name + "!");
//PhotonNetwork.SetPlayerCustomProperties(Kills);
Hashtable props = PhotonNetwork.player.customProperties;
props["Deaths"] = (int)props["Deaths"] + 1;
PhotonNetwork.SetPlayerCustomProperties(props);
Debug.Log(shooterName);
Debug.Log(amt);
Debug.Log(shooterID);
/*foreach(PhotonPlayer player in PhotonNetwork.playerList) {
if(PhotonNetwork.player.ID == shooterID) {
Hashtable old_props = player.customProperties;
props["Kills"] = (int)old_props["Kills"] + 1;
player.SetCustomProperties( props );
}
}*/
}
Die();
if (GetComponent<PhotonView>().isMine) {
guiScript gs = FindObjectOfType<guiScript>();
gs.isRespawning = true;
}
}
}
Playershooting.cs
void Fire() {
if(weaponData==null) {
weaponData = gameObject.GetComponentInChildren<WeaponData>();
if(weaponData==null) {
Debug.LogError("Did not find any WeaponData in our children!");
return;
}
}
if(cooldown > 0) {
return;
}
Debug.Log ("Firing our gun!");
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject(ray, out hitPoint);
if(hitTransform != null) {
Debug.Log ("We hit: " + hitTransform.name);
Health h = hitTransform.GetComponent<Health>();
while(h == null && hitTransform.parent) {
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health>();
}
// Once we reach here, hitTransform may not be the hitTransform we started with!
if(h != null) {
// This next line is the equivalent of calling:
// h.TakeDamage( damage );
PhotonView pv = h.GetComponent<PhotonView>();
if(pv==null) {
Debug.LogError("Freak out!");
}
else {
int shooterID = PhotonNetwork.player.ID;
shooterName = PhotonNetwork.player.name;
h.GetComponent<PhotonView>().RPC ("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage, shooterID, shooterName);
}
}
if(fxManager != null) {
DoGunFX(hitPoint);
}
}
else {
// We didn't hit anything (except empty space), but let's do a visual FX anyway
if(fxManager != null) {
hitPoint = Camera.main.transform.position + (Camera.main.transform.forward*100f);
DoGunFX(hitPoint);
}
}
cooldown = weaponData.fireRate;
}