• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Calling the C# gurus

Merad

Platinum Member
Simple question that google isn't answering for me... is the following bad?

Code:
MySystem.MyEvent += MyHandler;

// later...

MySystem.MyEvent -= MyHandler;

// later...

MySystem.MyEvent -= MyHandler;

Explanation:

I need to apply an acceleration to an object for a certain time interval. I do so by having the object subscribe to an event that fires just before the physics simulation performs a time step. When the acceleration is complete, it unsubscribes from the event, but it also needs to unsubscribe in the event that the object's Dipose() is called while the acceleration is in progress, but that means it unsubscribes twice.
 
You might look into an entity component system http://en.wikipedia.org/wiki/Entity_component_system .

But from the sounds of it, it sounds like you're coupling is a little weird. The object shouldn't subscribe and unsubscribe itself, but rather the manager/system/overlord/lifetime thingy should be in charge of subscribing and unsubscribing the various objects to the events. By having the object know about the various event systems you have to do a lot of work in each new object to let it know and interact with the various systems. That is a lot of code doing essentially the same thing.
 
To your specific question, no I don't believe detaching the handler twice will cause an error. It's a worrisome pattern for other reasons Cogman gets into, but it's a safe call imo.
 
Seriously, all I need to know is whether unsubscribing twice will cause a random crash, the implosion of the universe, etc.

ECS is massive overkill because there is literally one type of entity (two if you count static world geometry).

In a "real" game, yeah, the entity would probably send notifications to the physics system requesting that an acceleration be applied or removed. But this isn't a real game, it's about 1500 LOC to provide a visual representation of some academic code.

Edit: Replied before I saw Mark's post.
 
Unsubscribing is just removing a pointer from a stack when the system finds that pointer.
If the pointer does not exist, everyone goes on their merry way.
 
Back
Top