Sign in to follow this  
ankhd

Creating a unique id for each unit

Recommended Posts

Hi all.
I need a unique Id value for each unit I create, I was thinking of doing this.
In my unit class I have a static dword count when I call the constructor I add 1 to this value would that be a good way to make this id, or should I look in to other way to make this value like maybe the memory address of the object

Share this post


Link to post
Share on other sites
What do you plan to do with this id? For example, if you want to use this to identify units in a multiplayer game what you would want or need to do to generate an id would be different than a single player game.

Share this post


Link to post
Share on other sites
[quote name='ankhd' timestamp='1302838489' post='4798645']
Hi all.
I need a unique Id value for each unit I create, I was thinking of doing this.
In my unit class I have a static dword count when I call the constructor I add 1 to this value would that be a good way to make this id, or should I look in to other way to make this value like maybe the memory address of the object
[/quote]

If it's just a single-player game, keep it simple and go with your DWORD idea. Memory addresses would become a problem if you started saving and loading units.

Share this post


Link to post
Share on other sites
I will need two Id 's one for uint ID and a Mplayer ID.

I used DPLay(not going that way this time) Last Time and it made the players ID's what should one do in that case and yes I will need to save the units as well but I may be able to recreate the ID's

I will be only saveing unit states

Share this post


Link to post
Share on other sites
Definately go with a const dword id concept, rather than toying with memory addresses and I suggest you consider saving the ids, instead of re-generating them - it just makes things easier and avoids problems that you may encounter if trying to recreate entities with their ids in a specific order. It'd also allow you to store references to other entities via their ids, rather than pointer handles or another representation that'd have to be re-evaluated upon loading the game.

Share this post


Link to post
Share on other sites
[quote name='yewbie' timestamp='1302878582' post='4798794']
If your storing them in a vector you could just use its position in the vector as its ID number.
[/quote]

Not a good idea, if you have 5 units and the third down the vector is destroyed, the IDs 3 and 4 would change, an ID should not change during the lifetime of the entity it identifies, also, ID 4 would no longer be valid since its left outside the vector range.


The static uint idea is good, as for the multiplayer, this depends on the architecture being used, it its server / client, every unit creation should be authorized or at least validated by the server, so the server should be the one assigning ids by order of creation notification arrival, if it is peer to peer, it gets more tricky, you could assign one of the peers the title of host and give him the responsability of assigning IDs...

Share this post


Link to post
Share on other sites
[quote name='NEXUSKill' timestamp='1302881053' post='4798805']
[quote name='yewbie' timestamp='1302878582' post='4798794']
If your storing them in a vector you could just use its position in the vector as its ID number.
[/quote]

Not a good idea, if you have 5 units and the third down the vector is destroyed, the IDs 3 and 4 would change, an ID should not change during the lifetime of the entity it identifies, also, ID 4 would no longer be valid since its left outside the vector range.


The static uint idea is good, as for the multiplayer, this depends on the architecture being used, it its server / client, every unit creation should be authorized or at least validated by the server, so the server should be the one assigning ids by order of creation notification arrival, if it is peer to peer, it gets more tricky, you could assign one of the peers the title of host and give him the responsability of assigning IDs...


[/quote]

I was thinking more along the lines of a unit type scenario instead of the actual unit, something static that would never change move with adding or deleting. Sorry for the confusion =p

Share this post


Link to post
Share on other sites
I'm new into this, but did you consider the UUID (Unique Universal ID) it's fairly easy to use and it could save you a lot of time, I'd a post explaining how to create them in here: [url="http://crosstantine.blogspot.com/2010/07/creating-uuid-universally-unique.html"]http://crosstantine.blogspot.com/2010/07/creating-uuid-universally-unique.html[/url], usually my projects has an utility project that contains this function, it's pretty handy and avoid the "global" variables.

My 2 cents.

Share this post


Link to post
Share on other sites
Would it be ok to use this GUID as my ID's or is this over kill

[size="2"] GUID randomGuid;[/size]

[color="#008000"][size="2"][color="#008000"]// create random GUID[/color][/size][/color]

[color="#008000"][size="2"][color="#008000"][/color][/size][/color][size="2"] randomGuid = GUID_NULL;

CoCreateGuid(&randomGuid);



and if yes is this a valid thing to do with them

if(guid1 == guid2)
its the same ID

there seams to be lots of guid links in google but cant find any usful info. I'll go and try again now.






[/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this