How do I check instance variable in array

  • Hi,

    I manage instance variables of sprites with an array XY.

    The X is and ID of sprite and Y a number.

    I want to check along the array if the number of sprite in Y is equal to a number and destroy all sprites found.

    For example my array is :

    0 - 0,4
    1 - 1,4
    2 - 2,3
    3 - 3,3
    4 - 4,1
    5 - 5,1
    [/code:1e7rhwkn]
    
    If my number to search is 3 , i want destroy sprites with Y number 3 so i ll destroy sprites in 2,3 in the X of my array.
    
    Ty for helps.
  • Hello worm1,

    does that help?

    Although I do not understand why an array is needed. Wouldn't it be easier with an instance variable? (as in the disabled lines)

  • Thx kriand , it s seems to be a good solution but i can't pick instance with UID , i must pick instance with value in X of array . It s my problem

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So does the X index contain a instance variable of the sprite? Instead of 'pick by UID' you can take 'compare instance variable'.

  • Yes the X index is a instance variable of sprite , i called it "ID" but it isnt the "UID" of the sprite

  • Seriously, why do you need this array? Working with instance variables in C2 is much easier than working with 2D arrays.

    If you are sure that you need the array, consider doing this:

    Create 1-dimentional array, set Global=No, add it to the same container with the sprite. Now each instance of sprite will have its own instance of array for values. Sprite+array in each pair will be logically linked together, you'll not need the ID variable. You can pick a sprite and its own array will be picked automatically, and vice versa.

    This code will search all arrays for a number N and destroy sprites (and their arrays) if the number is found:

    For each Array
       Array For Each X Element 
           Array Current value = N    ->  Sprite destroy   
    [/code:ak13oj4u]
  • Thx kriand , it s work

    dop2000 i use an array because i have to remove for example n+3 sprites or n-3 sprites, i have to concatenate after the rest of sprites and reorder by id all and i don 't know it s possible just with instance variables?

  • You mean, if you destroy the sprite with ID=5, the sprite with ID=6 should become ID=5, ID=7 become ID=6 and so on?

  • Not exactly . For example i have 10 sprites with instance variable "ID" 0 to 9 and second instance variable "NUM" random 1 to 4 . I have a random number "n" start 0 until 9 and a second number "NUM2" random 1 to 4 . If "n" has value 3 for example i ll must check in the list of sprites the n+1, n+2 or n-1, n-2 so it will be sprites with ID 4,5 or 2,1 . After with the number "NUM2" i must compare instance variable "NUM" of sprite 4,5 or 2,1 and if is equal i destroy sprites with ID 4,5 or 2,1 and to finish i reorder the list of id like you say

  • worm1

    Here you go:

    https://www.dropbox.com/s/phwh7xejbayll ... .capx?dl=0

    Simple code, no arrays. You can even remove the ID variable and use IID instead.

  • Thank you very much dop2000 , that's exactly what I was trying to do and without using an array it's even better !

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