Is this a bug? Referencing by UID...

Discussion and feedback on Construct 2

Post » Fri Feb 15, 2013 7:46 pm

I came across this problem while trying to reference instanced objects variables using the object's UID. I isolated my problems down to some unexpected behavior in C2. Not sure if it's a bug, or if I'm doing something wrong when I reference the variable.

In the example, I have some instances of my sprite, some placed ahead of time in the layout, some created at run time. The original is at 0,0. When you click on them, the text box displays information about that sprite. First it's X position, then it's X position as referenced by it's own UID. Lastly it displays the sprite's UID. When you start clicking on different instances, you see that they seemingly randomly retrieve another instance's X position instead of it's own.
What is going on? Is it a bug, or have I done something wrong?

https://www.dropbox.com/s/t2o3doebf1x8yyb/bugtest.capx
B
11
S
4
G
1
Posts: 159
Reputation: 1,803

Post » Fri Feb 15, 2013 8:21 pm

Sprite(index), and Sprite.UID are two different things.
Sprite(index) is the index for that instance as it was created from 0 to sprite.count.
object.uid is a number assigned to any object when it is created, again from 0 to the number of objects in the game.
Note: uid starts when you add things in the editor, and continues at runtime when you create new objects, including instances.

What you probably want here is sprite.iid. No need for the () since you dont need to pick anything else.newt2013-02-15 20:25:34
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sat Feb 16, 2013 1:03 am

So I could go sprite.iid.X or sprite.uid.X? to get the X position for a given instance?
B
11
S
4
G
1
Posts: 159
Reputation: 1,803

Post » Sat Feb 16, 2013 1:35 am

Well you are picking the instance with the condition, so sprite.x, or self.x would be all you need. uid, and iid would be somewhat redundant.

Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sat Feb 16, 2013 1:45 am

In the example, it is redundant, yes. However, I'm just trying to figure out the proper syntax for use in expressions. So is that the proper syntax for referring to a particular instances variables via UID?

Edit: Apparently not, I just tried it. How would you make that set text action work the way I intended? From within the "set text to" expression?

Edit: It works if I use the IID in parenthesis. Cool. Still would like to know how to use the UID in expressions though.Yttermayn2013-02-16 02:03:26
B
11
S
4
G
1
Posts: 159
Reputation: 1,803

Post » Sat Feb 16, 2013 2:15 am

Yes iid is the sprite(index), so sprite(iid) is the picked sprite.
sprite.uid is also the picked sprite, but it has no use unless you save the uid to a variable, because you can't possibly guarantee what its uid will be at runtime.

The general rule of thumb is if you are not creating a bunch of new instances all the time, then use iid.
If you are then use uid, and pick via variable, or uid, as in object.uid= object.variable... do stuff.
Image Image
B
161
S
48
G
90
Posts: 7,356
Reputation: 66,767

Post » Sat Feb 16, 2013 2:16 am

You may want to check how events work.
Contrary to a "usual" coding language, the picking (selecting) of instance is done by the conditions in the events and the actions/expressions apply to the picked instances.

In your example, when you click on a sprite, only this sprite is picked, and so it doesn't matter to refer to it through its UID or IID.

You might also want to check in the how do I FAQ (link in my signature) at the section "Picking/Selecting instances" there are several topics worth checking and explaining all about picking in details.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Post » Sat Feb 16, 2013 4:37 am

Thanks, guys. This little example is just to help me work out this aspect of the picking system. The project I am working on involves a lot of procedural generation and being able to reference instances of run time generated objects correctly is essential.
I have been checking the FAQ's, manual, and forums before each post I make, but a lot of times expression syntax and other little details are left out.
B
11
S
4
G
1
Posts: 159
Reputation: 1,803


Return to Construct 2 General

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest