February 7, 2013

Gamemaker (GML) Asynchronus Eventhandling

Posted in Development, Tools tagged , , , , , , , , at 3:03 pm by SarahAndrea Royce

While I was working on my february entry for #OneGameAMonth I ran into a problem. You see, Gamemaker is really good to develop games that follow a certain shape. But as soon as a game gets away from this scheme a bit and needs more individual coding, it’s not really that good. While objects play a vital part, in GML (The [G]ame[m]aker [l]anguage) their usage is somewhat limited. And what became especially problematic was the lack of Events.

See, events are a wonderful thing. Other objects register for the happening and one objects then, e.g. after a certain activity is finished, notifies the other objects. Not really knowing anything about them.

Gamemaker only provides a very limited kind inside the object, where they are essential on how gamemaker works. So its a bit confusing that they missed out on a more global scale. So without that, I had to trick around with global variables and status checks in the step events (which means code that is run through at the rate of the FPS) a ka very expensive one. And the more you do it, the more confusing it gets. It still was working fine when I finished multiplayer, but when I finished the first try on the singleplayermode the cardhouse crashed.

And instead of spending days on debugging, I instead decided to delete this kind of code completely (which meant I had a whole workday to get a working multiplayerversion again, too) and built my own eventmanagement system. And somewhat gnawing with a naming convention that is near on Gamemakers native functions to make it easy for other users to use it.

I have made a Zipfile with 3 Objects and 4 functions that you can import to your Gamemaker project (you can, but don’t have to, place them in an own group)  and it will provide you with the following three functions:

event_add(id, name, userEvent);

id = object that throws the event
name = the Name of the event (e.g. “animation_over”)
userEvent: The number of the user event to trigger if the event is fired

event_fire(id, name);

id = object that throws the event (default is the calling object)
the Name of the event (e.g. “animation_over”)

event_delete(id, name);
id = object that the event should be deleted for (default is the calling object)
name = the Name of the event (e.g. “event_animation_over”)

! always use this in the destroy event of your object to avoid memory leaks !

Once again the download link: EventHandlingGMX.zip

And please ignore the advertisement below, that’s not the download!