- Home /
Dictionary Contains Key (TKey)
It works, but I wanted to make sure, if there is a better way to do, I'm still learning :)
thanks.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.XR.WSA;
public class ElementData
{
public int test;
public ElementData(int e)
{
test = e;
}
}
public class Test : MonoBehaviour
{
privat int complex;
public int id ; // only for tested
public Dictionary<int,ElementData> testDic = new Dictionary<int, ElementData>()
void Start()
{
testDic.Add(1,new ElementData(600));
testDic.Add(2,new ElementData(700));
testDic.Add(3,new ElementData(800));
testDic.Add(4,new ElementData(900));
testDic.Add(5,new ElementData(1000));
}
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
complex = Random.Range(6,15);
if (!testDic.ContainsKey(complex))
{
CreatDictionar(complex);
} else {
complex = Random.Range(100,200);
CreatDictionar(complex);
}
}
}
private void CreatDictionar(int n)
{
testDic.Add(n, new ElementData(id));
}
}
Better way to do what? What is the point/goal of your code? What is the question?
I want to make one list of all objects representing a character, and then check the parameters of these characters.
What are the disadvantages of dictonary, and whether it is a good use for dicsonary
Answer by TrustBird · Sep 26, 2020 at 09:04 AM
I'm trying to make a strategy game, where in dictonary I keep all the parameters of the character, there should be many of them, but they are only representations of the character as an icon and a number.
this gives me access to all characters from one place but i need to know the dictonary key
this method seems quick to me, but less object-oriented (I don't know if I'm right)
Is this approach ok or it is better to do it differently. I don't know if I explained it well enough