Most efficient way of picking objects while matching their values

Not favoritedFavorited Favorited 0 favourites
  • 2 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • From time to time I find myself needing to pick any instance of ObjectA, if the value of its instance variable matches the UID of ObjectB, but in doing so ObjectB also needs to be picked.

    Sometimes I also need to count the number of instances of ObjectA which instance variable match against the UID of ObjectB, which means that it would need to return 0 if there are no such matches.

    I've used various methods to achieve this ranging from looping to using containers, but given that Construct often has clever shortcuts to otherwise convoluted operations, does anyone know what the most efficient way of achieving the desired results is, by only using pure vanilla events and expressions, and without using custom code or plugins?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This will loop over each of objB and pick any objB with a variable that matches B's uid. If you only have one instance of objB you can omit the loop.

    for each objB
    objA: variable=ObjB.uid
    -- do something

    If no instances of objA are picked the event won't run. However, you can utilize else for the case when nothing is picked. Mind the indention that represents nested events:

    for each objB
    -- objA: variable=ObjB.uid
    -- -- do something if 1 or more A's are picked
    -- else
    -- -- do something when 0 are picked

    Extending on that you can know how many instances are picked with the .pickedCount expression. But again, bear in mind that an event won't run if no instances are picked so you still need to utilize else.

    for each objB
    -- objA: variable=ObjB.uid
    -- -- text: append objA.pickedCount&" instances picked"&newline
    -- else
    -- -- text: append "0 instances picked"&newline
Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)