Search Unity

ArgumentException: An item with the same key has already been added. Key: Point

Discussion in 'Scripting' started by DragOOneD999, May 8, 2019.

  1. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    I have this error with the below script and I have no idea how to fix it
    PelletScript.CreateNodes () (at Assets/Scripts/PelletScript.cs:77)
    PelletScript.Start () (at Assets/Scripts/PelletScript.cs:37)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PelletScript : MonoBehaviour
    6. {
    7.     private static Dictionary<Point, Node> nodes;
    8.     private Point neighboursPos;
    9.  
    10.     public List<Node> neighbours = new List<Node>();
    11.     public List<Vector2> validDirections;
    12.  
    13.     public static PelletScript Instance { get; set; }
    14.  
    15.     public Point GridPosition { get; private set; }
    16.  
    17.     public Vector2 WorldPosition
    18.     {
    19.         get
    20.         {
    21.             return new Vector2(transform.position.x + (GetComponent<SpriteRenderer>().bounds.size.x / 2), transform.position.y - (GetComponent<SpriteRenderer>().bounds.size.y / 2));
    22.         }
    23.     }
    24.  
    25.     public bool Walkable { get; set; }
    26.  
    27.     private void Awake()
    28.     {
    29.         Instance = this;
    30.     }
    31.  
    32.     // Start is called before the first frame update
    33.     void Start()
    34.     {
    35.         if (nodes == null)
    36.         {
    37.             CreateNodes();
    38.         }
    39.  
    40.         PelletScript currentPellet = this;
    41.  
    42.         for (int x = -1; x <= 1; x++)
    43.         {
    44.             for (int y = -1; y <= 1; y++)
    45.             {
    46.                 //Debug.Log(x + ";" + y);
    47.                 neighboursPos = new Point(currentPellet.GridPosition.X - x, currentPellet.GridPosition.Y - y);
    48.                 //Debug.Log(neighboursPos.X + " " + neighboursPos.Y);
    49.                 if (MapManager.instance.InBounds(neighboursPos) && MapManager.instance.AllPellets[neighboursPos].Walkable && neighboursPos != currentPellet.GridPosition)
    50.                 {
    51.                     Node neighbour = nodes[neighboursPos];
    52.                     neighbours.Add(neighbour);
    53.                 }
    54.             }
    55.         }
    56.  
    57.         validDirections = new List<Vector2>(neighbours.Count);
    58.  
    59.         for (int i = 0; i < neighbours.Count; i++)
    60.         {
    61.             Node currentNeighbour = neighbours[i];
    62.             Vector3 currNPos = new Vector3(currentNeighbour.GridPosition.X, currentNeighbour.GridPosition.Y, 0);
    63.             Vector2 tempVector = currNPos - transform.localPosition;
    64.  
    65.             validDirections[i] = tempVector.normalized;
    66.         }
    67.     }
    68.  
    69.     private static void CreateNodes()
    70.     {
    71.         //Instantiates the dictionary
    72.         nodes = new Dictionary<Point, Node>();
    73.  
    74.         //Run through all pellets in the game
    75.         foreach (PelletScript pellet in MapManager.instance.AllPellets.Values)
    76.         {
    77.             nodes.Add(pellet.GridPosition, new Node(pellet));
    78.         }
    79.     }
    80.  
    81.     public void Setup(Point gridPos, Vector3 worldPos)
    82.     {
    83.         Walkable = true;
    84.         this.GridPosition = GridPosition;
    85.         transform.position = worldPos;
    86.         MapManager.instance.AllPellets.Add(gridPos, this);
    87.     }
    88. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It means that you already have added a node with the same grid Position. Since grid Position is the key which you use to retrieve dict items, adding the new key value pair would no longer guarantee that all keys are unique, and you wouldn't be able to to retrieve all values. Hence the error.

    -ch
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    I don't see where you're setting up the
    Point
    values in each
    GridPosition
    , or even the definition of
    Point
    to see how it would be compared. But let's assume that all that is done and compares can be done.

    A
    Dictionary<KeyType, ValueType>
    can only have one
    ValueType
    element at a given
    KeyType
    place. If you have an entry at
    Point(4, 3)
    , you cannot add another entry at
    Point(4, 3)
    without removing the old one first. If you attempt to do so, the
    ArgumentException
    is thrown.
     
  4. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    this are the point and node scripts

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public struct Point
    4. {
    5.     public int X { get; set; }
    6.  
    7.     public int Y { get; set; }
    8.  
    9.     public Point (int x, int y)
    10.     {
    11.         this.X = x;
    12.         this.Y = y;
    13.     }
    14.  
    15.     public static bool operator == (Point first, Point second)
    16.     {
    17.         return first.X == second.X && first.Y == second.Y;
    18.     }
    19.  
    20.     public static bool operator != (Point first, Point second)
    21.     {
    22.         return first.X != second.X || first.Y != second.Y;
    23.     }
    24. }
    25.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Node
    6. {
    7.     public Point GridPosition { get; private set; }
    8.  
    9.     public PelletScript PelletRef { get; private set; }
    10.  
    11.     public Node(PelletScript pelletRef)
    12.     {
    13.         this.PelletRef = pelletRef;
    14.         this.GridPosition = pelletRef.GridPosition;
    15.     }
    16. }
    17.