Using UIDs with Functions

Get help using Construct 2

Post » Fri May 17, 2013 12:28 pm

I'm having some difficulties working out precisely how Function calling and Parameters work for my project

Basically, in my main project, I have a family of sprites with a wide variety of behaviors that I would like to make as separate Functions, but I'm having trouble passing the right 'picked' instances to the function

Right now I'm passing the UID of the instance to the function, but the problem I'm running into is that, if the function is called multiple times per tick, only the first UID is acted upon.

I've got a simple project demonstrating the issue:
https://dl.dropboxusercontent.com/u/59997937/Project%20Help/Picking_with_Functions.capx

(note that my own project is much more complex than this, so I'm not looking for work-arounds to make this specific demo work)

In it, there are a number of instances of the same sprite. Those instances have a boolean 'Active'. Any instance with 'Active' true should use the function 'Hop'. But if more than one instance is active, only the first instance hops.

I hope this makes sense, I haven't found anything on the forums beyond passing the UID to the function. Thanks in advance!
B
7
S
2
G
1
Posts: 4
Reputation: 997

Post » Fri May 17, 2013 12:41 pm

https://www.scirra.com/tutorials/361/understanding-uid-iid-health-cards-and-rocket-smoke-trail
ImageImageImageImageImageImage
B
93
S
20
G
13
Posts: 1,215
Reputation: 18,990

Post » Fri May 17, 2013 12:50 pm

Your mistake is that your function "Hop" is called only once per click, so it only get one ID (the first).Prox2013-05-17 12:51:07
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Fri May 17, 2013 1:23 pm


@TelleS0808
I went back and tried that method on my test file

As I understand it, you create a kind of 'surrogate UID' which is more reliable than the UID and which can be picked with the Compare Instance Variable function

...but when I did this, I couldn't get the function to recognize any of the objects! I honestly have no idea why, and I think it may have something to do with how Functions are called, but I don't know.

@Prox
I was under the impression that 'Hop' would be activated once for each instance of obj_creature for which Active was true - since all events are supposed to have an implied 'for each' loop through them

If this is not the case, how would I make this happen (since a For Each loop cannot be run in the same event as another Trigger) ?

EDIT: I made a silly mistake in implementing the ID trick, but that still left me with only the first object hopping

EDIT AGAIN: Figured out how to make the 'For Each' loop work properly, thanks for that. But I'm still very curious as to how the function works to need that, if anyone has a clear understanding of the 'under the hood' of picking in this instanceDevRaccoon2013-05-17 13:35:25
B
7
S
2
G
1
Posts: 4
Reputation: 997

Post » Fri May 17, 2013 1:45 pm



Working CAPX

But you could do the same without the "pick instance with UID"
B
7
S
1
G
1
Posts: 32
Reputation: 1,016

Post » Fri May 17, 2013 4:04 pm

@DevRaccoon:

Prox is correct, your function signature parameter expects a *single UID* as its Param(0). When you use the obj_creature Is Active, you filter your SOL list to just that set of creatures, however, when referring to expressions such as obj_creature.X or obj_creature.UID, internally Construct has no idea which element in that array you're referring to, so it just picks the first obj_creature in the filtered picked list for that property. It doesn't pass that group of UIDs as a collection, it passes a single one to the Function, so you have to call the Function() *once* for each creature.

That being said, in general, you want to do as much filtering as possible before the for-each call to optimize performance. Instead of iterating across the entire list of obj_creature and doing the "Active comparison" each time, it's better to pick/filter first, then use for-each:

B
30
S
6
G
8
Posts: 232
Reputation: 6,119

Post » Fri May 17, 2013 4:18 pm

Okay, I think I get it now! And most importantly it works

Thanks for the help everyone.
B
7
S
2
G
1
Posts: 4
Reputation: 997


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests