A TADS3/adv3 module for assigning and managing unique object IDs

Not sure how much general use there is for this, but here’s a small-ish module for creating and managing unique object IDs: uniqueID github repo.

At preinit, the module will assign a unique numeric ID, an OID, to each object declared in source, not counting those declared in the module itself or in the stock adv3 library.

In addition, you can assign a unique alphanumeric ID, a UID, for each object, or have one automagically assigned by the module.

Usage is straighforward:

         guid(obj, 'someIdentifier');

To assign UID “someIdentifier” to object obj. If the UID is already in use, then the object will be assigned a UID by concatenating the second arg with a “#” and the object’s OID. So if obj’s OID is 123, the UID (in the event of a collision) would be “someIdentifier#123”.

To have the module supply a UID, use guid() without specifying a second argument. The UID will be of the form “obj#123”, where 123 is whatever the object’s OID is.

The return value of guid() is the assigned UID.

Each object’s OID and UID are assigned to properties on the object, _oid and _uid respectively.

Given an OID or UID you can obtain the object via:

         obj = uid2obj('someIdentifier');      // returns object with given UID
         obj = oid2obj(123);                   // returns object with given OID

I’m doing this as part of the memory/decision logic I’ve been working on, mostly to have an easier way to programmatically work with all the stuff (data structures, functions) indexed by object. Not sure how much use it’ll be to anyone else. But hey, now it’s out there.

The closest existing thing to this that I could locate was a contrib module, but it was more concerned with coming up with a canonical ordering of objects in source (which I don’t care about, all I need is a unique identifier).

2 Likes