Game Development Community

Script names and IDs - IDs unique?

by WesTT · in Torque Game Builder · 07/17/2008 (6:51 am) · 12 replies

I was under the impression, perhaps mistakenly, that the IDs of SimObjects were meant to be unique for every object added to the scene, regardless of their script name. Is this the case? I just noticed that when I had two objects of the same name, they also had the same ID. Kind of odd that I did not notice this until now.

Regards

#1
07/17/2008 (10:27 am)
But how do you know it was two objects with the same name/id and not actually the same object?

I'm pulling at straws here but, one case that could cause this confusion...

If you declare an object of the same name as an existing object (i've only done this with objects of the same class) it will just reinitialize the original object -- you don't actually get two identical object from that.

Or if you set the name of two objects the same within the editor, then when you reference that object name in script you are probably just always getting one of them and never the other.
#2
07/17/2008 (10:44 am)
OK, here's the setup. I have a group of spawn points in a scene, and these spawn points control, well, spawning enemies. I wanted a fairly easy way to shut them down and start them up at any time, so I created a spawn manager behavior, and an interface that will register the spawn point with that manager. The register code is very simply:

%this.managerObj.spawnPointManager.registerSpawnPoint(%this.owner);

Ahh, "But Wes... " I hear you say that's just passing in the name of the owner object, and if you have two objects with the same name you will get the problem you are having. So, very well, I tried:

%this.managerObj.spawnPointManager.registerSpawnPoint(%this.owner.getID());

This too gave the same result I was experiencing. That of some spawn points being turned on and most of the rest being left off. I have now determined that because I am using a SimGroup to store the spawn points, and the both the object name and the object ID returned is the same no matter the object instance in the level builder it is not adding it to the SimGroup, thus the object is being ignored.

Now, I suppose the reason could be that because I simply copied the objects from another of the same type in the level builder? However I was under the impression that all objects would be assigned a unique identifier regardless? And thus by using 'getID' objects with duplicate names would still be added to the SimGroup provided I actually passed in getID and not the name. I guess I am not saying that I do not understand all this, what I am really just asking is "Are IDs returned from getID guaranteed to be unique and if not, why? And what do we have to do to make sure that they are?" etc.
#3
07/17/2008 (10:47 am)
I would recommend putting in a few echo()s -
echo the id right at the call to registerSpawnPoint(), echo it inside registerSpawnPoint(), etc, etc.
#4
07/17/2008 (10:47 am)
Unless this is some crazy 1.7.4 bug (which seems unlikely) then the answer is "IDs are unique for all simobjects".

If %this.owner is returning the same object id every time then its because %this.owner is the same object for all your behaviors. Or registerSpawnPoint is actually just getting called on the same behavior over and over and skipping others...
#5
07/17/2008 (10:52 am)
Oh yes, sorry about that I did in fact have some output to show.

Quote:
Registering spawn point: 3413 <-- call to registerSpawnPoint
Registering spawn point ID: 3413 <-- inside registerSpawnPoint
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17

These are calls made by "unique" objects in the level builder. Standard t2dSceneObjects with the register behavior attached.
#6
07/17/2008 (10:57 am)
How can they be made by unique objects in the level builder if %this.owner is the same for all of them?
#7
07/17/2008 (11:00 am)
What I mean by that is that the objects, as represented by the level builder are unique and separate from one another. They have separate position and properties unique from one another. Again, as represented in the level builder and the level file itself.
#8
07/17/2008 (3:00 pm)
When does the code that registers a spawnBehavior's owner with the spawnPointManager get executed? Is it inside onLevelLoaded?
#9
07/17/2008 (3:43 pm)
Wes, did you say that your spawn points all have the same name?
#10
07/17/2008 (3:45 pm)
You say:

Quote:I have now determined that because I am using a SimGroup to store the spawn points, and the both the object name and the object ID returned is the same no matter the object instance in the level builder it is not adding it to the SimGroup, thus the object is being ignored.

Now I am assuming with your code you might be grabbing the object ID for the simGroup ? After you add all your spawn points have you tried doing the following command in the console to echo out the spawn points:

<simgroupname>.listObjects();

Looking at the logs:

Quote:Registering spawn point: 3413 <-- call to registerSpawnPoint
Registering spawn point ID: 3413 <-- inside registerSpawnPoint
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17
Registering spawn point: 3413
Registering spawn point ID: 3413
Registered Spawn Point, Total: 17

It looks like you are returning the one ID which might be the objectID of the SimGroup ?

Just a thought, you may want to post more of your code though.
#11
07/17/2008 (9:59 pm)
Sorry guys, I crashed out before. It was 4am after all. :)

@James: It is indeed inside onLevelLoaded. There is a behavior attached that allows one to set the spawnpoint manager to register with. It then calls registerSpawnPoint on the manager within the callback, this is why I figured it should guarantee uniqueness, at the very least by using getID.

@Phillip: Originally they did. I made sure to name each of the t2dSceneObjects in the builder with unique names. It was only just recently that I had this problem when I was setting up some more spawn points. When the problem did present itself (as I had just copied a spawn point a couple of times, so it had the same name, and I neglected to rename them initially) I thought I would try using getID.

The possibility could be that getID() is simply returning the ID of the first object it finds with that particular name, this would make sense in a way. I was initially under the impression however that accessing %this.owner guaranteed that it would return the owner instance of that behavior. So I was also wondering that if there was some sort of clash with naming, that getID could perhaps return the ID of the actual owner instance. This is all based on my naivety with regards to TGBs underlying code base of course. :P

The code for the registerSpawnPoint function:

function SpawnPointManagerBehavior::registerSpawnPoint(%this, %spawnPoint)
{
   echo("Registering spawn point ID: ", %spawnPoint);
   if (!isObject(%spawnPoint) || !isObject(%this.spawnPointGroup))   
      return;
      
   %this.spawnPointGroup.add(%spawnPoint);
   %spawnPoint.setEnabled(false);   
   
   echo("Registered Spawn Point, Total: ", %this.spawnPointGroup.getCount());
}

Not much to it really, and I am simply outputting the %spawnPoint parameter.

The code for the call to the register function:

function SpawnManagerInterfaceBehavior::onLevelLoaded(%this, %scenegraph)
{
   if (!isObject(%this.managerObj))  
      return;
      
   echo("Registering spawn point: ", %this.owner.getID());

   %this.managerObj.spawnPointManager.registerSpawnPoint(%this.owner.getID());
   //%this.managerObj.spawnPointManager.registerSpawnPoint(%this.owner);
}
#12
07/17/2008 (10:15 pm)
Are you saying that you still have objects with the same name? If so that could screw up all kinds of stuff. Don't do it '-p