Search Unity

Displaying attributes of out variables

Discussion in 'Scripting' started by drhmiri, Jul 30, 2019.

  1. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    76
    I have a BadGuy class and a MonoBehavior class that references it, as follows:

    Code (CSharp):
    1. using System;
    2.  
    3. public class BadGuy : IComparable<BadGuy>
    4. {
    5.     public string Name { get; set; }
    6.     public int Power { get; set; }
    7.  
    8.     public BadGuy(string name, int power)
    9.     {
    10.         Name  = name;
    11.         Power = power;
    12.     }
    13.  
    14.     public int CompareTo(BadGuy other)
    15.     {
    16.         if (other == null)
    17.         {
    18.             return 1;
    19.         }
    20.  
    21.         return (Power - other.Power);
    22.     }
    23. }
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class Dicts : MonoBehaviour
    5. {
    6.     private void Start()
    7.     {
    8.         Dictionary<string, BadGuy> badguys_dict = new Dictionary<string, BadGuy>();
    9.  
    10.         BadGuy badguy1 = new BadGuy("Harvey",  50);
    11.         BadGuy badguy2 = new BadGuy("Joshua",  20);
    12.  
    13.         badguys_dict.Add("gangster", badguy1);
    14.         badguys_dict.Add("idiot",    badguy2);
    15.  
    16.         Debug.Log( badguys_dict["gangster"].Name );
    17.         Debug.Log( badguys_dict["stupid"].Name );
    18.         // KeyNotFoundException: The given key was not present in the dictionary.
    19.  
    20.         // To avoid getting an error if the key does not exist:
    21.         BadGuy val1 = null, val2 = null;
    22.         if ( badguys_dict.TryGetValue("gangster", out val1) )
    23.         {
    24.             Debug.Log(val1.Name);
    25.         }
    26.         if ( badguys_dict.TryGetValue("stupid", out val2) )
    27.         {
    28.             Debug.Log(val2.Name);
    29.         }
    30.     }
    31. }
    What am I missing or doing wrong that val1.Name and val2.Name are not printed?
    Is there a different way of accessing the attributes of an out variable?
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    264
    Code (CSharp):
    1.  badguys_dict.Add("gangster", badguy1);
    2.         badguys_dict.Add("idiot",    badguy2);
    3.         Debug.Log( badguys_dict["gangster"].Name );
    4.         Debug.Log( badguys_dict["stupid"].Name );
    You insert with the key "idiot", but try to access it with the key "stupid".
     
    drhmiri likes this.
  3. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    76
    I know! Only the second one should fail, which it does...
    In the first part, it fails and throws an error message; in the second part, it fails but silently.
    But why does the val1.Name does not print in the TryGetValue("gangster", out val1)?
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    264
    Exceptions stop the function from executing, so when it throws an exception on the Debug.Log line, it does not continue past that line. If you comment out the debug lines, does it still fail to print from the TryGetValue block?
     
    drhmiri likes this.
  5. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    76
    Got it! Many thanks. That was the problem. It's resolved now. Cheers