Are events atomic?

0 favourites
  • 5 posts
From the Asset Store
14 amazing sound files of game events like Level Ups, Level Completes, object spawn, object taking etc.
  • 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.

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the reply Ashley. You said:

    [quote:2jfimzp0]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.

  • 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.

  • That clears it. Thanks.

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