Re-Code some of my game: FOR-EACH + PICK ?

  • Hey All,

    As I'm learning Construct while working on my game, I appreciate the amazing fact that there are many ways to code the same thing and it's very dynamic and amazing to me.

    So, while I'm working on my game there is a part of the code where I have X amount of instances of the same enemy should be chosen randomly and change direction.

    THIS CODE is working perfect, but... one of my goals is to see if I can "compress" some of my code to have more efficient to my eyes and on the way learning new ways to do the same thing.

    This is the current code: (works great!)

    Notice the Red Enemies Wandering:

    If you'll look carefully on the GIF, Every single instance of this enemy is picked randomly and not just ONE at the time but it could be few, each one is affected individually by this code which is exactly what I wanted.

    Now, my CHALLENGE is to find a shorter code using "FOR EACH" and maybe "PICK" combined?

    Could this be done without additional Variables? that will be magic!

    I didn't find a good way to make it work with FOR EACH and PICK: my results always chose ONE at the time. means once it PICKS an instance, it affected ONLY ONE of them at the time, so the others either ALL stopped, or ALL continue walking without stopping at all, which is not what I wanted to do unlike the (working perfect) example above.

    Can you please help me Re-Code the same example using the built-in "FOR EACH" + "PICK" events and maybe make this magic in a shorter, simple code? it will be a GREAT way for me to learn as well.

    This should be interesting, PLEASE add either example file, GIF or a snapshot of the code so I can give it a try.

    Once again: I know, it works, I should leave it like that... but it's a great way for me to learn other ways using FOR EACH and PICK and see different thinking from you guys with much more experience than me.

    So the shortest code, the less variables, the better! but do it YOUR way and I'll give it a try.

    Thanks ahead!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Alon, some quick comments:

    - In Construct events, like I think you're seeing, the loop and pick are combined (e.g., your Slomper_Red Choose_random <= 0 condition). In your example, creating a separate loop and condition would take up one more line than what you have now. I would use a for loop if I needed an index value and/or really wanted to walk through the whole set of instances.

    - For variables, you might want to use a single Velocity instance variable and store +/- (2,...,6) in it, which could replace your Choose_random and Direction variables. Sometimes that's better or sometimes not, and the most important thing is what makes sense to you and is easiest to work with in your project.

  • I can only advise against using Wait like this. Imagine, the enemy plays the IDLE animation and the Wait command starts. Exactly at this moment you inflict damage on the enemy and a corresponding animation / movement starts. This would be interrupted as soon as the Wait command has expired and the enemy suddenly plays the WALK animation and runs away in one direction. All events that start during the Wait command can be interrupted shortly afterwards.

    If I remember correctly, I've given you an example with the timer behavior. You should use this behavior, since you can also cancel timers at will, or specify further conditions for triggering them.

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