UID vs IID Tutorial

0 favourites
  • 6 posts
From the Asset Store
Act on your instances, get/set their properties and variables only thanks to their UID. No more picking trouble! Plugin
  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • *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.

  • 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)".

  • Thank you for the clarification.

  • Thanks for the simple explanation.

    <img src="smileys/smiley1.gif" border="0" align="middle" /> <img src="smileys/smiley1.gif" border="0" align="middle" /> <img src="smileys/smiley2.gif" border="0" align="middle" /> <img src="smileys/smiley32.gif" border="0" align="middle" /> <img src="smileys/smiley1.gif" border="0" align="middle" />

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)