Page 1 of 1

### UID vs IID Tutorial

Posted: Wed May 09, 2012 6:37 pm
I know thereb is a tutorial on the correct use of Unique ID and Instance ID but I'm stumped if I can find it.If anyone could point me to where it is I would be very grateful.

### Re: UID vs IID Tutorial

Posted: Thu May 10, 2012 12:34 am
*Edited to include Kyatric's feedback about the UID not changing.*

I gained insight about UID and IID by reading some forums posts. Here is my attempt to summarize the two.

C2 increments the UID every time an instance of any object is spawned. C2 increments IID every time an instance of a specific object is spawned.

Remember that C2 is spawning instances of the circle, square and triangle object types. Also, indexes are zero based so they start at 0 not 1.

Here's an example. Lets say we have three sprites a circle, a square and a triangle. We tell C2 to spawn 3 circles then 3 squares and then 3 triangles. The UID will increase each time any objects are spawned while the IID will only increase when an instance of a specific object type spawns, for example Circle.

UID     IID
Circle 1      0       0
Circle 2      1       1
Circle 3      2       2
Square 1      3       0
Square 2      4       1
Square 3      5       2
Triangle 1    6       0
Triangle 2    7       1
Triangle 3    8       2

There isn't a right or wrong way to use UID or IID. Your use of UID or IID depends on what you are trying to do. If you want to select the 2nd square you could use the UID of 4 or the Square.IID of 1. Both will give you the same result.

Something to keep in mind is that the UID and IID will change if you destroy one of the object instances. Using the table from above, if we delete the second instance of the square object type the values will change like this.

UID     IID
Circle 1      0       0
Circle 2      1       1
Circle 3      2       2
Square 1      3       0
Square 3      5      1*
Triangle 1    6*      0
Triangle 2    7*      1
Triangle 3    8*      2

Notice how Square 3 is now the 2nd instance of the square object and it's IID changed to 1? You should also notice that the UID of Square 3 stayed the same. All of the triangle UID and IID values stayed the same.blueseth2012-05-11 01:50:03

### Re: UID vs IID Tutorial

Posted: Thu May 10, 2012 2:16 am
Hum there is a mistake in the explanation right before.

The UID doesn't "slide"/change. If you delete an instance, its UID is simply gone. The IID is the value getting rearranged on spawn and destroy of instances.

A quick capx to demonstrate

You can see in the capx, that on the second sprite line, there is an offset, we'd expect UID 4 and instead the first instance on the left is 5.
That's because I added the mouse object before deciding to add the second line.
You can also see if you right click a sprite to destroy it that the IID gets reorganised for the instances that are left, whereas the UID stand still.

And the "tutorial" you were originaly looking for should be listed in the how do I FAQ, section "Picking/selecting instance(s)".
Kyatric2012-05-10 02:18:06

### Re: UID vs IID Tutorial

Posted: Fri May 11, 2012 1:47 am
Thank you for the clarification.

### Re: UID vs IID Tutorial

Posted: Fri May 11, 2012 4:13 am

Thanks for the simple explanation.

### Re: UID vs IID Tutorial

Posted: Fri May 11, 2012 5:14 pm
UID should be considered as a pointer to that specific object for as long as it exists. As long as you have that UID you can access that object. IID is basically just another way of getting the .Count of a specific object. It can be useful in some situations but most of time you can handle anythin like that with a loop and .Count

UID is what you want to use if you need to act with exactly specific objects regardless of how many exist in the layout