How do functions work?

0 favourites
  • 7 posts
  • Hey all,

    I have been using functions and i was wondering how they work when called into action for multiple times simultaneously.

    For Example :

    There are 10 enemies and whenever an enemy dies. I call a function which creates an explosion at the enemies position, with the help of the function's parameters to store the enemies X and Y.

    Now when 2 enemies die simultaneously . Is the function called 2 times, 1 for each enemy, or is it stacked where it runs for one enemy and once finished, runs for the other enemy, or does it only run once ignoring the second enemy?.

    Now i have tried this with very mixed results . I have at times the function called 3 times simultaneously, and the functions sometimes runs twice, or once but never thrice i think. So anyone got any pointers.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It can depend on the particular case. The safest solution is to use a 'For each enemy' and call the function. That way the results are guaranteed.

  • I guess a lot of it has to do with only calling the function on triggered events..

    Also instead of using the x and the y as parameters I suggest using the enemy.uid so you can just pick that enemy to perform the actions on..

  • blackhornet

    I did use a for each loop. I'm still checking if my code is interfering in any way. still can't tell if functions work stack wise or not.

    LittleStain

    Thanks for UID tip.

  • LittleStain is quite right about the UID/picking. Another route is to use the 'On destroyed'. Just destroy the enemy, and then do your extra functionality in the 'On destroyed', rather than calling a function.

  • blackhornet

    Thanks for the help. i think it is better to move the enemy off screen then make it reappear elsewhere as another enemy rather than to destroy create destroy create. It's easier on the device.

    And i think I've figured out that functions run for each instance separately rather than stacking. Not certain but it seems that way when i run my project. Thanks again.

  • If your SOL contains more than one of your "enemy" object, only the first one will be affected. An explicit for each is thus needed to ensure the function is called multiple times (if needed).

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