Reputation: 1
I would like to dynamically create a list.
Currently I have the user entering a name that they want. Then I would like the name that they have submitted to be the name of the list.
For example:
The user entered "Collection1", then I would like to create a list dynamically on another script like the following:
List<int> Collection1 = new List<int>();
Currently I'm stuck on how to do this, is there a way to create the list or rename the list for example from tempList
to Collection1
?
Abit of background, so currently im trying to create a card game. Where users are able to put the selected cards into a deck. I would like to give the user an option to name the decks. So in order to differentiate between multiple decks (which i use list to complete), i need to have a different name for the list. Preferably i would like to let the users insert the name of the deck. So in this case, i would need to be able to rename/change/ create a list that is based on their inserted deck name. So if the user wants to name their deck as collection1, i would like to have the list named as collection1List.
There is also a possibility that the user (as in the player) will create more than one deck of cards. So it will not be suitable to create a predefined list.
Upvotes: 0
Views: 358
Reputation: 90862
is there a way to create the list or rename the list for example from tempList to Collection1?
NO!
What would be the use case for this? The user doesn't write/read/care about the code ... you as a developer are ... after that your code is compiled and you don't just extend the compiled code afterwards on runtime ;)
It sounds like what you want to use is a Dictionary<string, List<int>>
so you can address different Lists via a string
key name like e.g.
private Dictionary<string, List<int>> _lists = new Dictionary<string, List<int>>();
// list is optional => if you already have a list you want to register pass it in
// if only a string is passed in a new list is automatically created
public void AddList(string keyName, List<int> list = null)
{
if(_lists.ContainsKey(keyName))
{
Debug.LogError($"Already contains a list with key name {keyName}", this);
return;
}
if(list == null) list = new List<int>();
_lists.Add(keyName, list);
}
public List<int> GetList(string keyName)
{
if(!_lists.ContainsKey(keyName))
{
Debug.LogError($"Does not contain a list with key name {keyName}", this);
return null;
}
return _lists[keyName];
}
// or alternatively
public bool TryGetList(string keyName, out List<int> list)
{
return _lists.TryGetValue(keyName);
}
public void RemoveList(string keyName)
{
if(!_lists.ContainsKey(keyName))
{
Debug.LogError($"Does not contain a list with key name {keyName}", this);
return null;
}
_lists.Remove(keyName);
}
Now IF by "User" you actually mean a developer using your script in Unity it can get a bit more complex but you could do something like
[Serializable]
public class ListEntry
{
public string Name;
public List<int> Values;
}
// Edit via the Inspector in Unity
public ListEntry[] lists;
then you can simply add and name your lists via the Inspector.
I would then still use the before code (or something similar) on runtime and simply create the dictionary once using the values from the Inspector e.g.
private void Awake()
{
foreach(var list in lists)
{
_lists.Add(list.Name, list.Values);
}
}
And of course you then can/should implement something that makes sure that all list names are unique. Either in OnValidate
or in a custom editor script .. but I think that is a bit over the scope of this question ;)
Upvotes: 2
Reputation: 26450
Variable names, have no value in your code. They are something like placeholders of real values that are referenced around your code. Since there is no use case for what you ask, the answer is no.
If you want to create/access a list based on a key, then you could create a dictionary holding lists.
Dictionary<string,List<int>> lists = new Dictionary<string,List<int>>();
Then, when the new user enters
lists.Add("username", new List<int>);
// or if you want to use and existing
lists.Add("username", existingIntList);
And access like this:
// i.e.
foreach (int userItem in lists["username"]) {
// etc
}
Upvotes: 0