Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How do you use user events in GameMaker?

Answer

User events in GameMaker are custom events that you can trigger manually in your code. They are useful for organizing and structuring your code, especially when you have specific actions that don't fit into the standard event types like step, draw, or collision. In GameMaker, you have up to 16 user-defined events (from 0 to 15) that can be called for any instance.

To use a user event in GameMaker, follow these steps:

  1. Define a User Event in an Object Go to the object where you want to use the user event, right-click on the Events tab, hover over Add Event, then go to Other and select one of the User Event options (0-15).

  2. Add Actions or Code to the User Event Just like with other events, you can now add actions or GML code that should execute whenever this event is triggered. This could be anything from handling input to managing custom game mechanics.

  3. Trigger the User Event from Code To trigger this event, use the event_user() function within any GML script or event. For example, if you want to trigger User Event 0, you would call it like so:

    // Trigger User Event 0 for the current instance event_user(0); // Trigger User Event 0 for a specific instance, such as obj_Player with (obj_Player) { event_user(0); }

Here’s an example scenario where you might use a user event:

Imagine you have an object obj_Enemy that has a special ability it can perform. Instead of clumping all the code for this ability into the Step event or creating a custom script, you can put it in a user event for organization and readability. Any time you want the enemy to perform this action, you just call the user event associated with the ability.

// Inside obj_Enemy Create Event // Initialize attributes or other setup code // Inside obj_Enemy User Event 0 (setup earlier) // Put the special ability code here // Inside obj_Enemy Step Event or some other event when you want to trigger the ability if (special_condition_met) { event_user(0); // Triggers the enemy's special ability code }

Remember to only use user events when they make sense and help improve code clarity. There's no need to use them for everything, but they are very handy for specialized tasks that might clutter your main events.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost