- Home /
Is it possible to name a list by a string variable?
So here's a function that's supposed to produce a quest ID. Currently I'm just adding a fixed name, indexed. (temporary) The real function is remmed. At the middle, there's a list declaration, but I'd like to create a new list for each quest based on the incoming ID. Is this possible at all? The other question is: is this a good approach?
{
string questID = "QuestID_";
if (temporary == 1)
{
questID = questID+index.ToString();
}
else
{
//questID = AcquireInfoFromXML();
}
// Creating a list for a particular quest
**List<string> questID_Entries = new List<string>();**
for(int j = 0; j <= entry_Max; j++)
{
string entry = GetQuestEntries(j);
if (entry!="")
{
questID_Entries.Add(entry);
Debug.Log(questID+questID_Entries[j]);
}
}
///////////////////////////////////////////////////////////////
return questID;
}
Can you clarify what you mean about "Na$$anonymous$$g a list by a string variable"
The line you have starred creates a list of strings.
The name of the new list would be the string's value. So, the string Quest1 would create a list with the name Quest1 for its entries.
In other words, as I'm getting a particular questID, at the same time I'd like to create a list by the same ID.
Answer by roojerry · Aug 26, 2013 at 02:33 PM
You probably want to look at a Dictionary. You could use the string questID as the key and have a corresponding List for the value.
There's no corresponding list, because it's not yet created. This is the problem. :) I want to create the list first by the ID.
Store the ID's in a list/ array then once you want to add in the dictionary, do it.
List<string> idList new List<string>();
static int index;
idList.Add("Id1");
idList.Add("Id2");
var myDict = new Dictionary<string, List<GameObject>>();
// Once you got the list to go with:
myDict.Add(idList[index++], new List<GameObject>());
The static variable ensures to have only once the list id used.
Answer by perchik · Aug 26, 2013 at 03:13 PM
Here's how you could actually use Dictionary to do what you want.
Dictionary<string, List<string>> quests = new Dictionary<string, List<string>>();
quests.Add["Quest1"] = new List<string>();
quests["Quest1"].Add("whatever I'm adding to the quest list");
If the answer is correct, please mark it as the right answer.
Sorry, I don't have time to dabble with this at the moment, but thanks again.
haha, you are too busy to select a correct answer to the question, but you came back to respond. This question has been answered, please choose the answer that helped you and close this question for future reference.
And please watch the video on how this site works before posting again.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Update List<> in editor 0 Answers
How do I make a list of lists? 2 Answers
Cannot convert string to int. 1 Answer
add items to a list 1 Answer