I don't need to keep track of the object, just want to make sure it's valid before I add or remove a sound. I came up with this general purpose method:
Don't call orxStructure_GetGUID() as it's going to mask a part of the value and it won't work. This won't tell you if this object has been deleted and another one recreated in the same spot though.
If you want to retrieve or check if one of your object is still alive, for example to remove a sound you added on it.
orxU64 guid = orxStructure_GetGUID(object);
orxObject_AddSound(object, ...);
// Later on
// Retrieve the object on which we added a sound
object = orxOBJECT(orxStructure_Get(guid));
if(object != orxNULL)
{
// Still the same object and still alive
}
else
{
// Not alive anymore or has been replaced by another object in the same memory location
}
Sorry, the part about when to use orxStructure_GetGUID() wasn't very clear:
You should use it most all the time, with one exception: when you want to compare its value with orxSTRUCTURE_GUID_MAGIC_TAG_DELETED to know if an object is deleted. But usually you don't want to do that.
I guess I can change the value of orxSTRUCTURE_GUID_MAGIC_TAG_DELETED in the future to removed that limitation.
What you should be doing is never store the pointer to your object. You variable should be the GUID, not a pointer.
When you need to use the object with the orxObject_* API, you should locally retrieve it with orxStructure_Get(), then you can use it but you still should not store it.
Comments
I can't trigger the assert again. I'll try the executable on a few different machines.
A GUID will never be orxNULL. It can be orxU64_UNDEFINED or orxSTRUCTURE_GUID_MAGIC_TAG_DELETED.
If you want to check if an object is deleted, you can do:
Don't call orxStructure_GetGUID() as it's going to mask a part of the value and it won't work. This won't tell you if this object has been deleted and another one recreated in the same spot though.
If you want to retrieve or check if one of your object is still alive, for example to remove a sound you added on it.
Confused though, you said not to use orxStructure_GetGUID() but then used it in the listing.
I also want to test against a fresh object, one that started with a class definition.
Anyway, I'll see how I go.
What I really want to achieve is something like this:
orxOBJECT *object;
orxU64 guid = orxStructure_GetGUID(object);
or
orxOBJECT *object;
orxU64 guid = orxSTRUCTURE(object)->u64GUID;
But of course in both examples, the object is only declared as a type and not initialised.
I want to catch all orxNULLs, uninitialised, and the other two states you mentioned above.
You should use it most all the time, with one exception: when you want to compare its value with orxSTRUCTURE_GUID_MAGIC_TAG_DELETED to know if an object is deleted. But usually you don't want to do that.
I guess I can change the value of orxSTRUCTURE_GUID_MAGIC_TAG_DELETED in the future to removed that limitation.
What you should be doing is never store the pointer to your object. You variable should be the GUID, not a pointer.
When you need to use the object with the orxObject_* API, you should locally retrieve it with orxStructure_Get(), then you can use it but you still should not store it.