Understanding event loops and action blocks

0 favourites
  • 6 posts
From the Asset Store
25 high quality isometric ground blocks + sprite sheet
  • I thought I understood how C3 runs actions, but looks like I don't so I want to confirm with the community...

    Here's my code. I was expecting the highlighted action block to run 15 times, i.e. once for each enemy, meaning the counter would increment to 15. However, when I run it the counter only increments to 1 but all 15 enemies have their health set to 0. Can someone please explain why it works like this?

  • Event 3 needs to be a sub-event of 2, it's not. You can see by the indentation its at the same level as On_start. Drag it under event 2 - you should see it position accordingly.

  • Yeah, but even if you move event 3 under 2, the loop will not work correctly, because you are creating Enemy object, not Enemies family.

    It's not really clear what you are trying to do.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm not trying to do anything in particular with this code example, I'm just trying to understand how C3 executes events/actions in general. I was expecting event 3 to work like this:

    for each enemy whose health is > 0

    1. increment enemyKillCount by 1 (would be executed 15 times)

    2. set that enemy's health to 0 (would be executed 15 times)

    But instead it seems to me that C3 is evaluating/executing event 3 like this:

    if there is at least one enemy whose health is > 0

    1. increment enemyKillCount by 1 (would be executed 1 time)

    2. for each enemy whose health is > 0, set their health to 0 (would be executed 15 times)

    Is my understanding correct here?

  • Your event 3 is a top level event with no dependencies on other events. It will run every tick and will check for every enemies health value and return them all to 0 if there are any that are greater than 0.

    If there were continuously enemies with health greater than 0 you would see 1 added to kill count every tick.

  • If you want to run a loop for enemies with health>0, you need an event with two conditions:

    Enemies compare health>0
    System For Each Enemies
    	Add 1 to enemyKillCount	
    

    But as other people mentioned, this whole event should not be at the top level. It needs to be nested under some other event to be executed only once when needed. For example, when the level is finished. When it's at the top level, it will be run on every tick and your kill counter will increment like crazy.

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