How does hashtables work?

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;
    }

Questions:
In OnJoinedRoom() where is this.props actually declared? (and why do you need to add this. to it?)
Are these scrips all attached to objects in the scene and do each of those objects have a PhotonViews?

It is declared in the top of my NetworkManager.

public Hashtable props;

And these scripts are attached to an object which is my player.

Do each of your player objects have a PhotonView component attached?

Can you give the full error message?

Firstly. Thank you for helping me.

My players each have a Photonview attached to them

Here’s the error message

I can’t see any reason why that error would be happening with the scripts and information that you are giving, perhaps it could be related to something else, or maybe I’m just missing something.

With a google search of that error, it looks like it may have something to do with how the scene is loaded. When a player joins the room, are they already in the scene or do they need to load it first? If so, do you load the scene with a simple Application.LoadLevel(“scenename”)? If so, this could probably be fixed by changing it to PhotonNetwork.LoadLevel(“scenename”)

Other possibilities: I notice that you are declaring the hashtables as simply Hashtable. I don’t know why that seems to work for you, but I have always gotten errors when just trying to use Hashtable with photon custom properties. Try to replace every instance of “Hashtable” with “ExitGames.Client.Photon.Hashtable” if my first suggestion on loading the scene doesn’t work.

Another thing that I’d like to note, but should have any impact on anything other than reducing characters typed, is that you do not have to use the keyword “this.” when referring to variables and objects declared in your script.

If nothing works, please at least report back on how your scene is loaded so that maybe we can see if there’s a problem there.

1 Like