Pick by UID in an expression

Get help using Construct 2

Post » Mon Apr 22, 2013 3:54 pm

I know you can select the IID by this: sprite(2).x

But is there a way to do that but for UID instead? I have been going through the documentation and not coming up with any luck.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Mon Apr 22, 2013 4:33 pm

Sprite.uid
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,418

Post » Mon Apr 22, 2013 4:34 pm

There are also pick by UID actions as well.
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,418

Post » Mon Apr 22, 2013 5:07 pm

@BluePhaze - Oh sorry I should be more clear: I want to be able to select it followed by more detail. So if I type sprite(2).x, I will get the X location of the 3rd instance of 'sprite'. However if I use sprite.UID, that's it! I can't then select its X, Y or variables. This is important because I don't want to actually pick the object, just reference its information.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Mon Apr 22, 2013 5:47 pm

@Tobye - I had to do something similar to this, and while this might be a terrible way to handle it, it worked for what I needed ;)

I started by creating a dictionary to hold UID-IID pairs, so when I created an object, I added an entry to the dictionary with the UID as the key, and IID as the value. Then retrieving information from that object during an expression was something like:

sprite(Dictionary.Get(aUID)).X

I ran into some problems when I removed objects though, as that can change the IID causing the info in the dictionary to be incorrect. To fix it, I just ran a loop that reset the IIDs for the objects stored in the dictionary.

Like I said, maybe not the right way to do it, but it worked for my needs. Of course if I find out there's already an expression in there for this, I'll lose it ;)




B
9
S
3
G
4
Posts: 63
Reputation: 3,083

Post » Mon Apr 22, 2013 6:38 pm

@rogueNoodle - Thanks for the idea, I mixed it a little with what I was currently working with and it seems to work okay now. I stored the IDs in local variables instead of a dictionary though which makes things much neater, but might not work for you if it's a long running system.

I also was concerned about the IDs changing as things were destroyed, but it's all contained within a function call for now and I was able to work the creations and destructions in a way that it isn't currently a problem. But, if later on it does become a problem I'm scared it'll take me a long time to figure it out! xD Since the objects I'm dealing with are dictionaries, there no visual issue I can notice.

Let me know if you do find a simple expression for this!
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Mon Apr 22, 2013 8:02 pm

The best way is to use a local variable and a subevent. Use 'Pick by UID' in a subevent, store sprite.X in the local variable, then after that event finishes the local variable has the X co-ordinate of the sprite with a specific UID in it.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Tue Apr 23, 2013 12:03 am

@Tobye If you use the pick by UID option you can access the specific object that the UID refers to and all it's variables in that same event, just use sub events within that event.
B
49
S
11
G
10
Posts: 1,833
Reputation: 14,418

Post » Tue Apr 23, 2013 4:48 am

@BluePhaze - Yeah the problem I was having was tricky though, because I wanted to add the variable information of one instance to another instance of the same object. So, if I pick by UID I can access it's info, but then I need to pick the other instance before I can apply the info, so the UID picked instance is longer picked. So if there was a way to just select by UID in an expression I would get the information and be able to apply it straight away, without any of the extra fuss I have in there right now.

@Ashley has a good solution, thanks! It takes a little more work to setup than what I had (as it's a long list of variables), but it's less liable to run into problems.
B
27
S
8
G
5
Posts: 429
Reputation: 6,902


Return to How do I....?

Who is online

Users browsing this forum: Radulepy and 5 guests