A summary (of my thinking) of why we use events here

After watching 10+ various videos, I believe I finally understand how our events being used here, following is my summary for your reference and for mine as well:

" When Enemy died then invoking Health.AwardExperience() in which we call GainExperience() from Experience Componet;

GainExperience() then calls onExperienceGained() event which has already subscribed another event called UpdateLevel() when loading BaseStats sciprt as their signatures are the same;

Importantly, UpdateLevel() can check newLevel vs currentLevel by invoking CalculateLevel(); In doing above: we basically get rid of the idea of using Update() to check level and logging “you levelled up” for each frame, instead the UpdateLevel() is invoked only when GainExperience() was executed once any enemy is killed.

I kind of understand why we need event/delegate here as onExperienceGained() event must subscribe the UpdateLevel() in advance then we are able to invoke UpdateLevel() when GainExperience() is executed.

In short, when player killed an enemy GainExperience() would execute and invoke our event onExperienceGained() which in turn inovke the already subscribed function UpdateLevel() to compare level. "

I think this should be it, welcome any corrections if I’m mistaken any part. Thank you.

I think you got it, unless I myself am mistaken :slight_smile:

Thank you, if the logic is correct, then the health display should apply the same logic by using event would be some improvement.

Absolutely. As a rule of thumb, almost all UI should be event driven by the time your game goes to production. Used as a test mechanism, like Sam and Rick are doing now, Update() loops are fine, but for optimum performance, the Observer Pattern (and event driven model) is the ideal way to go.

3 Likes

Thank you for this good knowledge. You guys are the best tutors I’m lucky to have.

Privacy & Terms