I have been writing all the necessary code for procedurally generated items within my game. The database mechanism that I have built supports new items at runtime, such as when a player opens a chest. Everything works fine, but I realized a fatal flaw with the way that I generate the "unique" item identifiers. I generate items using a Prefix, such as Fiery, an item type, such as Claymore, and a suffix, such as of Greater Shadow.
I generate the database Identifier using the format of:
Proc_ + Prefix + Type + Suffix + Iteration
As an actual example, the procedural item "Fiery Claymore of Greater Shadow" would then become:
Proc_FCOGS_01
All seems well right? This looks like it could work quite nicely. However, I then realized that I can easily receive identifier clashes. Or as an example: "Freezing Claymore of Greater Striking" would also be created as:
Proc_FCOGS_01
And that's where the problem arises. Because I went through all my affixes, located here and found that a few more of my chosen affixes could also lead to clashing within the database, despite the generated item not actually being the same as the one that is already entered. The routine that I am using to generate these identifiers is as follows:
public string CreateIdentifier(string prefix, string itemType, string suffix)
{
if (!String.IsNullOrEmpty(prefix) && !String.IsNullOrEmpty(itemType) && !String.IsNullOrEmpty(suffix))
{
string input = prefix + " " + itemType + " " + suffix;
string[] segments = input.Split(' ');
StringBuilder builder = new StringBuilder(segments.Length + 5);
builder.Append("Proc_");
for (int i = 0; i < segments.Length; i++)
{
builder.Append(Char.ToUpper(segments[i][0]));
}
return builder.ToString();
}
return null;
}
After all of this, I can really only see two options:
Scrap my current Unique Identifier routine and think of another one.
If a collision exists within the Database, simply iterate the generated identifier ending number by 1, check again, and if necessary, iterate the the ending number by 1 again until an unused identifier is found.
As for number 1, that's why I'm here showing you all this. I need ideas about how I can do this in a way that will result in zero clashes unless the item is actually the same thing as previously generated.
And as for number 2, I would really prefer not doing this. I feel as though having fifteen or more database entries of Proc_FGSOTE
plus the unique iteration, to be a little silly. Why have fifteen database entries of the same item? Although, I guess that would indeed be putting the iteration value to use...it's just not what I had intended it to be used for and seems like a terrible waste of resources. I really feel as though a better routine is the way to go.
Does anyone know of a better way that I can be generating unique identifiers?
Edit:
Sorry, I fogot to mention that I don't want to use the entire item name as an identifier, even if it's compressed to trim out all the spaces, mainly because the identifiers are still going to be way too long. This seems like an arbitrary limitation, I know, it's just one that I wanted to enforce so that way the Database could be loaded/parsed faster later down the road.