Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Generating a unique ID for each object instance

This topic is 3316 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I wanted a quick and simple way of generating a unique string ID for each instance of a class would the following method suffice:

this.iD = this.GetType().Name.ToString() + this.GetHashCode().ToString();

Or do the resident coding gurus have a better suggestion for assigning a unique string based ID? Thanks guys.

Share this post


Link to post
Share on other sites
Advertisement
Normally I just make a static int member called ID. I initialize it as 1 with a static constructor.

I then have a static method called getID that returns an int.


public static int getID()
{
return ID++;
}



Now you can get a unique ID any time you want.

theTroll

Share this post


Link to post
Share on other sites
These GUID (as strings) are used for indentifying Quads in a resource pool of Quads.

E.G.


//=============================================
//--------- [Add Quad to Resource Pools] ------
//=============================================
// If the Dictionary associated with the current effect doesn't contain the string key (quad_ID)
if (!this.vertices_Pool[this.effect_Type].ContainsKey(quad_GUID))
{
// Add the string key (quad_ID) to the Dictionary and
// Create a new array of Vertices (maximum of 4) for the current key
this.vertices_Pool[this.effect_Type].Add(quad_GUID, new VertexPositionNormalExtra[GameConstants.QUAD_NUMBER_OF_VERTICES]);

// Add the current Quad's vertices to the Dictionary under the quad_ID key
this.vertices_Pool[this.effect_Type][quad_GUID] = quad_Current.Vertices;
}
else
{
// Add the current Quad's vertices to the Dictionary under the quad_ID key
this.vertices_Pool[this.effect_Type][quad_GUID] = quad_Current.Vertices;
}

Share this post


Link to post
Share on other sites
I think GUIDs are a bit overkill in this situation. Generating GUIDs is somewhat slowish if I recall correct, esp. when you're going to create thousands of them in a situation where an ID would suffice.

However, why exactly are you storing your quads with an ID? Wouldn't it be more efficient to just check if an object instance is already in the list? And on dictionaries, why not just calculate the hash code? The hash isn't used to store the object by, just as a way to quickly index it.

Toolmaker

Share this post


Link to post
Share on other sites
Yes I believe you are right. GUIDs might be a bit of overkill in this situation.

I guess using the static getID method would be the fastest.

However, as 10000s Quads are created, updated and deleted from various points in the Dictionary in no particular order, I must know which ones to update, delete etc.

The ID will not change for the Dictionary but when using an index of an array of Quads the index would change when Quads are deleted, this is why I use a Dictionary with IDs.

I can also easily get the number of Vertices and Indices to draw as follows with a Dictionary:


public void Draw(Camera camera)
{
foreach (string key in vertices_Pool.Keys)
{
// Check to see if there are any Quads to draw with the current key (effect)
if (vertices_Pool[key].Count != 0)
{
// Reset index and current vertex and index count to 0 for current key (effect)
index = 0;
vertex_Count = 0;
index_Count = 0;

// Check each GUID (Global Unique ID) key to see if VertexPositionNormalExtra array contains any data
foreach (string iD in vertices_Pool[key].Keys)
{
// If the VertexPositionNormalExtra array is not null
if (vertices_Pool[key][iD] != null)
{
// Copy the vertex data to the drawable vertices for this effect
vertices_Pool[key][iD].CopyTo(vertices_Drawable, vertex_Count);

// As all Quad indices are identical, don't work out indices until drawable Quads are being calculated
this.indices_Drawable[index++] = (short)(0 + vertex_Count);
this.indices_Drawable[index++] = (short)(1 + vertex_Count);
this.indices_Drawable[index++] = (short)(2 + vertex_Count);

this.indices_Drawable[index++] = (short)(1 + vertex_Count);
this.indices_Drawable[index++] = (short)(3 + vertex_Count);
this.indices_Drawable[index++] = (short)(2 + vertex_Count);

// Increase the vertex count by the number of vertices in a Quad
vertex_Count += GameConstants.QUAD_NUMBER_OF_VERTICES;

// Do the same for indices
index_Count += GameConstants.QUAD_NUMBER_OF_INDICES;
}
}

// Make sure there are Quad vertices to draw (will be 0 if all vertex arrays associated with iD are null)
if (vertex_Count != 0)
{
// If reusing Quads (E.G. BulletManager) vertex_Count should not keep increasing if maximum avaialable Quad value (MAX_BULLETS) has been reached
//Console.WriteLine(vertex_Count);
DrawQuadrangles(key, camera);
}
}
}
}




EDIT:

Something like this will do the trick?


// Globally Unique IDentifier
public static class GUID
{
private static int ID;
public static int GetUID()
{
ID++;
return ID;
}
}

Share this post


Link to post
Share on other sites
I wouldn't make a whole new class. I would add the static member and static method to the class the needs the UID. This keeps it where it is going to be used.

theTroll

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!