For Each for object types in a family?

Get help using Construct 2

Post » Wed Oct 25, 2017 8:12 pm

Is there any way to loop through object types in a family? i.e. lets say I have a family, "Objects", with the following sprites: A, B, and C. I have 3 of A, 10 of B, and 1 of C.

Objects
A (x3)
B (x10)
C (x1)

I'd like to run a loop on the Family "Objects" that for each object type, picks those instances and does something. If I run a normal For Each, it would loop 31 times. I'd like to find a way to loop 3 times, pick all of each object type - A, B, and C, one per loop - and does something to those objects.

Possible?
B
15
S
2
Posts: 15
Reputation: 712

Post » Wed Oct 25, 2017 8:53 pm

BipolarMike wrote:Is there any way to loop through object types in a family? i.e. lets say I have a family, "Objects", with the following sprites: A, B, and C. I have 3 of A, 10 of B, and 1 of C.

Objects
A (x3)
B (x10)
C (x1)

I'd like to run a loop on the Family "Objects" that for each object type, picks those instances and does something. If I run a normal For Each, it would loop 31 times. I'd like to find a way to loop 3 times, pick all of each object type - A, B, and C, one per loop - and does something to those objects.

Possible?

why don't you make 3 familys? FamA, FamB and FamC instead of "objectsFam"?
B
20
S
8
G
8
Posts: 598
Reputation: 6,946

Post » Thu Oct 26, 2017 12:18 am

@BipolarMike
The only way to do this is to add an instance variable "type" to the family. Set type="A" for sprites A, type="B" for sprites B etc.
Then you will be able to pick family members by type and loop through them.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
17
S
9
G
155
Posts: 2,196
Reputation: 82,473

Post » Thu Oct 26, 2017 1:42 am

@nn81: If I did that I'd have to copy the code for each of the three families, instead of one single loop

@dop2000: That's not a bad idea! I think I might be able to use it as a starting point. Thank you!
B
15
S
2
Posts: 15
Reputation: 712

Post » Mon Jan 15, 2018 10:19 am

I would also like to know the answer. I have 20 items that are part of a family. I need to cycle through each one as each has their own states which affects other things. Therefore I need to reference the "self" value of the current object I am checking. For example

if obj1 has reached certain destination, set some_array.At(obj1.xpos,obj.ypos) to TRUE

I don't want to have to write the same line of code 20 times, so I want somethin along the lines of:

for each(obj){
current_obj = this obj
if current_obj has reached certain destination , set some_array.At(current_obj .xpos,current_obj .ypos) to TRUE
}
B
6
Posts: 11
Reputation: 292

Post » Mon Jan 15, 2018 10:45 am

@jtibbles
I'm not sure I understand your question. Have you tried "For each YourFamilyName" loop?

for each (FamilyName) {
if FamilyName has reached certain destination , set some_array.At(FamilyName.xpos, FamilyName.ypos) to TRUE
}
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
17
S
9
G
155
Posts: 2,196
Reputation: 82,473

Post » Thu Jan 18, 2018 6:07 pm

I assumed if I referenced "FamilyName" object within the foreach loop then it would be referencing the current object that it is cycling through ie. if 3 objects in a family:

foreach (FamilyName object){
sometext.setText("We are currently looking at FamilyName object ID Number "& FamilyName.ID )
}

So the first iteration would export the ID of the first FamilyName object, then the second cycle would export the ID of the second FamilyName object, then the third cycle would export the ID of the third FamilyName object

However this isn't the case. Every cycle of "FamilyName" would point to just the first object.

I eventually gave up with families so now I have 10 separate objects and therefore 10 lots of code.
B
6
Posts: 11
Reputation: 292

Post » Thu Jan 18, 2018 9:07 pm

That should be the case. Maybe post a screenshot of the event.
B
100
S
38
G
134
Posts: 5,550
Reputation: 85,313


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 2 guests