What is difference ? (for-each or direct)

0 favourites
  • 5 posts
  • I can't understand why iterating an event with conditions would be less efficient than directly, because if for example I test a collision without a loop on a sprite with several instances it will necessarily iterate them anyway?

    Example: (2 instance) sprite A, (1 instance) sprite B

  • Running events has a small performance overhead for every event block, condition and action encountered. If you use 'For each' as in example 1, then it means the overhead for all the sub-events, conditions and actions is repeated for every instance. If you don't use 'For each' as in example 2, then the overhead is only done once.

    Sometimes however do you need 'For each' for correct results, such as if you increment a global variable in an action inside it, but my advice is not to use 'For each' unnecessarily. And as ever, if you want to know the real performance impact, measure it yourself.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In your example you just have to add the value that you increment multiplied by pickedcout, I really don't see why construct needs to re-iterate the overload, and I also don't understand why there is no other ways to achieve this, in unity or godot there is not this strange limit, how is it that the only way to avoid any overload must be done without iteration necessarily?

    It is essential for a certain type of application to iterate, for example: To create another way of developing in construct in game, if only that could be added (create / edit event in game?).

    Could an addon do such a thing?

    What do you think ?

  • It does the overhead because you asked it to. By adding a 'for each' you are telling Construct 'repeat all the sub-events, conditions and actions for each instance'. So it does that for you. If you don't want it to do that for you, then don't use 'for each'.

    In general, computers can't guess what your intent is. You have to tell it what to do, and it will do what you tell it. This is true of all programming languages: if you write inefficient code, it probably won't guess that you could have used a more efficient algorithm and replace it for you, it will just do the best it can with the code you gave it. If you want it to do something more efficient, you have to write the code to tell the computer to do that instead.

  • "Pick all" is generally used to break out of a previous event where you already picked something, but you need it to happen in a subevent.

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