I am making a game, where two zombies will spawn after every three moves my character makes. The problem is that I have no idea how to do this.

I have my character moving correctly, in each key press event, so I want the game to count each time one of these events are triggered, and perform the action after every third trigger. Perhaps I need a global variable/counter that can count these events?

How do I make instances of enemies spawn after every three keypress movements?

  • 1
    \$\begingroup\$ Hi Toby, and welcome to Game Dev Stack Exchange. Check out our tour to see how we work. Please tag your Game Maker questions with [game-maker]; we prefer people do not put tags in titles. Also, when you reach 20 rep, you'll be able to join us in Game Development Chat. \$\endgroup\$ Jun 12 '17 at 12:19

Here is some pseudocode:

If keypress do
If KeyPressCounter%3 == 0 do
    Action ()

This preforms a modulo operation which will detect numbers divisible by 3.

  • \$\begingroup\$ This doesn't appear to give any guidance on how to do this in game maker. \$\endgroup\$ Jun 12 '17 at 12:19
  • \$\begingroup\$ StackExchange is not here to code for you, this pseudocode has valid logic and can be applied in GML. I would recommend @DeaSTL to extend his explaination to help OP understand why this works. \$\endgroup\$ Jun 13 '17 at 16:47
  • \$\begingroup\$ Besides, i've never used game maker but i've used other libraries. \$\endgroup\$
    – DeaSTL
    Jun 14 '17 at 16:15
  • \$\begingroup\$ Simpler: if keypresscounter++%3 :) \$\endgroup\$
    – Charanor
    Jun 15 '17 at 11:18

If you want to create this type of event, I suggest this :

If your character moves with the directional keys, then create four Keypress event : one for each key (Up, Down, Right, Left). Create a variable in Create event, and increment this variable in the four event previously created. Then, in the Step event, check if the variable is equal at 3. If yes, do your action and set the variable at 0. If not, do nothing.

Here what it looks in code :

Create event :


Key press left event :


Key press right event :


Key press up event :


Key press down event :


Step event :


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.