Delegates

Happy to learn about delegates but why do we use one here? if we made UpdateLevel pulic we could just call it in GainExperience in the experience script.

Loose coupling my man… loose coupling.

1 Like

Whenever possible, we want all script relationships to be one way, not two way. As Michael says, it’s a matter fo Loose Coupling vs Tight Coupling.

Since BaseStats calculates the level based on a call to Experience to get the number of current Experience Points, we know that BaseStats must be dependent on Experience. Were Experience to then call BaseStats, then Experience would also be dependent on BaseStats which makes these two classes tightly coupled.

By using the Observer Pattern, Experience doesn’t need to know anything about it’s subscribers to function correctly. You could have a dozen classes all subscribing to Experience.onExperienceGained, and Experience doesn’t need to know anything about them whatsoever. It just needs to see if anybody has subscribed and invoke the event appropriately.

thnx alot for clearing it up.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms