Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8875901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:05:51+00:00 2026-06-14T19:05:51+00:00

Say I am making a game and have a base Buff class. I may

  • 0

Say I am making a game and have a base Buff class. I may also have a subclass named ThornsBuff which subscribes to the Player class’s Damaged event.

The Player may have a list of Buffs and one of them may be the ThornsBuff. For example:

Test Class

Player player = new Player();
player.ActiveBuffs.Add(new ThornsBuff(player));

ThornsBuff Class

public ThornsBuff(Player player)
{
    player.DamageTaken += player_DamageTaken;
}

private void player_DamageTaken(MessagePlayerDamaged m)
{
    m.Assailant.Stats.Health -= (int)(m.DamageAmount * .25);
}

This is all to illustrate an example. If I were to remove the buff from the list, the event is not detached. Even though the player no longer has the buff, the event is still executed as if he did.

Now, I could have a Dispel method to unregister the event, but that forces the developer to call Dispel in addition to removing the Buff from the list. What if they forget, increased coupling, etc.

What I don’t understand is why the event doesn’t detach itself automatically when the Buff is removed from the list. The Buff only existed in the list and that is its one reference. After it is removed shouldn’t the event be detached?

I tried adding the detaching code to the finalizer of the Buff but that didn’t fix it either. The event is still running even after it has 0 references. I suppose it is because the garbage collector had not run yet? Is there any way to make it automatic and instant so that when the object has no references all its events are unregistered?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:05:51+00:00Added an answer on June 14, 2026 at 7:05 pm

    The event (player_DamageTaken) will continue to be called regardless of whether the Buff is in the ActiveBuffs list or not. This is because when you run DamageTaken += player_DamageTaken then it creates a reference from player.DamageTaken to that instance of ThornsBuff and its player_DamageTaken method. (Otherwise, how would the runtime know which instance of ThornsBuff to call player_DamageTaken on?)

    That reference will continue to exist until you call player.DamageTaken –= player_DamageTaken. The fact that you also add the Buff to ActiveBuffs simply adds another reference to the buff, which isn’t technically necessary (but it’s good form in my opinion since I don’t like having non-referenced event handlers hanging around).

    Now you’re probably wondering how to remove the event handler without causing the developer to call an extra Dispel or Dispose method, and for that here are some suggestions:

    1. At the beginning of player_DamageTaken do a check to see if player.ActiveBuffs.Contains(this), and if not, call player.DamageTaken -= player_DamageTaken and return. The disadvantage to this is that you’re searching the entire list of Buffs every time they take damage.

    2. Make ActiveBuffs an ObservableCollection (or something similar) that can listen for when buffs are removed from the list. When they are, you can call buff.Dispel automatically so that the developer doesn’t have to remember to call it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a game in PHP which is a money making game and
I am making a flight simulator game in XNA. I also have several blimps(Airships)
I'm making a snake game like player and I have the body moving fine
I'm making a new game that generates a huge grid (lets say 1000x1000). The
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
For a game I'm making I have a situation where I have a list
Say I am making a 2D Tile Based game in C# using System.Drawing for
Let's say I'm making an Use Case for a game that has a scoring
Here is the problem, lets say we are making a video game and want
I am making a platform game, but i have a problem with my collision

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.