- Home /
JS Dictionary?
Hi All, I've encountered a little problem lately. So I have a whole bunch of gameobjects and they all have the same script attached to them and there is the same int in the script but the int is different the gameobjects.
So I was trying to find a way to store all the gameobjects and their own different int in a dictionary, so for example, if I pick a int, I would find the gameobject that it is appointed to the int.
Anyway of doing this? I would really like to know. Thankyou. (I am writing in javascript)
Answer by fafase · Jul 12, 2015 at 11:44 AM
Dictionary is one way to go if the int values are not all consecutive. In this case, considering it goes from 0 to n and never skip a value in between, you can just use an array.
You could also use an ordered list, that is if you consider iterating through the collection based on the int value.
But if you are about to get any value, even negative, and you need to find based on the int value, then dictionary is the one.
var dict = new Dictionary.<int, GameObject>();
var objs = FindObjectsOfType(ScriptName);
foreach(var item in objs){
dict.Add(item.intValue,item.gameObject);
}
and use as :
if(dict.Contains(10)){
var obj : GameObject = dict[10];
}
I'm getting a new error:
ArgumentException: An element with the same key already exists in the dictionary
Here is my code:
import System.Collections.Generic;
private var openList = new Dictionary.<int, GameObject>();
private var Nodes : GameObject[];
function Awake()
{
Nodes = GameObject.FindGameObjectsWithTag("Node");
}
function FixedUpdate()
{
PickAnd$$anonymous$$ove();
}
function PickAnd$$anonymous$$ove()
{
for(Node in Nodes)
{
var nodeInfo : NodeInfo = Node.GetComponent(NodeInfo);
if(nodeInfo.Opened == true)
{
openList.Add(nodeInfo.F, Node);
}
}
}
What am I doing wrong?
You cannot have twice the same key in a dictionary. The point of it is that if you provide the key, you get the value, but if there is twice the same key, the compiler cannot know which you meant. So this is forbidden.
You said you want to provide the int value and get the matching object, that is not possible if you have many times the same values.
Also what you are doing there is not appropriate. Fetching component in an update, particularly for a collection is not so good. You should ins$$anonymous$$d have a collection of the script already, found once in Start. If you were to create new objects later on, add them to the existing list.
For your original issue, it is important to know if the F value is consecutive, that is it goes 0,1,2,3,... so that you can use a array of array for instance with the value a index of the main array.
So the F value can't be the same? And I don't quite understand what do you mean by "array of array of instance with the value a index of the main array." Anyway, thankyou for your time!
If you want to use dictionary, F values have to all be unique. This is the issue you experienced. Dictionaries have to have unique keys.
If you need to have duplicate of F values, then a solution would be an array of array. Search for jagged array.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Changing Custom Object Variable 1 Answer
Combine Children Dictionary in place of Hashtable? 2 Answers
Nested Dictionary Change Value Problem 3 Answers