For Each for object types in a family?

0 favourites
  • 8 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • 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?

  • 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"?

  • 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.

  • 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!

  • 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

    }

  • 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

    }

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • That should be the case. Maybe post a screenshot of the event.

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