Translations

Know another language? Translate this tutorial!

Stats

5.2K visitors
33.7K page views
865 translation visitors
5.9K translation page views

Understanding UID, IID, Health, Cards and Rocket smoke trail.

Favourite 82 favourites
Tutorial written by TELLES0808Originally published on 16th, August 2012 - 8 revisions

Introduction

Here you'll learn the basic of how "UID" and "IID" works, also, how the health of the enemy works.

Who never tried to setup a rocket with smoke trail?

And who never stuck on it while thinking how to setup the smoke to coincide with the rocket when creating it?

All these issues are interrelated and the solution is very easy, you just need some principles.

UID and IID

When you run the game, it create all the objects placed on the canvas, giving them a Unique ID (UID) and a Index ID (IID). They will change when you destroy or create a new object on the canvas.

All the issues and a better explanation about these two is placed on the Manual.

Ashley words:
"IIDs and UIDs in the editor don't change depending on the Z order, but they do change if you delete and insert instances. Because of this you should not rely on knowing UIDs or IIDs in advance at all - the instance with UID 3, for example, could change to a different object if you add or delete some things in the layout view. This could easily break your game or make it work differently "randomly" (it will seem random to many users because they won't realise certain things they did changed the UIDs and IIDs). For this reason I don't think UIDs or IIDs should ever be displayed in the editor, in order to discourage this bad practice."

As said, UIDs and IIDs are necessary for the Engine, but you don't need to rely at all about them.

Instead of using UIDs and IIDs, you should use conditionals where only the wanted object can meet.

Call it by encapsulate an object.

Encapsulating Objects

Let's start with a single example of a Rocket.

Resource file: Instance.CAPX
HTML5: Live sample

Procedure
Procedure


Minuteness
The procedure above show how to creating the rockets and place the smoke on their tail.

Every time the Engine create a new object, instead of picking their UID or IID (what can change on their life, and shuffle your ordering), it add +1 to a instance variable of a fixed object on your canvas, as an index.

The fixed object here is the text, but you can use an invisible sprite, or, a HUD object.

On created, the conditionals set to the rocket instance variable that index, giving him an unique ID instance.

Also, set the same index to the child smoke (child because the smoke 1 was created by the rocket 1, so, the rocket 1 is parent of smoke 1).

On destroyed, the conditional requires the smoke ID, and destroy only the smoke with the same ID of the rocket.

Because all the IDs are really unique, you don't need to rely about shuffling issues.

The same thing works with a HUGE amount of issues.

For example, when a Orc pick a weapon on the ground, now you can know what weapon was picked, because when you create this weapon, you'll give it the same ID of the Orc ID, then, when the Orc is destroyed, you can set to the weapon be destroyed too.

You can make a poker game, giving for each card an ID, where the ID can setup the frame position and show the right card. Now you can shuffle the cards without worrying about destroying them.

Make special rewards for Unique monsters and making him glow just turning an invisible effect to be visible when the variable ID meet a certain conditional.

And, my favorite, it works perfectly for achievements. ^^

All this explain what means "Encapsulating the Object", but still missing to explain about the health.

When you check if a rocket hit an enemy, them set the enemy life to subtract 1, you're encapsulating the event to that specific enemy.

How Instances Works
I think it covered all the proposed on the title, sorry if it's too high language (it means poor language on the programming field) or garbage, but I'm surely it will help someone having their first contacts with Construct 2!

This is a new simple sample of card shuffling and instance IDs manipulation:
http://www.scirra.com/arcade/example/1837/dealing-with-instance-variables

More samples:
Dealing with multiply enemies, how you can set their ID and manipulate each one:
Download the CAPX
Live Sample

https://dl.dropboxusercontent.com/u/47035927/Samples/IDs/sample2/index.html
https://dl.dropboxusercontent.com/u/47035927/Samples/IDs/sample2/filtering_IDs.capx

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Comments

4
hajas 19.9k rep

very cool guide :)

Thursday, August 16, 2012 at 4:08:42 PM
1
Hillstrom 4,593 rep

A must read!
I get lost sometimes trying to keep OO concepts when there's no code in C2. I almost fall in the UID/IID trap.

Saturday, January 19, 2013 at 7:49:52 PM
1
Hillstrom 4,593 rep

Looking into similar approaches to the same problem, of keeping instance in a somehow individualized and controlled, in all ways and "moments", i wonder if by version r116 of C2 still this kind of methods works better than relying on UIDs/IIDs even indirectly as parsed to local variables. I'm still using this method from @TELLES0808 . There's a better way so far?

Tuesday, January 22, 2013 at 1:23:12 AM
2
TELLES0808 22.8k rep

Comment by Hillstrom

Looking into similar approaches to the same problem, of keeping instance in a somehow individualized and controlled, in all ways and "moments", i wonder if by version r116 of C2 still this kind of methods works better than relying on UIDs/IIDs even indirectly as parsed to local variables. I'm still using this method from <span style="background:yellow;font-weight:bold;">@TELLES0808</span> . There's a better way so far?
You have to deal with sorting to use the UID as index. I did a sample game of racing where the ranking used this system, just to figure out and learn. The main problem of sorting the UID is deal with dependents, while using my method you can simple add a number followed by the dot, like this "dependent 1"will have the index instance as 1.1

Thursday, January 24, 2013 at 3:19:41 PM
4
oosyrag 64.4k rep

I think the Container feature is more suitable for this type of application now? I understand that feature was released after this tutorial was written.

Anyways great tutorial on the concepts and principles of picking instances!

Saturday, March 02, 2013 at 6:38:39 PM
1
Telyko 2,825 rep

That was very helpful thanks!

Wednesday, April 10, 2013 at 11:07:19 PM
1
Lordshiva1948 41.0k rep

Thanks for this tutorial

Saturday, June 08, 2013 at 3:46:04 PM
1
AmpedRobot 13.0k rep

Thanks very helpful.

Thursday, July 18, 2013 at 10:40:19 PM
1
thehorseman007 14.6k rep

Great and nice tutorial, love to see those nice examples, Live and Capx files are great.

Thanks for your hard work to get this tutorial cool and good.

Friday, December 06, 2013 at 7:33:01 AM
1
Steyo 357 rep

You just saved my life ! Thank you ;)

Monday, March 17, 2014 at 8:31:16 PM
0
greenleafvt 795 rep

Hi TELLES0808,

One question about this. Lets say I was making an isometric game and wanted the player to be able to select a tile and change a value - example the tile is dirt and they select what is planted on the tile. I have created instance values for the tile to have all the options but find the values only work for the fist instance of the sprite - not each individual instance.

Tried a few things. Making each instance have a value as I'm trying to do procedral generation from a json at some point, but to save CPU I would like to have the same graphic repeated and be able to load a sprite on top of it if the instance value was set to a number by the player vs making 1000 diferent lines of code .

I experimented with the child method but it still places things in the first instance of the object.

Let me know if you have any clues.

Thanks!

Wednesday, June 28, 2017 at 1:53:10 AM
0
superzenmachine 2,141 rep

This article is extremely informative and helpful, thank you very much!

Wednesday, July 19, 2017 at 4:05:37 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.