How do I check instance variable in array

Get help using Construct 2

Post » Sat Jan 20, 2018 11:34 am

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 :

Code: Select all
0 - 0,4
1 - 1,4
2 - 2,3
3 - 3,3
4 - 4,1
5 - 5,1


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.
B
6
S
1
Posts: 48
Reputation: 580

Post » Sat Jan 20, 2018 1:24 pm

Hello worm1,

does that help?
Image

Although I do not understand why an array is needed. Wouldn't it be easier with an instance variable? (as in the disabled lines)
B
8
S
2
G
1
Posts: 79
Reputation: 1,404

Post » Sat Jan 20, 2018 1:45 pm

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 :)
B
6
S
1
Posts: 48
Reputation: 580

Post » Sat Jan 20, 2018 1:56 pm

So does the X index contain a instance variable of the sprite? Instead of 'pick by UID' you can take 'compare instance variable'.
B
8
S
2
G
1
Posts: 79
Reputation: 1,404

Post » Sat Jan 20, 2018 2:31 pm

Yes the X index is a instance variable of sprite , i called it "ID" but it isnt the "UID" of the sprite :)
B
6
S
1
Posts: 48
Reputation: 580

Post » Sat Jan 20, 2018 2:45 pm

Image
B
8
S
2
G
1
Posts: 79
Reputation: 1,404

Post » Sat Jan 20, 2018 3:03 pm

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:

Code: Select all
For each Array
   Array For Each X Element
       Array Current value = N    ->  Sprite destroy   
B
17
S
9
G
127
Posts: 1,762
Reputation: 67,845

Post » Sat Jan 20, 2018 4:00 pm

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?
B
6
S
1
Posts: 48
Reputation: 580

Post » Sat Jan 20, 2018 4:25 pm

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?
B
8
S
2
G
1
Posts: 79
Reputation: 1,404

Post » Sat Jan 20, 2018 8:27 pm

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
B
6
S
1
Posts: 48
Reputation: 580

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 18 guests