Pick instances by instance variable within an expression?

  • As above

    Is it possible to pick object instances based on an instance variable directly within an expression?

    For example

    If I have 5 point sprites P, each with an instance variable “ID” = (A) to (E) .

    And I want to draw a line between only 2 of the points, say P(ID=B) and P(ID=E).

    Then is there any way to pick them directly in an expression?

    Specifically thinking, passing function parameters in a function call?

    eg in semi-psudocode.... just calling a function passing the X and Y values of P(ID(B)) and P(ID(E))

    On button click - call function (“drawlinebetween”) (Parameter(0)= Sprite P(with instance variable (“ID”=B).X), Parameter(1)= Sprite P(with instance variable (“ID”=B).Y), Parameter(2) = Sprite P(with instance variable (“ID”=(E).X, Parameter(2) = Sprite P(with instance variable (“ID”=(E).Y ).

    On function “drawlinebetween” - [draw line between points XY1 and XY2] (function.Para(0) , function.Para(1), function.Para(2), function.Para(3) )


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Try it!

    If you run into any issues, we'll help you fix it!

  • There's a system condition called "pick by evaluate." You can choose the object type you want to pick by clicking it in the window that pops up, then use an expression like "object.variable = 1"

  • You can access other instances by iid with sprite(i).x, where i is an instance number. I guess you could make a helper function to pick the instance by a variable and return the iid. I'd just change your drawl one function to take the variable values and do the picking inside the function.

  • spacedoubt

    thanks I did try it but I couldnt see a way of doing it directly with construct 2 built in features hence the pseudocode


    yes, currently I am solving the issue by picking instances before feeding them to the function but it will prove very ineficient for large amounts of sprites and you cant use this pick by evaluate within an expression hence the question.


    Dude , youre a genius. Why didnt I think of this?

    I feel a bit dumb now......(especially as I was talking about functions. See Im starting to understand this construct 2 stuff but im still not thinking like a coder)

    Thankyou very much

    I will try it this weekend and post a capx so people get what I was on about

  • so I tried it out on something "simple" (haha) for this question


    and it worked. thanks again R0J0hound

    for anyone that interested theres a capx in my arcade link t(the second one "GooTwo") in the above thread.

    or straight here https://www.scirra.com/arcade/other-games/goo-two-14408

    you will see that the drawline function passes parameters that are themselves calling function getIDfromUID to get the index number of the sprite from the UID (which could be swapped for an IV) , hope that makes sense


    stamping as solved...... (how to I change the heading to marked solved ????)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)