How to keep track of objects by an ID?

BlitzMax Forums/BlitzMax Programming/How to keep track of objects by an ID?

ImaginaryHuman(Posted 2005) [#1]
Hi folks. I thought I would just throw this small problem your way. I am trying to come up with a solution but nothing is springing up at me so far.

What I want to do is have numerous freely associating `objects`, let's say they are just instances of a custom type. These objects need to be saved to disk and loaded back in later. They also need to be passed over a network to another computer which needs to use the objects in the exact same way as originally intended, interacting with specific other objects correctly despite the fact that another computer system might have its objects stored in an entirely random order. What with the saving/loading/networking it throws a spanner in the works.

All of the objects are free floating ie they are not necessarily in a list or a fixed order or organization. Any given objects can freely associate or disassociate with any others. They can be loaded or created any order.

If there is some kind of collection of objects on one computer system that is set up to work with certain other objects, how could these possibly be passed for example over a network to another computer and still know which objects to operate on? If the objects are saved to disk with some kind of ID number then when they are loaded back in maybe their ID number doesn't make sense anymore, and maybe the objects sent over a network all have different ID's on another computer.

I thought perhaps about some kind of remapping but even to remap you'd need to have a unique ID of some sort for each object to begin with?

So how can I identify which object is which? I am going to use groupings and categorization but I don't think that will be specific enough.

Any ideas greatly appreciated.


Robert(Posted 2005) [#2]
Perhaps you could compute a random string for each new object (based on time and date to prevent duplicates) created, and then save that to file. When loading the object back in, you can recreate the connections by searching other objects by ID string.

Because these random strings are always going to be different, it should be possible to uniquely identify objects when sent over a network (transmit the random string as well)

Edit: This generates a random (but hopefully unique!) UID of constant 60 character length (with the most varying part on the left-hand side of the string to speed up comparison speed):

Function generateUniqueId:String()
	Local id:String
	Local result:String

	id=String(Rand(10000,20000))+MilliSecs()+CurrentTime()+CurrentDate()
	id=id.Replace(" ","")
	id=id.Replace(":","")
	id=id.Replace("-","")

	For i=0 Until id.length
		result :+ Right(Hex(id[i]),2)		
	Next

	Return result
End Function



Dirk Krause(Posted 2005) [#3]
You need what they call a GUID. It's a number that is unique within the known universe. It is usually created from the system time, the MAC adress and random seed.

The problem that you are adressing is 'data replication' and this is not for the faint of heart.

This is where I usually leave :-).


ImaginaryHuman(Posted 2005) [#4]
Umm, oh ok. ;-) Maybe the random string thing would work. Thing is it also needs to be synchronized with another system accross a network, not sure if that would work?

Maybe some kind of ... shared hash table or something?


Perturbatio(Posted 2005) [#5]
a possibility might exist through the use of Rand or Rnd and SeedRnd. SeedRnd allows you to set the random number generator to the same state each time, thus when you generate a random number you get the same results everytime.

Apparently with B3D there was an issue where some machines did not begin at the same position in the algorithm but I don't know if that exists with BMax or not.


ImaginaryHuman(Posted 2005) [#6]
Hmm, now that is actually a possibility. I think there was a thread already discussing whether this was cross-platform some while ago and apparently it is consistent. I could set the seed to a specific number and then have it generate a sequence of random numbers which would be the same on both systems. But hey, they really isn't going to work in the big picture because not only are objects not likely to be created in the same order on both systems, there can be different numbers of objects on both systems totally non-synchronized ... so really it needs to allow synchronization in a non-synchronized kind of world. Also if I use the random number generator to generate a sequence of numbers - why not just start at 0 and add 1 each time to create new numbers?

New objects could perhaps `register` with some kind of centralized registry, but the registry would have to be shared among all computers networked. Not impossible, but to synchronize them all at some point *after* they've already been up and running a while, would be something of a chore.

Hmmmmmmmmmmmmmmmmm.