- Home /
Trading card game uniqe id generation
So in making a trading card game, I need to make sure every trading card has a unique ID, but I need the first character needs to identify what type of card it is, would it be best practice to use an ID generator and concatenate the identifier?
p.s. I low-key want the ID's to be ordered so that if you got all the cards you could kind of see the order each card was released but I feel like that's a bad idea just wanted to share this thought.
Answer by MickyX · May 03 at 03:36 PM
I personally don't see whats wrong with adding some identifier information into the identifier if you want to do that
I guess you want something like CardID-Day-Month-Year-UniqueID
I would use GUIDs to do this
So you could have CardID: 1 - Lets you know which card it is Day - Month - Year let you know when it was released GUID is the unique reference for that particular card
In code this could look like
public void GenerateNewCards(int numOfCards, string cardID, string dateInfo)
{
for (int i = 0; i < numOfCards; i++)
{
string newCardReference = string.Format("{0}-{1}-{2}", cardID,dateInfo, System.Guid.NewGuid().ToString());
Debug.Log(newCardReference);
}
}
I ran that for 5 cards and got
GenerateNewCards(5,"1","01-12-2022");
1-01-12-2022-54ab56cb-8611-4100-a4d4-adeca2c3ee85
1-01-12-2022-b5dd3c42-4a88-4b4e-bad5-8ae250a4660e
1-01-12-2022-009f4831-a929-455c-9a0b-07ef9266f005
1-01-12-2022-30b0da50-294d-4801-8637-fac368cddb34
1-01-12-2022-d9b45f61-9ab3-45dd-b038-96898a578dfe
This should create you results that are unique and you can read the first bit to know what the card is and when it was released. You can then parse that string later to get that information out
Answer by Monsoonexe · May 04 at 12:31 AM
In my games, I need a persistent, unique identifier when saving game state so that it can be loaded again on another play session. I accomplish this with the Guid
class: https://docs.microsoft.com/en-us/dotnet/api/system.guid?view=net-6.0
E.g. string saveID = Guid.NewGuid().ToString();
If you need to determine an order to it, just add some arbitrary letters to it: string cardID = "a" + Guid.NewGuid().ToString()
. "a"
can be replaced with an array entry as well. Consider: string[] prefixes = new string[] { "a", "b", "c", ... "aa", "ab", "ac"} ;