Generating UUIDs in Corona

Pretty straight forward question: I want to generate UUIDs to use as a unique way to track many game objects. Is there an out-of-the-box method in the Corona SDK to generate a UUID?

Thanks...

We are looking for a solution for this too!!!

Does this need to be unique across all game instances on any device? Could you, for example, generate an MD5 hash of UDID and then append the time?

Sorry I'm not very familiar with using MD5, so not quite sure what you mean.

The basic scenario would be:

Somewhere in a game loop there will be logic that may try to spawn from 1 - n arbitrary game objects (within a single loop). To ensure that each object can be treated uniquely, I'd like to try and add a data member to it which contains a unique variable (normally in other languages I would use some kind of UUID generation function).

Technically, as a rough-as-guts method, I could keep a global counter running which is incremented whenever a new object is spawned, but I wouldn't know how high that number could need to go over a long period of time, and also introduces logic risk where the number may not be properly incremented somewhere in the game code.

If you had a simple example of your method, I'd be most interested. The key would be that every single time the generator is run, it must return a unique key (it can be local to the app itself though).

Many thanks
Cel.

For us...
We are trying to generate a object on a touch. The app will generate multiple objects over time from multiple touches. Our strategy is to use a generic function and a global counter then to dynamically append a nameID to each object's base name at the time of spawn. This is so we can make the object unique from the others so that when the user touches the object again (in a delete mode)...the object will remove itself.

Without the unique id...the delete function listener only removes the last object created...not the one being touched...since all spawned objects have the same object name.

We are looking into tables as a way to sort them...but it seems a unique object name is the best solution...if available.

I believe in actionscript...the naming appendix convention uses []s...such as:
objectBaseName[ uniqueID ]

How bout using a random number with a timestamp at the time the object was created ?

Pseudo Code

1
2
local myObject = display.newRect(touch.x,touch.y,someWidth,someHeight);
myObject.UUID  = getRandomNumber()+getCurrentTime();

But then how do you call it later?

like this?
myObject.123456: removeSelf()

I think I've tried that and it doesn't seem to work?

ah !

the touch event has a target member.

1
2
3
4
5
6
7
8
9
10
local function myEvent(event)
      
     local obj = event.target
 
     -- check if obj is valid 
 
     if ( obj ~= nil ) then
          obj:removeSelf()
     end
end

Maybe I'm not clear about the exact requirements... but couldn't you use the object's reference as a unique ID?

Like newObj.name = tostring(newObj)

The advantage is that this name is guaranteed unique for the existence of the object, and that the name of the object is inherent. The latter may be used to easily identify the object inside of generic event handlers.

-Frank.

Good call Frank, but I wonder if an object is deleted, would the internal name it had be recycled and assigned to some other constructed object at a later time? (I don't know if that's the case). Also, would an object's unique instance name be unique only to the scope it was created in or would it globally unique?

The toString(newObj) is essentially the memory reference so it will only be unique within the lifetime of the newObj as the memory will be recycled.

For my app that uniqueness is good enough... Not sure it meets your requirements...

-Frank.

Frank says :

"The toString(newObj) is essentially the memory reference so it will only be unique within the lifetime of the newObj as the memory will be recycled."

Carlos says

Very true.

;-)

c

I just submitted code for a simple UUID/GUID-generator at the code-exchange:

http://developer.anscamobile.com/code/uuidguid-string-generator-coronalua

-FrankS.

views:2110 update:2011/10/5 21:23:48
corona forums © 2003-2011