0 Favourites

Using UIDs with Functions

  • 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:

    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!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Your mistake is that your function "Hop" is called only once per click, so it only get one ID (the first).

  • 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 instance

  • <img src="http://world-inventors.com/screen3.png" border="0">

    Working CAPX

    But you could do the same without the "pick instance with UID" <img src="smileys/smiley17.gif" border="0" align="middle">

  • 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:

    <img src="https://dl.dropboxusercontent.com/u/12667027/Construct%202/FunctionForEach.png" border="0" />

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

    Thanks for the help everyone.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
76 9,273
Pandadoor's avatar
Pandadoor
Unread hot topic
0 Favourites
[plugin] 3D using Copperlicht
72 25,984
Reken's avatar
Reken
Unread hot topic
67 9,854
spartacuswell's avatar
spartacuswell