Search code examples
htmlconstruct-2

How do I make action for certain instances of object?


I don't want to make action to the object in general I have 8 instances for example (0,1,2,3,4,5,6,7) I need to make the action on 2 , 5 and 7 only for example how ??


Solution

  • This is something that really confused me about Construct 2 coming from a software engineering background.

    Picking which instances of a Sprite to effect works sort of like filtering a database. You start with all the instances in a list, and then you filter them out using conditions. When possible, Construct 2 will automatically guess which instance you want. (Like if you just spawned Enemy at EnemySpawn, it will know your next reference Enemy is only the last one created).

    This magically works most of the time, but you can select a set of objects with a few of these conditions:

    Compare instance variable Compare the current value of one of the object's instance variables.

    Is boolean instance variable set Test if one of the object's boolean instance variables is set to true. (Invert the condition to test if false.)

    Pick by unique ID (UID) Pick the instance matching a given unique ID (UID) number.

    In my experience, adding an instance variable and setting it to keep track of a subset of Sprites or the state of the sprite is the cleanest way to limit an action to a specific group of sprites.

    See also Object Expressions from the Scirra Manual:

    You can add a 0-based object index to get expressions from different object instances. For example Sprite(0).X gets the first Sprite instance's X position, and Sprite(1).X gets the second instance's X position. For more information see index IDs (IIDs) in common features. You can also pass another expression for the index. Negative numbers start from the opposite end, so Sprite(-1).X gets the last Sprite's X position.

    These are not "specific" object IDs (Construct 2 calls them Index IDs, or IIDs) but the refer to all the objects of that type in the order they were created.

    So:
    Enemy(0) is the first
    Enemy(-1) is the most recent
    Enemy(5) is the 6th Enemy created, ignoring destroyed Sprites (See below).

    You have to keep in mind though, that if you destroy an object with IID 3, then all the objects after it will shift in the list and their IID will decrease by one. (Like a LinkedList data structure)

    If you destroy instance D, the others shift

    Instance |A|B|C|D|E|F|     -->     |A|B|C|E|F| 
    IID      |0|1|2|3|4|5|     -->     |0|1|2|3|4|
    

    Hopefully one of those methods can help you get to what you need!