NPN328
NPN328

Reputation: 1942

Why use custom events instead of direct method calling?

I'm new to programming and I've been checking a lot of game coding tutorials. I've noticed that on most of them they use custom events to trigger methods instead of calling a method directly.

What's the reasoning behind this practice? Why aren't they just calling the method?

For example:


We have two objects: A and B. A has method A.methodA() that B needs to use when X condition is triggered.

Why implement:

B dispatches an event to A that tells A to run A.methodA()

Instead of:

B uses A.methodA()

Upvotes: 13

Views: 1913

Answers (5)

FilippoG
FilippoG

Reputation: 586

I built my own, very simplified, events dispatcher system. AS Event model is very powerful, but in 99% of situations you don't need that power. A simple callback with parameters fired as an event is more than enough. You can still retain the versatility from an event model, but don't need to write too many lines of code for, let's say, a simple button. I can setup a simple event like this:

Buttonizer.autoButton(_buttQuit, this, "onPress");
public function onPressQuit(c:Sprite) {
// Execution goes here
}

You can build your own event model, it will make life simpler, and your code much more concise.

Upvotes: 1

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

The main reason is separation of interests. When using events, class A doesn't need to know about the existence of class B (and vice versa).

Some benefits to this are:

  • Much easier unit testing (you can test Class A without class B)
  • Less chance of breaking your code when you change class A or B
  • Less references to other classes in your code, which reduces the potential for memory leaks
  • Cleaner code
  • More flexible/reusable code (a bunch of other classes could all listen/respond to the event without any additional code in the your dispatcher)

Upvotes: 16

The_asMan
The_asMan

Reputation: 6402

You might want to read this.

And also note using the callback method allows you to pass parameters to it directly and not via a custom event model.

Upvotes: 1

Josh
Josh

Reputation: 8149

Personally, I use custom events simply for the ease of use. I can have one class dispatch an event when something happens (say an animation finishes or an error occurs in a download) and any number of other classes run any number of other functions based on that event. In addition, I code for reusability. The goal of each class is complete independence so that it can run in any project without needing other packages. So rather than have one class call a method of another class, I dispatch an event from the first class that the second class listens for and then run that method. Then when I need that first class for another project, I can just copy/paste it without having to modify it and without losing any functionality.

EDIT: Also, it's worth noting that sometimes people do what you describe to get around having to pass in event arguments.

Say you have a button on the stage and you need to be able to click it, but you also need to be able to manually call that method. Some people don't realize you can pass in a null event and have only the single method. Or you can set it as a null default argument, see below:

private function onClickHandler( e:MouseEvent = null ):void{
    //as long as you never reference "e" within this method, this method can be used both for MouseEvent listeners and manually calling it elsewhere in the code
}

That technique can help avoid having an event handler that only calls another method and nothing else. At this point in my programming, every single AS3 event handler I write sets the event argument to null by default. It just makes things easier later on.

Upvotes: 2

lostPixels
lostPixels

Reputation: 1343

Typically in bigger applications using events will help abstract everything. When you have 15+ classes and they're all ditpatching events to a controller, it's a lot easier to figure out what's going on than reading through all different parts of the code to trace functions. Using callbacks begins to create spaghetti code.

However, direct function calls are going to be executed faster than events.

Upvotes: 4

Related Questions