Creating a unique id for each unit

Started by
9 comments, last by ankhd 13 years ago
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
Advertisement
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.

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


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.
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
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.
Another alternative is to use a hash function to generate the ID, based on the object's content, or data.
Latest project: Sideways Racing on the iPad
If your storing them in a vector you could just use its position in the vector as its ID number.

If your storing them in a vector you could just use its position in the vector as its ID number.


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...

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



[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.


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
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: http://crosstantine.blogspot.com/2010/07/creating-uuid-universally-unique.html, usually my projects has an utility project that contains this function, it's pretty handy and avoid the "global" variables.

My 2 cents.

This topic is closed to new replies.

Advertisement