So you know how when you duplicate something in the Hierarchy, the new thing has a number added to the name so you can tell it apart from the original? Basically, I’m trying to do that in my code
What I want to be able to do is go into an array of enemy names, find any duplicates, and add a number to the end of their name, so you can tell each of the duplicates apart.
I’ve tried Googling this, because I figured this is something that would be pretty common, but I can’t seem to find results that are relevant to what I’m trying to do.
Any help would be appreciated as always
You could search for: find duplicates in string array. There are many… And then you could use the result and add the numbers you want.
For what purpose do you need to add to it? In an array, the index already can be used as a reference to each object. But if they are gameobjects and you just want to add a number to the gameobject name, you will want to keep track of what number you are on, add that number and then increment. Or, you could just add a number to the name when you instantiate it, if that is what you are doing.
There’s tons of ways to do this. You could create a dictionary/map and use that to store a key->value pair where the name is the key and the value is the current ‘cnt’ as you loop over the array:
string[] names = *your name list*;
var dict = new Dictionary<string, int>();
int cnt;
for(int i = 0; i < names.Length; i++)
{
if(dict.TryGetValue(names[i], out cnt)
{
cnt++;
dict[names[i]] = cnt;
names[i] += " (" + cnt + ")";
}
else
{
dict[names[i]] = 0;
}
}
Or you can use some linq to get the distinct names, and then enumerate each of those. It requires more loops, but it is a little more readable.
using System.Linq;
...
string[] names = *your name list*;
foreach(var nm in names.Distinct())
{
int cnt = 0;
for(int i = 0; i < names.Length; i++)
{
if(names[i] = nm)
{
if (cnt > 0) names[i] += " (" + cnt + ")";
cnt++;
}
}
}
Or I could think of other ways.
You basically just need to count over the list keeping track of what objects you’ve counted while doing it.
The names are part of a custom data type I made for characters participating in a battle. I am using a bunch of CSV files to load the characters into the battle array, each CSV represents a different character.
I made 3 party members and a single enemy, who I have put into the array 3 times, so that it’s a 3 on 3. But in the dialog texts of my battle system, all 3 of their names are identical, so it could become confusing very quickly which enemy you are attacking/which enemy is attacking you unless I can add numbers to the end of their names
So here is what I came up with:
for (int i = 0; i < Participants.Length; i++) {
if (Participants [i].alignment == Alignment.Enemy) {
string dupename = Participants [i].name;
int duplicates = 0;
for (int j = 0; j < Participants.Length; j++) {
if (Participants [j].name == dupename) {
if(duplicates == 0){
duplicates++;
}
else if (duplicates == 1) {
Participants [i].name = (dupename + " " + duplicates);
duplicates++;
Participants [j].name = (dupename + " " + duplicates);
}
else if (duplicates >= 2) {
duplicates++;
Participants [j].name = (dupename + " " + duplicates);
}
}
}
}
}
@lordofduct Your solution with the nested loops got me thinking in the right frame of mind for this, so I have to thank you.
I don’t know if this is the most elegant solution, but as far as I can tell, it works great