Event questions

0 favourites
  • 13 posts
From the Asset Store
Very simple code without excess options (15 events for server and 11 events for client)
  • I just started using Construct2 the other day and so far I am quite impressed. However, the event filtering seems a bit confusing even after reading the manual pages here on the site. Maybe somebody can shed some light on how things are supposed to work.

    What I was trying to do is decrement a monster's health counter every tick, check if it's reached 0, then delete the current instance and spawn a couple of new one somewhere else later.

    Here's the implementation that works:

    <img src="http://i.imgur.com/4MNlH.png" border="0" />

    (Alive and TimeLeft are instance variables that are initialized to True and 5 respectively. RespawnCount is a global var)

    That Foreach loop looks redundant so I tried this approach instead:

    <img src="http://i.imgur.com/6iRz9.png" border="0" />

    The monsters still die after 5 seconds, but even if there were 10 on screen only a single new one will spawn. Why ?

    Just for completeness sake- if I were to add a "System- Every tick" condition in front of #10 or as a parent-event of #10 would that have any impact at all ?

    (By the way I have a few decades of C++/Java experience- feel free to get as technical as you like- I really want to learn how C2's event loop works)

    Some random questions:

    The manual mentions local variables, but I don't see an option to insert them. Is that a restriction of the free version ?

    There's an "Add Include" command, but how do I create a new Event Sheet for it ?

    Exporting to HTML5 (with/without Minify) just shows me a black page when I try to run the index.html in Chrome. Preview from C2 works fine, uploading to Chrome Webstore and running the game from there works as well. However, I would like for this to be an offline-only game; what's a simple,reliable way to achieve that ?

  • +System: Every tick
      -> Monster: Substract dt from TimeLeft
    +Monster: TimeLeft <= 0
      -> Monster: Destroy
      +System: repeat 2 times
        -> System: create Monster on layer 0 at random(layoutwidth),random(layoutheight)

    Should work.

    It will spawn 2 monsters anywhere in your whole layout for one monster destroyed.

    You don't really need a 'Alive' boolean because the very fact that the instance is not destroyed means that it is alive.

    And indeed you don't need any foreach as long as condition on objects only relate to themselves.

    Also notice that the repeat loop is nested inside a parent condition.

    It's the same idea as

    first condition
    {
      action
      second condition
      {
        action
      } 
    }
  • First part I'm not sure what the problem is if you have it working. I'll need more details or to see the capx.

    Local variables are nested under a parent event, if you make a global var and drag it under a parent event it will make it local.

    To include a event sheet, on the folder icon in your objects ect bar right click and add another event sheet, ass your events such as controls ect and then on the event sheet you wish to include the other event sheet on right click and select include event sheer.

    Welcome to C2 by te way<img src="smileys/smiley1.gif" border="0" align="middle" />

  • Ninja'd by Yann <img src="smileys/smiley2.gif" border="0" align="middle" />

  • Thanks smitchell- got the locals & extra event sheets working now.

    Is there a way to have something akin to procedures/functions in C2? Let's say every time object A collides with object B I want some complex action to take place. However, when user presses the spacebar I want that same complex action to occur. What's the best way to go about this without copy/pasting everything ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • function plugin

    also check out the other plugins, very interesting things there

  • You don't really need a 'Alive' boolean because the very fact that the instance is not destroyed means that it is alive.hat variable is used somewhere else in code - once we start the death animation of a monster I set it to false so that no other actions are performed on that instance.

    nd indeed you don't need any foreach as long as condition on objects only relate to themselves.hat's what I thought and which is why I am puzzled about the behavior I described above. The first & second version I posted SHOULD behave the same way in my opinion. Yet the second one never seems to increase that Respawn counter past 1.

  • Normal, you set Respawn to 0 every tick o.o

  • That variable is used somewhere else in code - once we start the death animation of a monster I set it to false so that no other actions are performed on that instance.

    You still don't need it as you can check if animation is playing.

    s playing

    True if a given animation is currently set. Animations are identified by their name (case insensitive).

    Edit: Also for "functions" you can use the plugin, or use groups, activating/deactivating them when you need it.

  • Normal, you set Respawn to 0 every tick o.o

    But event sheets run from top to bottom, so it should only be 0 at the very end of the tick.

    This is the pseudo-code of what I had in mind:

    foreach monster:

    Subtract dt from monster.Health

    foreach monster:

    if monster.alive & monster.Health<=0 :

        destroy monster

        increase RespawnCounter by 1

    Repeat RespawnCounter times:

    System create new monster

    System Set RespawnCounter=0

        

    So first we decrease the lifetime counter for all monsters. Then pick the ones that have lost all their health (and have their "Alive" flag set) and kill those. For everyone that got killed increment global variable so we can respawn them later. Next run a loop and create a bunch of new monsters. Once we are done with that reset respawncounter to 0 so no more new monster appear.

    I am completely aware of the fact that you can get the same behavior with a dozen different conditions/actions. My question is: why is the example I posted in the OP not behaving as outlined in the pseudo code above? The monsters all disappear but only 1 new monster gets spawned. It's as if the Repeat loop only runs once.

  • probably because they all disappeared at the same tick.

    So if you don't use a foreach, you will trigger the add 1 to RespawnCounter only one time (even if all the monster are destroyed, it's like an internal loop)

    If you use the foreach, each monster will be taken into account and RespawnCounter will be increased for each monster that have their health <= 0

  • Yep, they all disappeared at the same tick.

    The C2 System manual states: or Each is commonly mis-used or used redundantly - actions already apply for each instance picked by conditions, so it often is simply not needed.hat's why I tried to remove the ForEach because it sounds like C2 should already be iterating over each instance that disappeared during that tick without me having to run a ForEach condition on it.

    For reference here's the bad code again:

    <img src="http://i.imgur.com/6iRz9.png" border="0" />

  • Well if you send me the capx I can show you how I would implement what you ask. 'cause you know... it's easier if you have a capx ready :D

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