Are events atomic?

Get help using Construct 2

Post » Wed Aug 31, 2016 11:49 am

Hello,

I am still in the learning phase so this is probably trivial for the advanced developers among you.

I tried looking for the answer but I couldn't find it. Probably I am not using the correct keywords.

Basically, I am trying to assign a "health bar" to an enemy. The advice I read was to avoid the UIDs. I am using a global variable instead that gets incremented every time an enemy is created. It goes something like this:

1- Create enemy.
2- Increment global_var.
3- Assign global_var to enemy.local_var
4- spawn a health bar.
5- assign enemy.local_var to health bar.local_var.
6- Pin health bar to global var.

Am I guaranteed that steps 2 and 3 (part of the same event) will happen without interruption? Can another enemy creation event happen in a different thread and cause the global_var to be incremented between steps 2 and 3?

Many thanks.
B
14
S
3
Posts: 29
Reputation: 870

Post » Wed Aug 31, 2016 12:38 pm

The event engine runs on a single thread so unless you call a function (or a trigger runs) everything will happen in the exact order you specify.
Scirra Founder
B
397
S
236
G
88
Posts: 24,423
Reputation: 194,560

Post » Wed Aug 31, 2016 12:46 pm

Thanks for the reply Ashley. You said:
so unless you call a function (or a trigger runs) everything will happen in the exact order you specify.


I have my enemy creation done with a time trigger. First type of enemy is created every 3s, Second type every 5s and third every 7s.
Does that mean if the time trigger happens for 2nd enemy, it could interrupt the event triggered by the first enemy? If that happens then I can see a potential problem in the way I am creating the enemies.

If so, is there a better way to create enemies and link them to their health bar that doesn't suffer this issue?

Thanks again.
B
14
S
3
Posts: 29
Reputation: 870

Post » Wed Aug 31, 2016 2:13 pm

Timers never interrupt anything - in fact no events ever interrupt each other, they are queued to run synchronously with other events. I just meant if you use something like "call function", that jumps away to another event, runs it then comes back, by design.
Scirra Founder
B
397
S
236
G
88
Posts: 24,423
Reputation: 194,560

Post » Wed Aug 31, 2016 2:25 pm

That clears it. Thanks.
B
14
S
3
Posts: 29
Reputation: 870


Return to How do I....?

Who is online

Users browsing this forum: dop2000, MPPlantOfficial and 21 guests