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… [import]uid: 9428 topic_id: 2534 reply_id: 302534[/import]
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… [import]uid: 9428 topic_id: 2534 reply_id: 302534[/import]
We are looking for a solution for this too!!! [import]uid: 9492 topic_id: 2534 reply_id: 7308[/import]
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? [import]uid: 54 topic_id: 2534 reply_id: 7353[/import]
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.
[import]uid: 9428 topic_id: 2534 reply_id: 7354[/import]
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] [import]uid: 9492 topic_id: 2534 reply_id: 7355[/import]
How bout using a random number with a timestamp at the time the object was created ?
Pseudo Code
local myObject = display.newRect(touch.x,touch.y,someWidth,someHeight);
myObject.UUID = getRandomNumber()+getCurrentTime();
Carlos [import]uid: 24 topic_id: 2534 reply_id: 7472[/import]
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?
[import]uid: 9492 topic_id: 2534 reply_id: 7474[/import]
ah !
the touch event has a target member.
local function myEvent(event)
local obj = event.target
-- check if obj is valid
if ( obj ~= nil ) then
obj:removeSelf()
end
end
Look at the sample code “drag me” … you can see how the touch event you can drag an object around.
Hope that helps.
Carlos
[import]uid: 24 topic_id: 2534 reply_id: 7479[/import]
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.
[import]uid: 8093 topic_id: 2534 reply_id: 7517[/import]
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? [import]uid: 9428 topic_id: 2534 reply_id: 7540[/import]
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. [import]uid: 8093 topic_id: 2534 reply_id: 7601[/import]
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 [import]uid: 24 topic_id: 2534 reply_id: 7643[/import]
I just submitted code for a simple UUID/GUID-generator at the code-exchange:
http://developer.anscamobile.com/code/uuidguid-string-generator-coronalua
-FrankS.
[import]uid: 8093 topic_id: 2534 reply_id: 24964[/import]