How do functions work?

Get help using Construct 2

Post » Sun Jul 27, 2014 3:34 pm

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.
B
16
S
4
Posts: 140
Reputation: 1,458

Post » Sun Jul 27, 2014 4:06 pm

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.
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

Post » Sun Jul 27, 2014 4:18 pm

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..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Sun Jul 27, 2014 4:30 pm

@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.
B
16
S
4
Posts: 140
Reputation: 1,458

Post » Sun Jul 27, 2014 5:19 pm

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.
ImageImageImage
B
71
S
22
G
240
Posts: 3,732
Reputation: 133,990

Post » Tue Jul 29, 2014 12:57 pm

@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.
B
16
S
4
Posts: 140
Reputation: 1,458

Post » Tue Jul 29, 2014 1:50 pm

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).
B
71
S
30
G
25
Posts: 984
Reputation: 19,503


Return to How do I....?

Who is online

Users browsing this forum: adventurist, brunopalermo, Yahoo [Bot] and 34 guests